2017 ACM Arabella Collegiate Programming Contest(solved 9/13, complex 12/13)

时间:2021-11-12 07:23:54

A.Sherlock Bones

题意: 给出长度为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-
# define MOD
# define INF
# 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<<,l,mid
# define rch p<<|,mid+,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=,f=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-') f=-; ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-''; ch=getchar();}
return x*f;
}
inline void Out(int a) {
if(a<) {putchar('-'); a=-a;}
if(a>=) Out(a/);
putchar(a%+'');
}
const int N=;
//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=;
LL ans=;
mem(num,);
FOR(i,,n-) {
if (s[i]=='') ++pos;
else ++num[pos];
}
for (int i=; i<=pos; i+=) sum1[i]=num[i]+(i>=?sum1[i-]:);
for (int i=; i<=pos; i+=) sum2[i]=num[i]+sum2[i-];
int top1=(pos&?pos:pos-), top2=(pos&?pos-:pos);
FOR(i,,pos-) {
if (i&) {
ans+=(LL)num[i]*(sum2[i+]-sum2[i-]);
ans+=(LL)(num[i]+)*(sum2[top2]-sum2[i+]+(top2-i-)/);
}
else {
ans+=(LL)num[i]*(sum1[i+]-sum1[i-]);
ans+=(LL)(num[i]+)*(sum1[top1]-sum1[i+]+(top1-i-)/);
}
}
printf("%lld\n",ans);
}
return ;
}

B.Unusual Team

水题。

C.Cheap Kangaroo

水题。

D.Magical Bamboos

题意:给出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-
# define MOD
# define INF
# 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<<,l,mid
# define rch p<<|,mid+,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=,f=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-') f=-; ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-''; ch=getchar();}
return x*f;
}
inline void Out(int a) {
if(a<) {putchar('-'); a=-a;}
if(a>=) Out(a/);
putchar(a%+'');
}
const int N=;
//Code begin... int a[N]; int main ()
{
int T=Scan();
while (T--) {
int n=Scan();
FOR(i,,n) a[i]=Scan();
int tmp=a[]&, flag=;
FOR(i,,n) if (a[i]%!=tmp) {flag=; break;}
puts(flag?"yes":"no");
}
return ;
}

E.Competitive Seagulls

题意:给出一串长度为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-
# define MOD
# define INF
# 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<<,l,mid
# define rch p<<|,mid+,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=,f=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-') f=-; ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-''; ch=getchar();}
return x*f;
}
inline void Out(int a) {
if(a<) {putchar('-'); a=-a;}
if(a>=) Out(a/);
putchar(a%+'');
}
const int N=;
//Code begin... int main ()
{
int T=Scan();
while (T--) {
int x=Scan();
puts(x!=&&x!=?"first":"second");
}
return ;
}

F.Monkeying Around

题意是有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 + ;
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, , sizeof has);
int n, m;
scanf("%d%d", &n, &m);
for (int i = ; i <= m; ++i) {
int pos, joke, dis;
scanf("%d%d%d", &pos, &joke, &dis);
one[i].L = max(, pos - dis), one[i].R = min(n, pos + dis), one[i].id = i;
two[i] = one[i];
idForJoke[i] = joke;
}
sort(one + , one + + m, cmp1);
sort(two + , two + + m, cmp2);
int ans = , p1 = , p2 = ;
for (int i = ; 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()]] > ;
} 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 ;
}

G.Snake Rana

2017 ACM Arabella Collegiate Programming Contest(solved 9/13, complex 12/13)

H.Mirrored String I

水题。

I.Mirrored String II

题意:求字符串出现给定字符的最长回文子串。

非给定字符在原字符串中分割出一些子串,对这些子串使用马拉车算法更新答案即可。

# 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-
# define MOD
# define INF
# 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<<,l,mid
# define rch p<<|,mid+,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=,f=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-') f=-; ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-''; ch=getchar();}
return x*f;
}
inline void Out(int a) {
if(a<) {putchar('-'); a=-a;}
if(a>=) Out(a/);
putchar(a%+'');
}
const int N=;
//Code begin... char Ma[N<<];
int Mp[N<<];
char a[]={'A', 'H', 'I', 'M', 'O', 'T', 'U', 'V','W','X','Y'}; void Manacher(char *s, int len){
int l=;
Ma[l++]='$'; Ma[l++]='#';
FOR(i,,len-) Ma[l++]=s[i], Ma[l++]='#';
Ma[l]=;
int mx=, id=;
FOR(i,,l-) {
Mp[i]=mx>i?min(Mp[*id-i],mx-i):;
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=, ans=;
FOR(i,,len-) {
bool mark=false;
FOR(j,,) if (s[i]==a[j]) {mark=true; break;}
if (!mark) {
if (now<=i-) {
Manacher(s+now,i-now);
FOR(k,,*(i-now)+) ans=max(ans,Mp[k]-);
}
now=i+;
}
}
if (now<=len-) {
Manacher(s+now,len-now);
FOR(k,,*(len-now)+) ans=max(ans,Mp[k]-);
}
printf("%d\n",ans);
}
return ;
}

J.Lazy Physics Cat

数学水题。

K.Owl Geeks

数学水题。

M.Make Cents?

水题。