A. String Similarity
题目原文
题目大意
定义两个字符串相似:两个字符串中至少有一个位置的值相同。
现在给出一个长度为 2 n − 1 2n-1 2n−1的字符串 s s s,现在让你构造一个长度为 n n n的字符串,使得这个字符串与 s s s的所有长度为 n n n的子串都相似。
解题思路
可以发现, s s s的所有长度为 n n n子串都有一个公共位置 s [ n ] s[n] s[n]
解法
直接输出 n n n个 s [ n ] s[n] s[n]就行,这个答案一定与所有子串都相似。
代码
/*************************************************
* Copyright: lemonaaaaa *
* Author: lemonaaaaa *
* Date: 2020-8-25 *
* Description: Edu Codeforces Round 94 *
**************************************************/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=200005;
int T,n;
char str[maxn];
int main()
{
#ifdef lemon
freopen("A.txt","r",stdin);
#endif
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
scanf("%s",str+1);
for(int i=1;i<=n;i++) printf("%c",str[n]);
printf("\n");
}
return 0;
}
B. RPG Protagonist
题目原文
题目大意
现在有两个人,一个人可以带 p p p单位的重量,另一个人可以带 f f f单位的重量。现在有两种物品,给出两种物品单件的重量以及两种物品的数量,求这两个人最多一共带走多少物品(数量最多)。
解题思路
可以发现,我们一定是采取贪心的策略。
我们先考虑一个人,那一定是先尽可能地选重量小的装,这样一定能够装更多数量。
那其实我们只需要求出第一个人选剩下的数量,第二个人能装的数量可以直接O(1)算出来。
解法
直接枚举第一个人选了多少个第一种物品,然后我们根据贪心策略,我们一定是让第一个人尽可能地装满,然后这个时候我们就得到了两种物品剩下的数量,这个时候就按照前面说的直接先选小的的策略进行选择即可。
代码
/*************************************************
* Copyright: lemonaaaaa *
* Author: lemonaaaaa *
* Date: 2020-8-25 *
* Description: Edu Codeforces Round 94 *
**************************************************/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=200005;
int T;
long long p,f,cnts,cntw,s,w,ans;
int main()
{
#ifdef lemon
freopen("B.txt","r",stdin);
#endif
scanf("%d",&T);
while(T--)
{
ans=0;
scanf("%lld%lld",&p,&f);
scanf("%lld%lld",&cnts,&cntw);
scanf("%lld%lld",&s,&w);
for(long long i=0;i<=cnts;i++)
{
if(i*s>p) break;
long long resp=p-i*s;
long long t2=resp/w;
if(t2>cntw) t2=cntw;
long long t1=i+t2;
long long t3=cntw-t2;
if(s<=w)
{
long long t4=f/s;
if(t4>cnts-i) t4=cnts-i;
long long temp=f-t4*s;
long long t6=temp/w;
if(t6>t3) t6=t3;
long long t5=t4+t6;
ans=max(ans,t1+t5);
}
else
{
long long t4=f/w;
if(t4>t3) t4=t3;
long long temp=f-t4*w;
long long t6=temp/s;
if(t6>cnts-i) t6=cnts-i;
long long t5=t4+t6;
ans=max(ans,t1+t5);
}
}
printf("%lld\n",ans);
}
return 0;
}
C. Binary String Reconstruction
题目原文
题目大意
对于一个原串 s s s,现在通过以下规则构建串 t t t。
对于一个位置 i i i,如果 s [ i − x ] s[i-x] s[i−x]存在并且 s [ i − x ] = 1 s[i-x]=1 s[i−x]=1或者 s [ i + x ] s[i+x] s[i+x]存在并且 s [ i + x ] = 1 s[i+x]=1 s[i+x]=1,那么 t [ i ] t[i] t[i]就为1,否则 t [ i ] t[i] t[i]为0.
现在给出串 t t t,让你还原原串 s s s.
解题思路
直接从前往后依次贪心还原即可,如果有冲突的就无解。注意 0 0 0是要求 s s s前后两个位置都为 0 0 0.
解法
如上。
代码
/*************************************************
* Copyright: lemonaaaaa *
* Author: lemonaaaaa *
* Date: 2020-8-25 *
* Description: Edu Codeforces Round 94 *
**************************************************/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=200005;
int T,x;
char str[maxn];
int ans[maxn];
int main()
{
#ifdef lemon
freopen("C.txt","r",stdin);
#endif
scanf("%d",&T);
while(T--)
{
scanf("%s%d",str+1,&x);
int n=strlen(str+1);
bool flag=false;
for(int i=1;i<=n;i++) ans[i]=-1;
for(int i=1;i<=n;i++)
{
if(str[i]=='1')
{
bool f=false;
if(i-x>=1)
{
if(ans[i-x]==1) f=true;
else if(ans[i-x]==-1) ans[i-x]=1,f=true;
}
if(!f&&i+x<=n)
{
if(ans[i+x]==1) f=true;
else if(ans[i+x]==-1) ans[i+x]=1,f=true;
}
if(!f) flag=true;
}
else
{
bool f=true;
if(i-x>=1)
{
if(ans[i-x]==-1) ans[i-x]=0;
if(ans[i-x]==1) f=false;
}
if(i+x<=n)
{
if(ans[i+x]==-1) ans[i+x]=0;
if(ans[i+x]==1) f=false;
}
if(!f) flag=true;
}
}
for(int i=1;i<=n;i++) if(ans[i]==-1)
{
int f=1;
if(i-x>=1&&str[i-x]=='0') f=0;
if(i+x<=n&&str[i+x]=='0') f=0;
ans[i]=f;
if(f==0)
{
if(i-x>=1&&str[i-x]=='1') flag=true;
if(i+x<=n&&str[i+x]=='1') flag=true;
}
if(f==1)
{
if(i-x>=1&&str[i-x]=='0') flag=true;;
if(i+x<=n&&str[i+x]=='0') flag=true;
}
}
if(flag) {printf("-1\n");continue;}
for(int i=1;i<=n;i++) printf("%d",ans[i]);printf("\n");
}
return 0;
}
D. Zigzags
题目原文
题目大意
给出一个数组,求满足以下条件的四元组 ( i , j , k , l ) (i,j,k,l) (i,j,k,l)的个数。
1 ⩽ i < j < k < l ⩽ n a i = a k and a j = a l 1 \leqslant i < j < k < l \leqslant n \\ a_i = a_k \; \text{and} \; a_j=a_l 1⩽i<j<k<l⩽nai=akandaj=al
解题思路
先前缀和求出各数字出现的次数,再枚举 j j j和 k k k的位置,这时两个数字都是确定的,然后直接 O ( 1 ) O(1) O(1)算出 j j j前 a [ k ] a[k] a[k]的数量与 k k k后 a [ j ] a[j] a[j]的数量,相乘累加答案即可。
解法
如上。
代码
/*************************************************
* Copyright: lemonaaaaa *
* Author: lemonaaaaa *
* Date: 2020-8-26 *
* Description: Edu Codeforces Round 94 *
**************************************************/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=3005;
int T,n,a[maxn],cnt[maxn][maxn];
int main()
{
#ifdef lemon
freopen("D.txt","r",stdin);
#endif
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
for(int j=1;j<=n;j++)
{
cnt[i][j]=cnt[i-1][j];
}
cnt[i][a[i]]++;
}
long long ans=0;
for(int i=1;i<=n;i++)
{
for(int j=i+1;j<=n;j++)
{
ans+=(long long)cnt[i-1][a[j]]*(long long)(cnt[n][a[i]]-cnt[j][a[i]]);
}
}
printf("%lld\n",ans);
}
return 0;
}