Description
In the newest census of Jersey Cows and Holstein Cows, Wisconsin cows have earned three stalls in the Barn of Representatives. The Jersey Cows currently control the state's redistricting committee. They want to partition the state into three equally sized voting districts such that the Jersey Cows are guaranteed to win elections in at least two of the districts.
Wisconsin has 3*K (1 <= K <= 60) cities of 1,000 cows, numbered 1..3*K, each with a known number (range: 0..1,000) of Jersey Cows. Find a way to partition the state into three districts, each with K cities, such that the Jersey Cows have the majority percentage in at least two of districts.
All supplied input datasets are solvable.
Wisconsin has 3*K (1 <= K <= 60) cities of 1,000 cows, numbered 1..3*K, each with a known number (range: 0..1,000) of Jersey Cows. Find a way to partition the state into three districts, each with K cities, such that the Jersey Cows have the majority percentage in at least two of districts.
All supplied input datasets are solvable.
Input
* Line 1: A single integer, K
* Lines 2..3*K+1: One integer per line, the number of cows in each city that are Jersey Cows. Line i+1 contains city i's cow census.
* Lines 2..3*K+1: One integer per line, the number of cows in each city that are Jersey Cows. Line i+1 contains city i's cow census.
Output
* Lines 1..K: K lines that are the city numbers in district one, one per line
* Lines K+1..2K: K lines that are the city numbers in district two, one per line
* Lines 2K+1..3K: K lines that are the city numbers in district three, one per line
* Lines K+1..2K: K lines that are the city numbers in district two, one per line
* Lines 2K+1..3K: K lines that are the city numbers in district three, one per line
Sample Input
2 510 500 500 670 400 310
Sample Output
1 2 3 6 5 4
/* 怎么随机。 初始依次划为1,2,3 用一个sum[4]数组来存和 用一个vis[]数组来标记是否是赫斯特奶牛胜利 */ #include <iostream> #include <cstdio> #include <cstring> #include <ctime> #include <algorithm> using namespace std; #define maxn 280 int max_sum; int sum[4]; bool Judge() { int ans = 0; if(sum[1] > max_sum) ans++; if(sum[2] > max_sum) ans++; if(sum[3] > max_sum) ans++; if(ans >= 2) return 1; return 0; } struct City { int id,key,fa; }city[280]; bool cmp(City a,City b) { return a.key > b.key; } int main() { //freopen("in.txt","r",stdin); srand((unsigned)time(NULL)); int k; while(scanf("%d",&k)!=EOF) { memset(sum,0,sizeof(sum)); max_sum = 500*k; for(int i = 1;i <= 3*k;i++) { scanf("%d",&city[i].key); city[i].id = i; } sort(city+1,city+3*k+1,cmp); for(int i = 1;i <= k;i++) { city[i].fa = 1;sum[1] += city[i].key; } for(int i = k+1;i <= 2*k;i++) { city[i].fa = 2;sum[2] += city[i].key; } for(int i = 2*k+1;i <= 3*k;i++) { city[i].fa = 3;sum[3] += city[i].key; } int pos1,pos2; while(!Judge()) { pos1 = rand()%(2*k)+1,pos2 = rand()%(2*k)+1; while(city[pos1].fa == city[pos2].fa) { pos2 = rand()%(2*k)+1; } int f1 = city[pos1].fa,f2 = city[pos2].fa; sum[f1] = sum[f1] - city[pos1].key + city[pos2].key; sum[f2] = sum[f2] - city[pos2].key + city[pos1].key; city[pos1].fa = f2,city[pos2].fa = f1; } for(int i = 1;i <= 3*k;i++) { if(city[i].fa == 1) printf("%d\n",city[i].id); } for(int i = 1;i <= 3*k;i++) { if(city[i].fa == 2) printf("%d\n",city[i].id); } for(int i = 1;i <= 3*k;i++) { if(city[i].fa == 3) printf("%d\n",city[i].id); } } return 0; }