CodeForces Good Bye 2016

  A题,水题略过。

  B题,也水,但是想复杂了。只要运动超出[0,20000]的范围就算不可能了。

  C题,我自己的方法是解不等式,然后取最大的答案即可。代码如下:

 1 #include <stdio.h>
 2 #include <algorithm>
 3 #include <string.h>
 4 #include <iostream>
 5 #include <string>
 6 #include <map>
 7 #include <vector>
 8 using namespace std;
 9 const int N = 400 + 5;
10 const int inf = 2e9;
11 
12 int main()
13 {
14     int L = -inf, R = -inf;
15     int n; cin >> n;
16     //int add = 0;
17     int pre = 0;
18     for(int i=1;i<=n;i++)
19     {
20         int change, d;
21         scanf("%d%d",&change, &d);
22         //add += change;
23         if(d == 1)
24         {
25             if(L == -inf) L = 1900 - pre;
26             else L = max(L, 1900 - pre);
27         }
28         else
29         {
30             if(R == -inf) R = 1899 - pre;
31             else R = min(R, 1899 - pre);
32         }
33         pre += change;
34     }
35     if(L != -inf && R != -inf && L > R) puts("Impossible");
36     else if(L != -inf && R == -inf) puts("Infinity");
37     else printf("%d
",R + pre);
38     return 0;
39 }
C

  D题是记忆化搜索,补题的时候没做出来。。代码如下:

 1 #include <stdio.h>
 2 #include <algorithm>
 3 #include <string.h>
 4 #include <iostream>
 5 #include <string>
 6 #include <map>
 7 #include <vector>
 8 #include <set>
 9 using namespace std;
10 const int N = 400 + 5;
11 const int inf = 2e9;
12 typedef pair<int,int> pii;
13 
14 int dirx[] = {1,1,1,0,-1,-1,-1,0};
15 int diry[] = {1,0,-1,-1,-1,0,1,1};
16 int vis[40][10][N][N];
17 int t[40], n;
18 int mp[N][N];
19 
20 void dfs(int pos, int dir, int x, int y)
21 {
22     if(pos > n || vis[pos][dir][x][y]) return ;
23     vis[pos][dir][x][y] = 1;
24     for(int i=0;i<t[pos];i++) mp[x+dirx[dir]*i][y+diry[dir]*i] = 1;
25     x += dirx[dir] * (t[pos] - 1);
26     y += diry[dir] * (t[pos] - 1);
27     dfs(pos+1, (dir+1)%8, x+dirx[(dir+1)%8], y+diry[(dir+1)%8]);
28     dfs(pos+1, (dir+7)%8, x+dirx[(dir+7)%8], y+diry[(dir+7)%8]);
29 }
30 
31 int main()
32 {
33     cin >> n;
34     for(int i=1;i<=n;i++) scanf("%d",t+i);
35     dfs(1,1,N/2,N/2);
36     int ans = 0;
37     for(int i=0;i<N;i++)
38     {
39         for(int j=0;j<N;j++) ans += mp[i][j];
40     }
41     cout << ans << endl;
42     return 0;
43 }
D

  E题,好题。用0~4分别表示拥有2017的若干个字母的情况,x[i][j]表示从i状态转移到j状态需要删除几个字符,然后进行转移。并且用线段树进行维护答案。具体见代码:

 1 #include <stdio.h>
 2 #include <algorithm>
 3 #include <string.h>
 4 #include <iostream>
 5 #include <string>
 6 #include <map>
 7 #include <vector>
 8 #include <set>
 9 #define ls (o<<1)
10 #define rs (o<<1|1)
11 #define t_mid (l+r>>1)
12 #define lson ls,l,t_mid
13 #define rson rs,t_mid+1,r
14 using namespace std;
15 const int N = 200000 + 5;
16 const int inf = 0x3f3f3f3f;
17 typedef pair<int,int> pii;
18 
19 int n,q;
20 char s[N];
21 struct node
22 {
23     int x[5][5];
24     node operator + (const node A) const
25     {
26         node ans;
27         for(int i=0;i<5;i++)
28         {
29             for(int j=0;j<5;j++)
30             {
31                 ans.x[i][j] = inf;
32                 for(int k=0;k<5;k++) ans.x[i][j] = min(ans.x[i][j], x[i][k] + A.x[k][j]);
33             }
34         }
35         return ans;
36     }
37 }p[N<<2];
38 
39 void build(int o,int l,int r)
40 {
41     if(l == r)
42     {
43         for(int i=0;i<5;i++) for(int j=0;j<5;j++) p[o].x[i][j] = i==j ? 0 : inf;
44         if(s[l] == '2')
45         {
46             p[o].x[0][0] = 1;
47             p[o].x[0][1] = 0;
48         }
49         else if(s[l] == '0')
50         {
51             p[o].x[1][1] = 1;
52             p[o].x[1][2] = 0;
53         }
54         else if(s[l] == '1')
55         {
56             p[o].x[2][2] = 1;
57             p[o].x[2][3] = 0;
58         }
59         else if(s[l] == '7')
60         {
61             p[o].x[3][3] = 1;
62             p[o].x[3][4] = 0;
63         }
64         else if(s[l] == '6')
65         {
66             p[o].x[3][3] = 1;
67             p[o].x[4][4] = 1; // 已经有2017,现在多出6,要删掉6
68         }
69         return ;
70     }
71     build(lson);
72     build(rson);
73     // up(o)
74     p[o] = p[ls] + p[rs];
75 }
76 
77 node query(int o,int l,int r,int ql,int qr)
78 {
79     if(l == ql && r == qr) return p[o];
80     if(qr <= t_mid) return query(lson,ql,qr);
81     else if(ql > t_mid) return query(rson,ql,qr);
82     else return query(lson,ql,t_mid) + query(rson,t_mid+1,qr);
83 }
84 
85 int main()
86 {
87     cin >> n >> q;
88     scanf("%s",s+1);
89     build(1,1,n);
90     while(q--)
91     {
92         int l, r;
93         scanf("%d%d",&l, &r);
94         int ans = query(1,1,n,l,r).x[0][4];
95         printf("%d
",ans == inf ? -1 : ans);
96     }
97     return 0;
98 }
E
原文地址:https://www.cnblogs.com/zzyDS/p/6278649.html