Codeforces Good Bye 2013 ABCDE

我会带着你远行 2022-08-24 14:15 172阅读 0赞

继续总结做过的练习赛。

链接:http://codeforces.com/contest/379

A New Year Candles

题意:初始有a根蜡烛,每根蜡烛照明1小时,每b根蜡烛的残骸可以变成一根新蜡烛,问一共可以照明多久

  1. #include <cstdio>
  2. int main ()
  3. {
  4. int sum=0,a,b;
  5. scanf("%d%d",&a,&b);
  6. sum+=a;
  7. int last=a;
  8. while (last>=b)
  9. {
  10. sum+=last/b;
  11. last=last%b+last/b;
  12. }
  13. printf("%d\n",sum);
  14. return 0;
  15. }

B New Year Present
题意:一个机器人要塞钱进钱袋,n个钱袋每个要塞ai个.不能连续塞两次,求任意方案
思路:让机器人从左走到右,再从右走到左,最多也只要2*300*300步

  1. #include <cstdio>
  2. #include <cstring>
  3. const int N=305;
  4. int data[N];
  5. int n;
  6. int main ()
  7. {
  8. scanf("%d",&n);
  9. int i;
  10. for (i=1;i<=n;i++)
  11. scanf("%d",&data[i]);
  12. int cur=1,left=1,right=n;
  13. bool flag=true;
  14. while (flag)
  15. {
  16. flag=false;
  17. for (i=1;i<n;i++)
  18. if (data[i])
  19. {
  20. printf("PR");
  21. data[i]--;
  22. flag=true;
  23. }
  24. else
  25. printf("R");
  26. for (i=n;i>1;i--)
  27. if (data[i])
  28. {
  29. printf("PL");
  30. data[i]--;
  31. flag=true;
  32. }
  33. else
  34. printf("L");
  35. }
  36. printf("\n");
  37. return 0;
  38. }

C New Year Ratings Change
题意:n个人,每个人最少要ai个礼物,要求每个人给礼物个数不重复,问怎么分配。
写得很逗的一题,居然fst,在case36超时了……
之后删了一个无关的变量,就795ms过了,看来以后交代码前有必要先优化一下。

  1. #include <cstdio>
  2. #include <algorithm>
  3. using namespace std;
  4. const int N=300005;
  5. struct Node
  6. {
  7. __int64 num,out;
  8. int id;
  9. void Get (int i)
  10. {
  11. id=i;
  12. scanf("%I64d",&num);
  13. }
  14. bool operator < (Node b) const
  15. {
  16. return num<b.num;
  17. }
  18. }data[N];
  19. bool cm (Node a,Node b)
  20. {
  21. return a.id<b.id;
  22. }
  23. int main ()
  24. {
  25. int n,i;
  26. scanf("%d",&n);
  27. for (i=0;i<n;i++)
  28. data[i].Get(i);
  29. sort(data,data+n);
  30. __int64 cur=0;
  31. for (i=0;i<n;i++)
  32. {
  33. if (cur<data[i].num)
  34. {
  35. cur=data[i].num;
  36. data[i].out=cur;
  37. }
  38. else
  39. {
  40. cur++;
  41. data[i].out=cur;
  42. }
  43. }
  44. sort(data,data+n,cm);
  45. for (i=0;i<n;i++)
  46. printf(i==n-1?"%I64d\n":"%I64d ",data[i].out);
  47. return 0;
  48. }

D New Year Letter

当时没能做出的一题。参考了:http://blog.csdn.net/accelerator\_/article/details/17713303
题意:给出递推次数k,初始字符串S1、S2的长度n,m, 使用类似斐波纳契数列的字符串合并的递推操作,使得最后得到的字符串中刚好含有x个”AC”,现在要你构造出满足条件的S1和S2。
思路:暴力枚举,dp,枚举两个串的头尾字母和AC个数。每次判断进行dp递推。
f[k]=f[k-2]*a+f[k-1]*b+sum;如果头尾字母构成AC,sum=1,否则为0

  1. #include <cstdio>
  2. #include <cstring>
  3. const int N=105;
  4. int k,x,n,m;
  5. __int64 f[N];
  6. char ans[N],s[N],e[N];
  7. void Out (char s, char e, int len, int num)
  8. {
  9. memset(ans,'B',sizeof(ans));
  10. ans[0]=s;
  11. ans[len-1]=e;
  12. ans[len]=0;
  13. int bo = (s == 'A' ? 0 : 1); //开头是否为'A'
  14. for (int i=0;i<num;i++)
  15. {
  16. ans[bo + 2*i] = 'A';
  17. ans[bo + 2*i+1] = 'C';
  18. }
  19. printf("%s\n", ans);
  20. }
  21. bool OK (char s1, char e1, char s2, char e2, int xx, int yy)
  22. {
  23. s[1]=s1; e[1]=e1;
  24. s[2]=s2; e[2]=e2;
  25. f[1]=xx; f[2]=yy;
  26. for (int i=3;i<=k;i++)
  27. {
  28. s[i] = s[i-2];
  29. e[i] = e[i-1];
  30. f[i] = f[i-1] + f[i-2] + (e[i-2]=='A' && s[i-1]=='C');
  31. }
  32. if (f[k] == x)
  33. return true;
  34. return false;
  35. }
  36. bool judge ()
  37. {
  38. for (char s1='A'; s1<='C'; s1++) for (char e1='A'; e1<='C'; e1++)
  39. {
  40. if (n == 1 && s1 != e1) continue; //长度为1,且首尾相同
  41. for (char s2='A'; s2<='C'; s2++)
  42. for (char e2='A'; e2<='C'; e2++)
  43. {
  44. if (m == 1 && s2 != e2) continue; //长度为1,且首尾相同
  45. int lx = n - (s1 != 'A') - (e1 != 'C');
  46. int ly = m - (s2 != 'A') - (e2 != 'C');
  47. lx /= 2; ly /= 2; //各自串中可能有的AC个数上限
  48. for (int xx = 0; xx <= lx; xx++)
  49. for (int yy = 0; yy <= ly; yy++)
  50. {
  51. if (n == 2 && s1 == 'A' && e1 == 'C' && xx == 0) continue;
  52. if (m == 2 && s2 == 'A' && e2 == 'C' && yy == 0) continue;
  53. if (OK(s1, e1, s2, e2, xx, yy))
  54. {
  55. Out(s1, e1, n, xx);
  56. Out(s2, e2, m, yy);
  57. return true;
  58. }
  59. }
  60. }
  61. }
  62. return false;
  63. }
  64. int main ()
  65. {
  66. scanf("%d%d%d%d",&k,&x,&n,&m);
  67. if (judge()==false)
  68. printf("Happy new year!\n");
  69. return 0;
  70. }

E New Year Tree Decorations
题意:n片纸,每块长都是k,求每块纸可见面积是多少
官方题解是俄语真是压力山大,参考了http://kmjp.hatenablog.jp/entry/2014/01/02/1000
果然日语还是能看懂两句的……
思路:枚举每一个区间,单独考虑,在每一个区间内对每张纸进行切割

  1. #include <map>
  2. #include <cmath>
  3. #include <cstdio>
  4. #include <algorithm>
  5. using namespace std;
  6. #define upmax(a,b) ((a)=(a)>(b)?(a):(b))
  7. int Y[301][301];
  8. double S[301]; //存储最终每张纸的可见面积
  9. int main ()
  10. {
  11. int i,x,n,k;
  12. scanf("%d%d",&n,&k);
  13. for (i=0;i<n;i++)
  14. for (x=0;x<=k;x++)
  15. scanf("%d",&Y[i][x]);
  16. for (x=0;x<k;x++)
  17. {//枚举每一个区间
  18. map<double,double> M;
  19. M[0]=0; M[1]=0;
  20. for (i=0;i<n;i++)
  21. {//枚举每张纸
  22. map<double,double> M2;
  23. M2[0]=M[0];
  24. double prex=0,prey=M[0];
  25. map<double,double>::iterator it;
  26. for (it=M.begin();it!=M.end();it++)
  27. {//枚举区间内所有交点,其中0和1为左右端点
  28. if (it->first==0) continue;
  29. double cx = it->first;
  30. double cy = it->second;
  31. double slope=Y[i][x+1]-Y[i][x]; //斜率
  32. double y1= Y[i][x]+slope*prex; //待切割斜线的端点坐标
  33. double y2= Y[i][x]+slope*cx;
  34. //分四种情况讨论
  35. if (y1>=prey && y2>=cy)
  36. {
  37. S[i] += ((y1+y2)-(cy+prey))*(cx-prex)/2.0; //梯形面积公式
  38. M2[cx]=y2;
  39. }
  40. else if (y1<=prey && y2<=cy)
  41. M2[cx]=cy;
  42. else if (y1>=prey && y2<=cy)
  43. {//计算交点
  44. double nx = prex + (cx-prex)*(abs(y1-prey)/(abs(y1-prey)+abs(y2-cy)));
  45. double ny = prey + (cy-prey)*(abs(y1-prey)/(abs(y1-prey)+abs(y2-cy)));
  46. S[i] += ((y1-prey))*(nx-prex)/2.0;
  47. M2[nx]=ny; //将新交点加入容器,用于切割之后的线
  48. M2[cx]=cy;
  49. }
  50. else if (y1<=prey && y2>=cy)
  51. {//计算交点
  52. double nx = prex + (cx-prex)*(abs(y1-prey)/(abs(y1-prey)+abs(y2-cy)));
  53. double ny = prey + (cy-prey)*(abs(y1-prey)/(abs(y1-prey)+abs(y2-cy)));
  54. S[i] += ((y2-cy))*(cx-nx)/2.0;
  55. M2[nx]=ny;
  56. }
  57. prex=cx; prey=cy;
  58. }
  59. upmax(M2[0],Y[i][x]);
  60. upmax(M2[1],Y[i][x+1]);
  61. swap(M,M2);
  62. }
  63. }
  64. for (i=0;i<n;i++)
  65. printf("%.12lf\n",S[i]);
  66. return 0;
  67. }

F New Year Tree
貌似与树的直径有关,以后学习了这部分再回来看。

发表评论

表情:
评论列表 (有 0 条评论,172人围观)

还没有评论,来说两句吧...

相关阅读

    相关 GOOD BYE OI

    大米饼正式退役了,OI给我带来很多东西 我会的数学知识基本都在下面了 博客园的评论区问题如果我看到了应该是会尽力回答的... 这也是我作为一个OIer最后一次