Codeforces Round #626 (Div. 2, based on Moscow Open Olympiad in Informatics)
前言
比赛AC
A. Even Subset Sum Problem
简明题意
- 给定长度为n的数组a。需要你输出a的一个子序列,使这个子序列的和是偶数
正文
- 判断a中奇数和偶数的个数,如果奇数数量>=2,则任意输出两个奇数。如果偶数数量>=1,则任意输出一个偶数。否则输出-1
代码
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<stack>
#include<map>
#include<queue>
#include<string>
#include<vector>
using namespace std;
const int maxn = 1e5 + 10;
int a[maxn];
int b[maxn];
void solve()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
int ji = 0, ou = 0;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
if (a[i] % 2 == 0) ou++, b[i] = 1;
else ji++, b[i] = 0;
}
if (ou >= 1 || ji >= 2)
{
if (ou >= 1)
{
cout << 1 << endl;
for (int i = 1; i <= n; i++)
if (b[i] == 1)
{
cout << i << endl;
break;
}
}else
{
cout << 2 << endl;
int cnt = 0;
for (int i = 1; i <= n; i++)
if (b[i] == 0)
{
cout << i << " ";
cnt++;
if (cnt == 2)
break;
}
}
}
else
{
cout << -1 << endl;
}
}
}
int main()
{
//freopen("Testin.txt", "r", stdin);
solve();
return 0;
}
B. Count Subrectangles
简明题意
- 给长度为n的01串a和长度为m的01串b,现在有一个n*m的矩阵,矩阵中的每一个元素g[i][j]=a[i]*b[j]。现在假设g矩阵计算出来了,询问面积为k的全1矩形有多少少个(这个矩形里每个元素必须是1)
正文
- 假设a[3]=1,那么,如果b[2]=1,b[3]=1,我们就会发现有一个12的全1矩形.如果a[2]=a[3]=1,b[3]=b[4]=b[5]=1,那么就是23的全1矩形.
- 所以我们可以枚举a中的连续1的数量,乘以b中连续1的数量,就是答案。
- 所以对k质因数分解,假设质因子是p,那么一条边长为p,另一条为k/p,我们只需要在a中寻找连续长度为p的数量,乘以b中连续长度为k/p的数量,累乘起来就行。
- 问题在于怎么计算数组中连续某个长度的数量。这个可以把数组扫一遍,找到每一段连续的1的长度。比如我找到了一段连续5个1,假设数组rec[i]记录连续i个1的数量,那么rec[1]+=5,rec[2]+=4,rec[3]+=3,rec[4]+=2,rec[5]+=1.这样好像并不是很好统计。我当时是这样想的,假设当前已经5个1了,再增加一个1,你会发现,rec[1],rec[2],rec[3],rec[4],rec[5],rec[6]都会增加1,所以这相当于区间加和,用差分维护一下就可以了
- 要注意最后质因数分解k的时候,假设k很大而n,m很小,那么p可能同时大于nm,这时候你去计算rec[p]就会导致re。所以每次质因数分解了,要特别判断一下p、k/p的合法性。
代码
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<stack>
#include<map>
#include<queue>
#include<string>
#include<vector>
using namespace std;
const int maxn = 1e5 + 10;
int cf[40000 + 10];
int rec_aa[40000 + 10];
int rec_bb[40000 + 10];
void solve()
{
int n, m, k;
cin >> n >> m >> k;
int cur = 0;
for (int i = 1; i <= n; i++)
{
int t; scanf("%d", &t);
if (t == 1)
{
cur++;
cf[cur + 1]--;
cf[1]++;
}
else if (t == 0)
{
cur = 0;
}
}
for (int i = 1; i <= n; i++)
rec_aa[i] = rec_aa[i - 1] + cf[i];
memset(cf, 0, sizeof cf);
cur = 0;
for (int i = 1; i <= m; i++)
{
int t; scanf("%d", &t);
if (t == 1)
{
cur++;
cf[cur + 1]--;
cf[1]++;
}
else if (t == 0)
{
cur = 0;
}
}
for (int i = 1; i <= m; i++)
rec_bb[i] = rec_bb[i - 1] + cf[i];
long long ans = 0;
for (int i = 1; i * i <= k; i++)
if (k % i == 0)
{
if (i <= n && k / i <= m)
ans += rec_aa[i] * rec_bb[k / i];
if (i *i != k && k / i <= n && i <= m)
ans += rec_aa[k / i] * rec_bb[i];
}
cout << ans;
}
int main()
{
//freopen("Testin.txt", "r", stdin);
solve();
return 0;
}
C. Unusual Competitions
简明题意
- 给定n长的括号序列,每次能将一串长度为l的连续子串重排,代价为l。现在想要把括号序列变得合法,问最小的代价。
正文
- 我的思路是,把(当成1,)当成-1,然后顺序考虑这个字符串。
- 每次从遇到的第一个-1开始,假设这个位置为x,直到累加后和为0,假设这个位置为y,那么,y-x+1就是这一次的消耗。直到遇到下一个-1.
- 这样做为啥是对的呢?其实我也不太懂,反正当时就搞出来这个想法,没想到就A了。。。
代码
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<stack>
#include<map>
#include<queue>
#include<cstdio>
#include<string>
using namespace std;
const int maxn = 1e6 + 10;
char a[maxn];
void solve()
{
int n; cin >> n;
scanf("%s", a + 1);
int ans = 0, cur = 0, st = -1;
for (int i = 1; i <= n; i++)
{
if (a[i] == '(') cur++;
else if (a[i] == ')') cur--;
if (cur == 0 && st != -1) ans += i - st + 1, st = -1;
if (cur < 0 && st == -1) st = i;
}
cout << (cur == 0 ? ans : -1);
}
int main()
{
//freopen("Testin.txt", "r", stdin);
solve();
return 0;
}
赛后补题
D. Present
简明题意
- 给定长度为n(2<=n<=4e5)的数组a,求两两之和的异或值。
正文
- 考虑到异或项最大是2e7,那么结果肯定是<= 2 [ l o g 2 ( 2 e 7 ) ] 2^{[log_2(2e7)]} 2[log2(2e7)](向上取整),算出来最多25个二进制项。那么我们直接计算答案转换为二进制的每一项,最后合并成10进制的答案就行了。
- 那么问题来了,如何知道答案的第i位是0还是1呢?我们可以直接找a数组中两项和的i位是1的项有多少个。奇数个,答案的第i位是1,否则是0.
- 现在问题就是如何在a数组中寻找有多少个两项之和的第i位为1。
- 假设现在在计算答案的第k位。我们可以直接枚举a数组的每一项 a i a_i ai,然后再到 a i a_i ai后面寻找 a j a_j aj使得 a i + a j a_i+a_j ai+aj的第k位是1.这个寻找我们可以想到二分查找。现在我们需要把k位以上的都先抹除掉,不然不好找。
- 假设我们在考虑k=4,那么 a i 和 a j a_i和a_j ai和aj的范围都在 [ 0 , 11111 ] [0,11111] [0,11111],所以 a i + a j a_i+a_j ai+aj的范围: [ 0 , 11111 + 11111 ] = [ 0 , 111110 ] [0,11111+11111]=[0,111110] [0,11111+11111]=[0,111110],现在需要他俩的和第k位是1,那么符合要求的 a i + a j a_i+a_j ai+aj范围就成了: [ 10000 , 11111 ] ∪ [ 110000 , 111110 ] [10000,11111]\cup[110000,111110] [10000,11111]∪[110000,111110]。现在把 a i + a j a_i+a_j ai+aj的范围用含k的式子表示,那么就是: [ 2 k , 2 k + 1 − 1 ] ∪ [ 2 k + 1 + 2 k , 2 k + 2 − 2 ] [2^k,2^{k+1}-1]\cup[2^{k+1}+2^k,2^{k+2}-2] [2k,2k+1−1]∪[2k+1+2k,2k+2−2]
- 所以现在可以直接枚举a[i]的每一项,然后二分查找在刚刚求出的那个区间的数有多少个,如果是奇数个,那么第k位是1,偶数个则是0.
代码
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<stack>
#include<map>
#include<queue>
#include<cstdio>
#include<map>
#include<string>
using namespace std;
const int maxn = 4e5 + 10;
int a[maxn], b[maxn];
void solve()
{
int n; cin >> n;
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
int ans = 0;
for (int k = 0; k <= 26; k++)
{
for (int i = 1; i <= n; i++)
b[i] = a[i] % (1 << (k + 1));
sort(b + 1, b + 1 + n);
long long cnt = 0;
for (int i = 1; i <= n; i++)
{
int p1 = lower_bound(b + 1 + i, b + 1 + n, (1 << k) - b[i]) - b;
int p2 = upper_bound(b + 1 + i, b + 1 + n, (1 << (k + 1)) - 1 - b[i]) - b - 1;
cnt += 1ll * p2 - p1 + 1;
p1 = lower_bound(b + 1 + i, b + 1 + n, (1 << (k + 1)) + (1 << k) - b[i]) - b;
p2 = upper_bound(b + 1 + i, b + 1 + n, (1 << (k + 2)) - 2 - b[i]) - b - 1;
cnt += 1ll * p2 - p1 + 1;
}
ans += (cnt & 1) * (1 << k);
}
cout << ans;
}
int main()
{
//freopen("testin.txt", "r", stdin);
solve();
return 0;
}
E. Instant Noodles
简明题意
- 有一个二分图,左右的顶点数都是n。再给出m条边。右端的每个点有一个权值a[i]
- 现在定义s是左端点的一个子集,这个集合s的值f(s),定义为二分图右端所有与s有连边的点的权值和。
- 现在求所有的f(s)互相的gcd
正文
- 看到题目的问题,子集都gcd起来,这样我们应该联想到gcd的一个性质,gcd(a,b,c)=gcd(a,b,c,a+b,a+c,b+c,a+b+c)。这时候,我们把a,b,c称为最小单位,也就是无论gcd式子里是多少个最小单位的和,其结果都是最小单位的gcd。因此只需要考虑最小单位的gcd
- 也就是说,比如左点是{1,2,3},那么所形成的所有集合有{1},{2},{3},{1,2},{1,3},{2,3},{1,2,3}。(空集由于题意不计)。所以把这个对应到上面的gcd式子,我们可以发现是不是直接计算{1},{2},{3}的gcd就可以了呢?因为{1},{2}这样的集合是最小单位。
- 差不多是这个意思,但是这样不对。因为这个s到f(s)是有两层映射的。也就是说,f({1})+f({2})!=f({1,2}),因为,假设1号节点映射到了右端的1,2节点,而2号节点映射到1,3节点,所以 f ( 1 ) = c 1 + c 2 , f ( 2 ) = c 1 + c 3 f(1)=c_1+c_2,f(2)=c_1+c_3 f(1)=c1+c2,f(2)=c1+c3而 f ( 1 , 2 ) = c 1 + c 2 + c 3 ! = f ( 1 ) + f ( 2 ) = c 1 + 2 c 2 + c 3 f(1,2)=c_1+c_2+c_3 !=f(1)+f(2)=c_1+2c_2+c_3 f(1,2)=c1+c2+c3!=f(1)+f(2)=c1+2c2+c3。所以以{1},{2}…这样的集合为最小单位是不对的。
- 考虑到每一个f(s)都是右端一些点的组合。那么我们是不是可以直接以右端的每一个点为最小单位呢?这样看起来很合理,但实际上也不对。看这样一个例子,共有123三个点,1映射到12,2映射到12,3映射到1。这时我们发现N(s)中根本没有出现1,2这样单独的点,所以直接以右端点为基本单位也是不对的。但此时,我们可以发现,直接以1,2整体作为基本单位,就是对的了。所以最终就是,所有的在右端的,具有相同边的点是基本单位,把这些点的权值和gcd起来就是答案。
- 总结一下,以后遇到很多数的gcd,我们可以考虑找到gcd的基本单位。
代码
#pragma GCC optimize(2)
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<stack>
#include<map>
#include<queue>
#include<cstdio>
#include<map>
#include<string>
using namespace std;
const int maxn = 5e5 + 10;
long long gcd(long long a, long long b)
{
if (b == 0) return a;
return gcd(b, a % b);
}
long long a[maxn];
vector<int> g[maxn];
map<vector<int>, long long> rec;
void solve()
{
int t;
cin >> t;
while (t--)
{
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
scanf("%lld", &a[i]), g[i].clear();
rec.clear();
while (m--)
{
int u, v;
scanf("%d%d", &u, &v);
g[v].push_back(u);
}
for (int i = 1; i <= n; i++)
{
if (g[i].size())
{
sort(g[i].begin(), g[i].end());
rec[g[i]] += a[i];
}
}
long long ans = -1;
for (auto& it : rec)
if (ans == -1) ans = it.second;
else ans = gcd(ans, it.second);
printf("%lld\n", ans);
}
}
int main()
{
//freopen("Testin.txt", "r", stdin);
solve();
return 0;
}