TOJ 3800: -3+1

痛定思痛。 2022-05-06 10:56 144阅读 0赞

3800: -3+1

时间限制(普通/Java):1000MS/3000MS 内存限制:65536KByte
总提交: 150 测试通过:59

描述

ZOJ is 10 years old! For celebrating, we are offering the easiest problem in the world to you.

Recently we received a long sequence. We can modify the sequence once by the following two steps.

  1. Choose any element in the sequence, say x(satisfying x ≥ 3), and subtract 3 from x.
  2. Choose any element in the sequence, say x, and add 1 to x.

Now, we want to know how many times at most the sequence can be modified.

输入

The input contains multiple test cases. For each case, the first line contains an integer n(1 ≤ n ≤ 20000). The second line contains n integers describing the sequence. All the numbers in the sequence are non-negative and not greater than 1000000.

输出

Output number of times at most the sequence can be modified, one line per case.

样例输入

1
10
2
10 11

样例输出

4
10

题目来源

ZOJ 10th Anniversary

题意:有一个操作,先对序列中任意一个数字-3,然后再对任意一个数字+1,问最多可以进行几次这样的操作。

思路:输入的时候,就把每个数字处理一下,能/3的都除,记录除了几次即为现有+1的次数,然后这个数就变成了%3,所以整个序列就变成0,1,2的序列(op用来记录0,1,2分别的个数)。

然后先考虑2,因为给2一个1,它变成3之后又能+1,所以对2而言,操作数++,1的次数不变,但前提是1至少有一次。

然后考虑1,因为让1变成3要2个1,但是1变成3之后又能+1,所以容易理解为1个1就能转换1,所以要先判sum1为2的情况,这个时候只能处理到一个1,如果有多的话,就算作1个+1处理一个1。

考虑完以上,整个序列全部为0,此时剩下1的个数去填满,要循环的填,因为填完一个又会增加1次。

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<math.h>
  4. #include<iostream>
  5. #include<string>
  6. #include<algorithm>
  7. #include<map>
  8. #include<set>
  9. #include<queue>
  10. #include<vector>
  11. using namespace std;
  12. #define inf 0x3f3f3f3f
  13. #define LL long long
  14. int a[20010],op[5];
  15. int main()
  16. {
  17. int n,i,j,cs,sum1;
  18. while(scanf("%d",&n)!=EOF)
  19. {
  20. memset(op,0,sizeof op);
  21. cs=0;sum1=0;
  22. for(i=0;i<n;i++)
  23. {
  24. scanf("%d",&a[i]);
  25. cs+=a[i]/3;
  26. sum1+=a[i]/3;
  27. a[i]=a[i]%3;
  28. op[a[i]]++;
  29. }
  30. if(sum1)
  31. cs+=op[2],op[2]=0;
  32. if(sum1==2)
  33. cs++,op[1]=0,sum1=0;
  34. else if(sum1>=op[1])
  35. cs+=op[1],sum1-=op[1],op[1]=0;
  36. while(sum1>=3)
  37. {
  38. cs+=sum1/3;
  39. sum1=sum1/3+sum1%3;
  40. }
  41. printf("%d\n",cs);
  42. }
  43. }

发表评论

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

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

相关阅读

    相关 TOJ1292 排序

    滴,集训第二十九天打卡。 (其实都快结束了... 距离老师上一次开比赛也是九天前了,于是我只能在TOJ划水了... 还在https://www.panda.tv/1352

    相关 TOJ1092 More Beautiful

    描述 当老师不容易,尤其是当小学的老师更难:现在的小朋友做作业喜欢滥用括号。 虽然不影响计算结果,但不够美观,容易出错,而且可读性差。但又不能一棒子打死,也许他们就是将来

    相关 TOJ4448 判断正确

    描述 小航比较粗心,判断他写的表达式是否正确。 输入 多组数据,直到文件结束。每行输入 a 运算符号 b 关系符号 c ,(0<=a,b,c<=9),用英文字母表示。