NC24365. [USACO 2012 Dec G]Gangs of Instanbull
描述
输入描述
* Line 1: N (1 <= N <= 100) and M (1 <= M <= N) separated by a space.
The total number of cows in all the gangs will be N. The
total number of gangs is M.
* Lines 2..1+M: The (1+i)th line indicates how many members are in
gang i. Each gang has at least 1 member.
输出描述
* Line 1: Output YES on a single line if Bessie's gang can control the
field after the conflict. Otherwise output NO on a single
line.
* Line 2: If Bessie's gang can control the field after the conflict
output the maximum number of cows that could be on the field
on a single line.
* Lines 3..2+N: On the (i+2)th line output the index of the gang of
the cow that appears in the ith minute in the
lexicographically earliest ordering that leaves the maximum
number of cows on the field after the conflict.
示例1
输入:
5 3 2 1 2
输出:
YES 1 1 3 2 3 1
说明:
INPUT DETAILS:C++14(g++5.4) 解法, 执行用时: 4ms, 内存消耗: 480K, 提交时间: 2020-07-06 17:49:32
#include<bits/stdc++.h> #define db double #define ll long long #define inf 0x3f3f3f3f #define ms(x,a) memset(x,a,sizeof(x)) #define vc vector #define pb(x) push_back(x) #define debug cout<<"***"<<endl #define sd(x) scanf("%d",&x) #define sdd(x,y) scanf("%d%d",&x,&y) #define sl(x) scanf("%lld",&x) #define sll(x,y) scanf("%lld%lld",&x,&y) #define pd(x) printf("%d\n",x) #define pl(x) printf("%lld\n",x) #define rep(i,a,b) for(int i = a;i <= b;i++) using namespace std; const int maxn = 2e2 + 10; const ll mod = 1e9 + 7; int n,m; void init(ll a[],int b) { for(int i = 0; i <= b; i++) { a[i] = 0; } } struct node { int num,val; } ss[maxn],tmp[maxn]; bool cmp(node a,node b) { return a.num > b.num || (a.num == b.num && a.val > b.val); } bool cmp1(int x,int y) { return x > y; } int bk[maxn],bk1[maxn],res[maxn]; priority_queue<int,vector<int>,less<int> >q; bool judge(int k) { for(int i = 1; i <= m; i++) { bk1[i] = bk[i]; } bk1[k]--; while(q.size())q.pop(); for(int i = 1;i <= m;i++){ if(bk1[i])q.push(bk1[i]); } int cur = 0; while(q.size() > 1){ int t1,t2; t1 = q.top(); q.pop(); t2 = q.top(); q.pop(); t1--,t2--; if(t1){ q.push(t1); } if(t2){ q.push(t2); } } return q.size() == 0; } int main() { // ios::sync_with_stdio(false); // cin.tie(0); scanf("%d%d",&n,&m); for(int i = 1; i <= m; i++) { scanf("%d",&ss[i].num); ss[i].val = i; bk[i] = ss[i].num; } for(int i = 2;i <= m;i++){ q.push(bk[i]); } int cur = 0; while(q.size() > 1){ int t1,t2; t1 = q.top(); q.pop(); t2 = q.top(); q.pop(); t1--,t2--; if(t1){ q.push(t1); } if(t2){ q.push(t2); } } if(q.size() == 0){ cur = 0; } else if(q.size() == 1){ cur = q.top(); q.pop(); } int ans = bk[1] - cur; if(ans <= 0) { cout<<"NO"<<endl; } else { if(m == 2){ cout<<"YES"<<endl<<ans<<endl; for(int i = 1;i <= ss[1].num;i++){ cout<<1<<endl; } for(int i = 1;i <= bk[2];i++){ cout<<2<<endl; } } else { cout<<"YES"<<endl<<ans<<endl; bk[1] = cur; for(int i = 1; i <= n - ans; i += 2) { for(int j = 1; j <= m; j++) { if(bk[j]) { bk[j]--; // cout<<i<<" "<<j<<endl; res[i] = j; for(int k = j + 1; k <= m; k++) { if(judge(k)) { // cout<<i<<" "<<k<<endl; res[i + 1] = k; bk[k]--; break; } } break; } } } for(int i = n - ans + 1; i <= n; i++) { res[i] = 1; } for(int i = 1; i < n - ans;) { int last = -1,j; for(j = i + 1; j < n - ans; j++) { // cout<<res[i]<<" "<<res[j]<<endl; if(res[j] == res[i]) { last = j + 1; // cout<<last<<" ?"<<endl; } } if(last == -1) { i = i + 2; } else { // cout<<i<<" "<<last<<endl; sort(res + i,res + 1 + last); i = last + 1; } } for(int i = 1; i <= n; i++) { cout<<res[i]<<endl; } } } return 0; }
C++11(clang++ 3.9) 解法, 执行用时: 4ms, 内存消耗: 376K, 提交时间: 2020-07-04 17:36:51
#include<bits/stdc++.h> #define LL long long #define pb push_back #define pii pair<int, int> #define mp make_pair using namespace std; const int maxn = 1e2 + 10; vector<int> ans; int n, m, best, num[maxn]; struct Node{ int num, id; bool operator < (const Node &A) const { return num < A.num; } }; int check() { int ret = 0, id = 0; for (int x : ans) { if (!ret) ret = 1, id = x; else { if (x == id) ++ret; else --ret; } } priority_queue<Node> q; for (int i = 2; i <= m; ++i) if (num[i]) q.push((Node){ num[i], i }); while (!q.empty()) { auto u = q.top(); q.pop(); if (!ret) ret = 1, id = u.id; else { if (id == u.id) ++ret; else --ret; } if (u.num > 1) { --u.num; q.push(u); } } if (num[1] < ret) return 0; return num[1] - ret; } int main() { #ifdef DEBUG freopen("text.in", "r", stdin); #endif scanf("%d%d", &n, &m); for (int i = 1; i <= m; ++i) scanf("%d", &num[i]); best = check(); if (best <= 0) return puts("NO"), 0; puts("YES"); printf("%d\n", best); for (int i = 0; i < n; ++i) { ans.pb(0); for (int j = 1; j <= m; ++j) if (num[j]) { --num[j]; ans[i] = j; if (check() == best) break; ++num[j]; } } for (int x : ans) printf("%d\n", x); return 0; }