2017 ACM Arabella Collegiate Programming Contest(solved 9/13, complex 12/13)
题意: 给出长度为n的01串,问f(i,j)=f(j,k),(i<j<k)的i,j,k取值种数。其中f(i,j)表示[i,j]内1的个数, 且s[j]必须为1。
先把串看出是一串1两两之间穿插若干个0的联通块,不妨设block[i]为联通块i里面0的个数。
先考虑i,k处都为0的情况。
枚举i在哪个联通块里面。再枚举j,由于对称性,此时k在block[i+1],block[i+3],block[i+5]。。。内,那么此时方法数为block[i]*(block[i+1]+block[i+3]+...),前缀和预处理优化一下即可。
i,k处其中至少一个为1的情况同理。
时间复杂度O(n).
# include <cstdio> # include <cstring> # include <cstdlib> # include <iostream> # include <vector> # include <queue> # include <stack> # include <map> # include <bitset> # include <set> # include <cmath> # include <algorithm> using namespace std; # define lowbit(x) ((x)&(-x)) # define pi acos(-1.0) # define eps 1e-8 # define MOD 1000000007 # define INF 1000000000 # define mem(a,b) memset(a,b,sizeof(a)) # define FOR(i,a,n) for(int i=a; i<=n; ++i) # define FDR(i,a,n) for(int i=a; i>=n; --i) # define bug puts("H"); # define lch p<<1,l,mid # define rch p<<1|1,mid+1,r # define mp make_pair # define pb push_back typedef pair<int,int> PII; typedef vector<int> VI; # pragma comment(linker, "/STACK:1024000000,1024000000") typedef long long LL; inline int Scan() { int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();} return x*f; } inline void Out(int a) { if(a<0) {putchar('-'); a=-a;} if(a>=10) Out(a/10); putchar(a%10+'0'); } const int N=200005; //Code begin... char s[N]; int num[N], sum1[N], sum2[N]; int main () { int T, n; scanf("%d",&T); while (T--) { scanf("%d%s",&n,s); int pos=1; LL ans=0; mem(num,0); FOR(i,0,n-1) { if (s[i]=='1') ++pos; else ++num[pos]; } for (int i=1; i<=pos; i+=2) sum1[i]=num[i]+(i>=2?sum1[i-2]:0); for (int i=2; i<=pos; i+=2) sum2[i]=num[i]+sum2[i-2]; int top1=(pos&1?pos:pos-1), top2=(pos&1?pos-1:pos); FOR(i,1,pos-1) { if (i&1) { ans+=(LL)num[i]*(sum2[i+1]-sum2[i-1]); ans+=(LL)(num[i]+1)*(sum2[top2]-sum2[i+1]+(top2-i-1)/2); } else { ans+=(LL)num[i]*(sum1[i+1]-sum1[i-1]); ans+=(LL)(num[i]+1)*(sum1[top1]-sum1[i+1]+(top1-i-1)/2); } } printf("%lld\n",ans); } return 0; }
水题。
水题。
题意:给出n个数,可以给其中某个数减1,其他的数加1,问是否可能最后全相等。
操作实际上就是给其中某个数减2,因此判断他们的奇偶性即可。
# include <cstdio> # include <cstring> # include <cstdlib> # include <iostream> # include <vector> # include <queue> # include <stack> # include <map> # include <bitset> # include <set> # include <cmath> # include <algorithm> using namespace std; # define lowbit(x) ((x)&(-x)) # define pi acos(-1.0) # define eps 1e-8 # define MOD 1000000007 # define INF 1000000000 # define mem(a,b) memset(a,b,sizeof(a)) # define FOR(i,a,n) for(int i=a; i<=n; ++i) # define FDR(i,a,n) for(int i=a; i>=n; --i) # define bug puts("H"); # define lch p<<1,l,mid # define rch p<<1|1,mid+1,r # define mp make_pair # define pb push_back typedef pair<int,int> PII; typedef vector<int> VI; # pragma comment(linker, "/STACK:1024000000,1024000000") typedef long long LL; inline int Scan() { int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();} return x*f; } inline void Out(int a) { if(a<0) {putchar('-'); a=-a;} if(a>=10) Out(a/10); putchar(a%10+'0'); } const int N=100005; //Code begin... int a[N]; int main () { int T=Scan(); while (T--) { int n=Scan(); FOR(i,1,n) a[i]=Scan(); int tmp=a[1]&1, flag=1; FOR(i,2,n) if (a[i]%2!=tmp) {flag=0; break;} puts(flag?"yes":"no"); } return 0; }
题意:给出一串长度为n的白棋子,两位操作者轮流将p<=ceil(n/2)的连续棋子涂黑,其中p为质数,若不存在这样的p,则p=1,无法操作者输,问谁能赢?
结论:n=2或者n=3,则第二位赢,否则第一位赢。
可以构造出来,当n!=2并且n!=3时,如果n为奇数,第一位涂最中间的3颗棋子,之后无论第二位怎么涂,只需涂第二位涂的对称位置即可。
如果n为偶数,第一位涂最中间的2颗棋子,其余同上。
# include <cstdio> # include <cstring> # include <cstdlib> # include <iostream> # include <vector> # include <queue> # include <stack> # include <map> # include <bitset> # include <set> # include <cmath> # include <algorithm> using namespace std; # define lowbit(x) ((x)&(-x)) # define pi acos(-1.0) # define eps 1e-8 # define MOD 1000000007 # define INF 1000000000 # define mem(a,b) memset(a,b,sizeof(a)) # define FOR(i,a,n) for(int i=a; i<=n; ++i) # define FDR(i,a,n) for(int i=a; i>=n; --i) # define bug puts("H"); # define lch p<<1,l,mid # define rch p<<1|1,mid+1,r # define mp make_pair # define pb push_back typedef pair<int,int> PII; typedef vector<int> VI; # pragma comment(linker, "/STACK:1024000000,1024000000") typedef long long LL; inline int Scan() { int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();} return x*f; } inline void Out(int a) { if(a<0) {putchar('-'); a=-a;} if(a>=10) Out(a/10); putchar(a%10+'0'); } const int N=1005; //Code begin... int main () { int T=Scan(); while (T--) { int x=Scan(); puts(x!=2&&x!=3?"first":"second"); } return 0; }
题意是有N个猴子在座位上讲笑话,每个猴子讲的笑话会有种类和范围,当一个猴子听到没有听过的笑话的时候,它会从座位上离开,掉地上;当听到已经听过的笑话的时候,它会从地上回到座位。现在给出各个讲笑话的情况,问最终猴子有多少在座位上。
把这个转换成区间问题,按照所有区间的左端点排序,右端点排序。枚举每一个猴子,维护当前影响该点的笑话,如果最后的笑话出现2次以上,那么该猴子在座位上。
#include <bits/stdc++.h> #define IOS ios::sync_with_stdio(false) using namespace std; #define inf (0x3f3f3f3f) typedef long long int LL; const int maxn = 2e5 + 20; struct Node { int L, R, id; }one[maxn], two[maxn]; bool cmp1(struct Node a, struct Node b) { return a.L < b.L; } bool cmp2(struct Node a, struct Node b) { return a.R < b.R; } int idForJoke[maxn]; int has[maxn]; set<int>ss; void work() { ss.clear(); memset(has, 0, sizeof has); int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= m; ++i) { int pos, joke, dis; scanf("%d%d%d", &pos, &joke, &dis); one[i].L = max(1, pos - dis), one[i].R = min(n, pos + dis), one[i].id = i; two[i] = one[i]; idForJoke[i] = joke; } sort(one + 1, one + 1 + m, cmp1); sort(two + 1, two + 1 + m, cmp2); int ans = 0, p1 = 1, p2 = 1; for (int i = 1; i <= n; ++i) { while (p1 <= m && i >= one[p1].L) { ss.insert(one[p1].id); has[idForJoke[one[p1].id]]++; ++p1; } while (p2 <= m && i > two[p2].R) { ss.erase(two[p2].id); has[idForJoke[two[p2].id]]--; ++p2; } if (ss.size()) { ans += has[idForJoke[*ss.rbegin()]] > 1; } else ans++; } printf("%d\n", ans); } int main() { #ifdef local freopen("data.txt", "r", stdin); // freopen("data.txt", "w", stdout); #endif int t; scanf("%d", &t); while (t--) work(); return 0; }
水题。
题意:求字符串出现给定字符的最长回文子串。
非给定字符在原字符串中分割出一些子串,对这些子串使用马拉车算法更新答案即可。
# include <cstdio> # include <cstring> # include <cstdlib> # include <iostream> # include <vector> # include <queue> # include <stack> # include <map> # include <bitset> # include <set> # include <cmath> # include <algorithm> using namespace std; # define lowbit(x) ((x)&(-x)) # define pi acos(-1.0) # define eps 1e-8 # define MOD 1000000007 # define INF 1000000000 # define mem(a,b) memset(a,b,sizeof(a)) # define FOR(i,a,n) for(int i=a; i<=n; ++i) # define FDR(i,a,n) for(int i=a; i>=n; --i) # define bug puts("H"); # define lch p<<1,l,mid # define rch p<<1|1,mid+1,r # define mp make_pair # define pb push_back typedef pair<int,int> PII; typedef vector<int> VI; # pragma comment(linker, "/STACK:1024000000,1024000000") typedef long long LL; inline int Scan() { int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();} return x*f; } inline void Out(int a) { if(a<0) {putchar('-'); a=-a;} if(a>=10) Out(a/10); putchar(a%10+'0'); } const int N=1005; //Code begin... char Ma[N<<1]; int Mp[N<<1]; char a[15]={'A', 'H', 'I', 'M', 'O', 'T', 'U', 'V','W','X','Y'}; void Manacher(char *s, int len){ int l=0; Ma[l++]='$'; Ma[l++]='#'; FOR(i,0,len-1) Ma[l++]=s[i], Ma[l++]='#'; Ma[l]=0; int mx=0, id=0; FOR(i,0,l-1) { Mp[i]=mx>i?min(Mp[2*id-i],mx-i):1; while (Ma[i+Mp[i]]==Ma[i-Mp[i]]) Mp[i]++; if (i+Mp[i]>mx) mx=i+Mp[i], id=i; } } char s[N]; int main () { int T=Scan(); while (T--) { scanf("%s",s); int len=strlen(s); int now=0, ans=0; FOR(i,0,len-1) { bool mark=false; FOR(j,0,10) if (s[i]==a[j]) {mark=true; break;} if (!mark) { if (now<=i-1) { Manacher(s+now,i-now); FOR(k,0,2*(i-now)+1) ans=max(ans,Mp[k]-1); } now=i+1; } } if (now<=len-1) { Manacher(s+now,len-now); FOR(k,0,2*(len-now)+1) ans=max(ans,Mp[k]-1); } printf("%d\n",ans); } return 0; }
数学水题。
数学水题。
水题。