Codeforces Round #260 (Div. 2)

题目链接

Fedya and Maths

按照题目的公式求数,有一个规律4的倍数为4,然后就是大数取模,但是我还二的

把strlen(s)放到了循环上,导致了每次循环都要算一次,最后超时。本来很简单的,, 简直惨。。。

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdlib>
 4 #include <cmath>
 5 #include <cstdio>
 6 #include <vector>
 7 #include <algorithm>
 8 #define LL long long
 9 const int maxn = 100000+10;
10 using namespace std;
11 int ans;
12 char s[maxn];
13 int num[maxn];
14 
15 int main()
16 {
17     int i, j, x, len;
18     while(~scanf("%s", s))
19     {
20         len = strlen(s);
21         for(i = 0; i < len; i++)
22         num[i] = s[i]-48;
23         x = 0;
24         for(i = 0; i < len; i++)
25         {
26             x = (x*10+num[i])%4;
27         }
28         if(x==0)
29         cout<<4<<endl;
30         else
31         cout<<0<<endl;
32     }
33     return 0;
34 }

Boredom

每次找一个数,但要删除和它数字差一的数字,然后这个数字是得分,求最大 的得分。

分析:刚开始有思路,就乱搞了一下,结果最后判的时候超内存了。。。。

其实应该是dp【】,d[i] 表示到i个数为止的最大值,每次都是取间隔的一个和间隔两个的最大值。

下面是代码,判断条件有点多,代码可以更简化的。。。

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdlib>
 4 #include <cmath>
 5 #include <cstdio>
 6 #include <vector>
 7 #include <queue>
 8 #include <algorithm>
 9 #define LL __int64
10 const int maxn = 100000+10;
11 using namespace std;
12 LL d[maxn], h[maxn], ans;
13 int n;
14 
15 int main()
16 {
17     int i, a;
18     while(~scanf("%d", &n))
19     {
20         memset(h, 0, sizeof(h));
21         memset(d, 0, sizeof(d));
22         for(i = 0; i < n; i++)
23         {
24             scanf("%d", &a);
25             h[a] ++;
26         }
27         ans = 0;
28         d[1] = h[1];
29         d[2] = 2*h[2];
30         d[3] = 3*h[3]+d[1];
31         ans = d[1];
32         ans = max(d[2], ans);
33         ans = max(d[3], ans);
34         for(i = 4; i <= 100000; i++)
35         {
36             if(h[i]==0)
37             d[i] = ans;
38             else
39             {
40                 d[i] = (LL)h[i]*i+max(d[i-2], d[i-3]);
41                 if(h[i-1]==0)
42                 d[i] = max(d[i], d[i-1]+i*h[i]);
43             }
44             if(d[i]>ans) ans = d[i];
45         }
46         printf("%I64d
", ans);
47     }
48     return 0;
49 }
原文地址:https://www.cnblogs.com/bfshm/p/3900733.html