标签:ons 最优 style 素数 复杂 owb ems 前缀和 fine
题意:一个集合S的优美值定义为:最大的x,满足对于任意i∈[1,x],都存在一个S的子集S‘,使得S‘中元素之和为i。
# include <cstdio> # include <cstring> # include <cstdlib> # include <iostream> # include <vector> # include <queue> # include <stack> # include <map> # include <bitset> # include <set> # include <cmath> # include <algorithm> using namespace std; # define lowbit(x) ((x)&(-x)) # define pi acos(-1.0) # define eps 1e-8 # define MOD 1000000007 # define INF 1000000000 # define mem(a,b) memset(a,b,sizeof(a)) # define FOR(i,a,n) for(int i=a; i<=n; ++i) # define FO(i,a,n) for(int i=a; i<n; ++i) # define bug puts("H"); # define lch p<<1,l,mid # define rch p<<1|1,mid+1,r # define mp make_pair # define pb push_back typedef pair<int,int> PII; typedef vector<int> VI; # pragma comment(linker, "/STACK:1024000000,1024000000") typedef long long LL; inline int Scan() { int x=0,f=1;char ch=getchar(); while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} return x*f; } inline void Out(int a) { if(a<0) {putchar(‘-‘); a=-a;} if(a>=10) Out(a/10); putchar(a%10+‘0‘); } const int N=1005; //Code begin... VI v[N]; LL val[N], que[N]; int p[N], head, tail; void sol(int x, int &q, LL &w){ FO(i,q,v[x].size()) { if (v[x][i]>w+1) break; w+=v[x][i]; ++q; } } int main () { int n, m, x, A, B, K, T; n=Scan(); FOR(i,1,n) { m=Scan(); while (m--) x=Scan(), v[i].pb(x); sort(v[i].begin(),v[i].end()); p[i]=m; FO(j,0,v[i].size()) { if (v[i][j]>val[i]+1) {p[i]=j; break;} val[i]+=v[i][j]; } } T=Scan(); while (T--) { A=Scan(); B=Scan(); K=Scan(); int q=p[A]; LL w=val[A]; head=-1; tail=0; FO(i,0,v[B].size()) { while (v[B][i]>w+1) { if (head<tail || !K) break; if (head>=tail&&K) w+=que[head], sol(A,q,w), --head, --K; } if (v[B][i]<=w+1) { que[++head]=v[B][i]; while (head-tail+1>K) ++tail; } if (head<tail || !K) break; } if (K) while (head>=tail) w+=que[tail], sol(A,q,w), ++tail; printf("%lld\n",w); } return 0; }
标签:ons 最优 style 素数 复杂 owb ems 前缀和 fine
原文地址:http://www.cnblogs.com/lishiyao/p/7029491.html