第十四屆華中科技大學程式設計競賽決賽同步賽題解【持續更新】
阿新 • • 發佈:2019-02-10
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<string>
#include<map>
#include<queue>
#include<vector>
using namespace std;
#define ll long long int
#define INF 0x3f3f3f3f
const int maxn = 2e5 + 10;
int f[maxn];
int sum[maxn];
int find(int x) {
if (x != f[x]) {
int tmp = f[x];
f[x] = find(f[x]);
sum[x] = (sum[x] ^ sum[tmp]);
}
return f[x];
}
int main()
{
int n, m;
while (~scanf("%d %d", &n, &m)) {
for (int i = 0; i <= n; i++) f[i] = i, sum[i] = 0;
int ans = 0 ; bool flag = 0;
for (int i = 1; i <= m; i++) {
int l, r, s;
scanf("%d %d %d", &l, &r, &s);
l--;
int ra = find(l); int rb = find(r);
if (ra == rb) {
if ((sum[l] ^ sum[r]) != s) {
flag = 1 ;
printf("%d\n", i);
}
}
else {
f[ra] = rb;
sum[ra] = (sum[r] ^ sum[l] ^ s);
}
}
if (!flag)
printf("-1\n");
}
return 0;
}
B Trees on Sale
1.題目的要求可以簡化成,任意多個人要同樣數量的樹苗,都可以滿足。
2.通過打表可以發現捆的數量存在規律!
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define LL long long
int main()
{
LL n, k;
while (~scanf("%lld %lld", &n, &k)) {
LL now = 1;
LL sum = 0;
LL aa = 0;
while (now <= k) {
LL xian = sum / now;
LL ans = n - xian;
sum += ans * now;
if (ans != 0) {
aa += ans;
}
now = sum / n + 1;
}
printf("%lld\n", aa);
}
return 0;
}
F Beautiful Land
超大0-1揹包問題,想辦法來模擬0-1揹包的過程,注意中間狀態需要不斷更新,不可保留!記憶體超限!
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<string>
#include<algorithm>
#include<queue>
using namespace std;
map<int, long long> mp;
int w;
int n;
long long sum;
typedef struct
{
int w, v;
}Value;
Value value[150];
priority_queue<int> p;
bool compare(Value v1, Value v2)
{
return v1.w > v2.w;
}
int main()
{
int T;
scanf("%d", &T);
while (T--) {
scanf("%d %d", &n, &w);
for (int i = 1; i <= n; i++)
{
scanf("%d %d", &value[i].w, &value[i].v);
}
mp[0] = 0;
sum = 0;
sort(value + 1, value + 1 + n, compare);
for (int i = 1; i <= n; i++)
{
if (value[i].v == 0 || value[i].w > w) continue;
long long maxx = 0;
for (map<int, long long>::iterator it = mp.begin(); it != mp.end(); ++it)
{
int d = it->first;
if ((long long)d + (long long)value[i].w <= (long long)w && maxx <= it->second + value[i].v) { p.push(d + value[i].w); }
maxx = max(maxx, it->second + value[i].v);
}
while (!p.empty())
{
int d = p.top(); p.pop();
mp[d] = max(mp[d], mp[d - value[i].w] + value[i].v);
if (sum < mp[d]) { sum = mp[d]; }
}
}
mp.clear();
printf("%lld\n", sum);
}
return 0;
}
G Cards Game
博弈問題:
有N堆牌,每張牌都有自己的權值,A每次可以拿任意一堆的堆頂,B每次可以拿任意一堆的堆底。如果我在某一堆的半堆中有很好的牌,我必不可能被其拿去,若有不好的牌也必不可能被對方拿去,反之亦然。那麼決策的關鍵就來了,對於偶數堆,每個人拿到的牌就可以固定了,但是對於奇數堆的中間元素就有了決策的可能。也就意味著,A和B從大到小排著拿了。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
int a[105][105];
int c[105];
int x[105];
bool cmp(int a, int b) {
return a > b;
}
int main()
{
int n; int m; int tmp1 = 0, tmp2 = 0;
scanf("%d", &n); int cnt = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", &m);
for (int i = 1; i <= m; i++) {
scanf("%d", &x[i]);
}
if (m % 2) {
for (int i = 1; i <= m / 2; i++) {
tmp1 += x[i];
}
for (int i = (m + 1) / 2 + 1; i <= m; i++){
tmp2 += x[i];
}
c[++cnt] = x[(m + 1) / 2];
}
else {
for (int i = 1; i <= m / 2; i++) {
tmp1 += x[i];
}
for (int i = m / 2 + 1; i <= m; i++) {
tmp2 += x[i];
}
}
}
sort(c + 1, c + 1 + cnt, cmp);
for (int i = 1; i <= n; i++) {
if (i % 2) tmp1 += c[i];
else tmp2 += c[i];
}
printf("%d %d\n", tmp1, tmp2);
return 0;
}