Pagini recente » Cod sursa (job #1528623) | Cod sursa (job #2192800) | Cod sursa (job #1407552) | Monitorul de evaluare | Cod sursa (job #1715880)
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <vector>
using namespace std;
const int NMAX = 3005;
const int KMAX = 10;
const int STEPS_PER_TEST = 5000;
vector <int> towns[KMAX];
int sums[NMAX];
int main()
{
srand(time(NULL));
ifstream cin("sate2.in");
ofstream cout("sate2.out");
int t = 0;
cin >> t;
while (t --) {
int n, m, k;
cin >> n >> m >> k;
for (int i = 0; i < k; ++ i) {
towns[i].clear();
sums[i] = 0;
}
m /= k;
int val, sum = 0;
for (int i = 1; i <= n; ++ i) {
cin >> val;
sum += val;
int where = rand() % k;
towns[where].push_back(val);
sums[where] += val;
}
if (sum != k * m) {
cout << "NU\n";
continue;
}
vector <int> smalls;
vector <int> bigs;
int i;
for (i = 0; i < STEPS_PER_TEST; ++ i) {
smalls.clear();
bigs.clear();
bool ok = true;
for (int j = 0; j < k; ++ j)
if (sums[j] < m) {
ok = false;
smalls.push_back(j);
}
else if (sums[j] > m) {
ok = false;
bigs.push_back(j);
}
else if (rand() & 1)
smalls.push_back(j);
else
bigs.push_back(j);
if (ok)
break;
int s = smalls[rand() % smalls.size()];
int b = bigs[rand() % bigs.size()];
int pos = rand() % towns[b].size();
val = towns[b][pos];
swap(towns[b][pos], towns[b].back());
towns[b].pop_back();
sums[b] -= val;
towns[s].push_back(val);
sums[s] += val;
}
if (i == STEPS_PER_TEST)
cout << "NU\n";
else
cout << "DA\n";
}
cin.close();
cout.close();
return 0;
}