poj2454
我不会告诉你我对我自己的想法笑了一下午的。。
#include <iostream>
#include <algorithm> #include <time.h> #include <string.h> using namespace std; typedef struct select { int id; int num; }sel; bool comp(sel a, sel b) { return a.num < b.num; } int main() { int k; while (cin >> k) { sel cow[200]; for (int i = 0; i < k * 3; i++) { cin >> cow[i].num; cow[i].id = i + 1; } sort(cow, cow + k * 3, comp); srand((unsigned) time(NULL)); while (1) { int i = rand() % (k) + k; int j = rand() % (k) + 2 * k; //cout << i << ' ' << j << endl; swap(cow[i], cow[j]); int temp1 = 0; int temp2 = 0; for (int a = k; a < 2 * k; a++) { temp1 += cow[a].num; } for (int a = 2 * k; a < 3 * k; a++) { temp2 += cow[a].num; } if ((temp1 > 500 * k) && (temp2 > 500 * k)) { for (int a = 0; a < k * 3; a++) { cout << cow[a].id << endl; } break; } } } } |
貌似升序更快?
#include <iostream>
#include <algorithm> #include <time.h> #include <string.h> using namespace std; typedef struct select { int id; int num; }sel; bool comp(sel a, sel b) { return a.num > b.num; } int main() { int k; while (cin >> k) { sel cow[200]; for (int i = 0; i < k * 3; i++) { cin >> cow[i].num; cow[i].id = i + 1; } sort(cow, cow + k * 3, comp); srand((unsigned) time(NULL)); while (1) { int i = rand() % (k); int j = rand() % (k) + k; //cout << i << ' ' << j << endl; swap(cow[i], cow[j]); int temp1 = 0; int temp2 = 0; for (int a = 0; a < k; a++) { temp1 += cow[a].num; } for (int a = k; a < 2 * k; a++) { temp2 += cow[a].num; } if ((temp1 > 500 * k) && (temp2 > 500 * k)) { for (int a = 0; a < k * 3; a++) { cout << cow[a].id << endl; } break; } } } } |