列表

详情


NC24365. [USACO 2012 Dec G]Gangs of Instanbull

描述

Life is tough on the farm, and when life is tough you have to get tough. The cows have formed gangs (conveniently numbered 1 to M). The gangs coexisted in peace for a while, but now things are really getting out of control! 
The cows are competing over control of a great grazing field. This conflict happens over a series of minutes. Each minute a single cow walks into the field. If the field is empty the new cow's gang is considered to take control of the field. If the field is already controlled by the new cow's gang then the cow just starts grazing. Otherwise, a single cow from the controlling gang that is grazing confronts the new cow. 
These confrontations between two cows start with some arguing and inevitably end up with the pair coming to realize how much more more alike than different they are. The cows, seeing the error in their ways, leave the gang and the field and get a cold glass of soy milk at FJ's tavern. If after this confrontation the field is empty than no gang controls the field. Bessie realizes how these confrontations will occur. She knows how many cows are in each gang. 
 Bessie really wants her gang to control the field after the conflict is over and all cows are either on the field or at FJ's tavern. Help Bessie determine if it is possible for her gang, labeled 1, to control the field in the end. 
If it is possible, Bessie wants to know the maximum number of cows from her gang that could be on the field at the end. Output this number and the lexicographically earliest ordering of cows that results in this number of cows from Bessie's gang at the end. An ordering X is said to be lexicographically earlier than Y if there is some k such that X[k] < Y[k] and X[i] = Y[i] for i < k.

输入描述

* 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:
There are 5 cows and 3 gangs. Bessie's gang (gang 1) has 2 members, gang 2
has 1 member, and gang 3 has 2 members.

OUTPUT DETAILS:
Only one cow from Bessie's gang can end up on the field.

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

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;
}

上一题