Educational Codeforces Round 94 (Rated for Div. 2) 赛后总结及题解
题意:定义若两个长度相同的01串中,存在某一个位置,使得两字符串在该位置的字符相同(a[i]=b[i]),则称这两个字符串相似。现在给一个长度为2*n-1的01字符串s,要求构造一个长度为n的字符串,使得该字符串与给出字符串中的s[1..n],s[2..n+1],s[3..n+2],......,s[n..2*n-1]均相似。
思路:对于每一对相似字符串,只要有一个位置上的字符相同即可。
于是对于s[1..n]取出第一位,对于s[2..n+1]取出第二位......即s[1],s[3],s[5],......,s[2*n-1]组合起来,就可构造出答案。
#include<cstdio> #include<cstring> #include<algorithm> #define maxn 55 #define mod 100000000 typedef long long ll; using namespace std; char s[maxn]; int main() { int T,i,n,len; scanf("%d",&T); while (T--) { scanf("%d%s",&n,s); len=2*n-1; for (i=0;i<len;i+=2) { printf("%c",s[i]); } printf(" "); } return 0; }
题意:有两个容量分别为p、f的背包,有两种质量分别为s、w,数量分别为cnts,cntw的物品,要求输出背包能够装入的最大物品数。
思路:贪心思想,先枚举p背包装入的s物品数(这里假设s<w),剩余的容量尽可能装入f物品;对于f背包先尽可能装入s物品,剩余的容量尽可能装入f物品。
在每次枚举后得到的最大物品数中取最大值就是答案。
比赛的时候我思维混乱,一度想到背包,二分等等 _(:_」∠)_就这么个题卡了半天。。
其实想到二分验证的时候有想到贪心思路了,然而想得不够,贪错了。。
#include<cstdio> #include<cstring> #include<algorithm> #define maxn 55 #define mod 100000000 typedef long long ll; using namespace std; int p,f,s,w,cnts,cntw; int cnt,ans,lim; int main() { int T,i,j,k,t; scanf("%d",&T); while (T--) { scanf("%d%d",&p,&f); scanf("%d%d",&cnts,&cntw); scanf("%d%d",&s,&w); if (s>w) { swap(s,w); swap(cnts,cntw); } ans=0; lim=p/s; lim=min(cnts,lim); for (i=0;i<=lim;i++) { k=min((p-i*s)/w,cntw); cnt=i+k; j=min(cnts-i,f/s); t=min((f-j*s)/w,cntw-k); cnt+=j+t; if (cnt>ans) ans=cnt; } printf("%d ",ans); } return 0; }