Codeforces Round #367 (Div. 2)

  AB都是水题。

  C题,DP题。没能够独立的做出来,但是会了以后感觉还是蛮简单的= =。代码如下:

 1 #include <stdio.h>
 2 #include <algorithm>
 3 #include <string.h>
 4 #include <iostream>
 5 #include <math.h>
 6 #include <string>
 7 using namespace std;
 8 const int N = 100000 + 5;
 9 typedef long long ll;
10 const ll inf = 1e18;
11 
12 ll a[N];
13 string s[N][2];
14 string inv(string now)
15 {
16     for(int i=0;i<now.size()/2;i++) swap(now[i],now[now.size()-i-1]);
17     return now;
18 }
19 ll dp[N][2];
20 
21 int main()
22 {
23     int n;
24     cin >> n;
25     for(int i=1;i<=n;i++) scanf("%I64d",a+i);
26     for(int i=1;i<=n;i++) cin >> s[i][0], s[i][1] = inv(s[i][0]);
27     for(int i=1;i<=n;i++) dp[i][0] = dp[i][1] = inf;
28     dp[1][0] = 0, dp[1][1] = a[1];
29     for(int i=2;i<=n;i++)
30     {
31         if(s[i][0] >= s[i-1][0]) dp[i][0] = min(dp[i][0], dp[i-1][0]);
32         if(s[i][0] >= s[i-1][1]) dp[i][0] = min(dp[i][0], dp[i-1][1]);
33         if(s[i][1] >= s[i-1][0]) dp[i][1] = min(dp[i][1], dp[i-1][0] + a[i]);
34         if(s[i][1] >= s[i-1][1]) dp[i][1] = min(dp[i][1], dp[i-1][1] + a[i]);
35     }
36     if(dp[n][0] == inf && dp[n][1] == inf) puts("-1");
37     else printf("%I64d
",min(dp[n][0], dp[n][1]));
38     return 0;
39 }
C

  D题,字典树水题。但是有一个坑点在于题目规定了0是一开始就被放在里面的。代码如下:

 1 #include <stdio.h>
 2 #include <algorithm>
 3 #include <string.h>
 4 using namespace std;
 5 const int N = 34;
 6 typedef long long ll;
 7 
 8 int tot = 0;
 9 
10 struct node
11 {
12     int val;
13     int cnt;
14     node *child[2];
15     void clear()
16     {
17         cnt = 0;
18         val = -1;
19         for(int i=0;i<2;i++)
20         {
21             child[i] = NULL;
22         }
23     }
24 }nodes[N*200000];
25 node *root;
26 
27 node* getnew()
28 {
29     node *p = &nodes[tot++];
30     p->clear();
31     return p;
32 }
33 
34 void update(node *p,int deep,ll x,int op)
35 {
36     if(deep == -1) {p->val = x;return;}
37 
38     int m = ((x>>deep) & 1);
39     if(p->child[m] == NULL)
40     {
41         node *temp = getnew();
42         p->child[m] = temp;
43     }
44     p->child[m]->cnt += op;
45     update(p->child[m],deep-1,x,op);
46 }
47 
48 int query(node *p,int deep,ll x)
49 {
50     if(deep == -1) return p->val;
51 
52     int m = 1 - ((x>>deep) & 1);
53     if(p->child[m] != NULL && p->child[m]->cnt != 0)
54     {
55         return query(p->child[m],deep-1,x);
56     }
57     else return query(p->child[1-m],deep-1,x);
58 }
59 
60 int main()
61 {
62     tot = 0;
63     root = getnew();
64     update(root,N,(ll)0,1);
65     int q;
66     scanf("%d",&q);
67     while(q--)
68     {
69         char s[5];
70         int x;
71         scanf("%s%d",s,&x);
72         if(s[0] == '+') update(root,N,(ll)x,1);
73         else if(s[0] == '-') update(root,N,(ll)x,-1);
74         else
75         {
76             int t = query(root,N,(ll)x);
77             printf("%d
",t^x);
78         }
79     }
80     return 0;
81 }
D

  E题,用链表搞一下。虽然思路比较简单,但是还是调试了好几个小时。原因在于链表的调试起来不方便,以及我自己对于数据结构掌握的不好= =。最后是终于想清楚为什么我自己的写法需要再次定位p1和p2了。希望以后还是能够捋得清楚,抑或是换一种更为简单的写法。链表真是个麻烦的东西..代码如下:

  1 #include <stdio.h>
  2 #include <algorithm>
  3 #include <string.h>
  4 #include <iostream>
  5 #include <math.h>
  6 #include <string>
  7 using namespace std;
  8 const int N = 1000 + 5;
  9 typedef long long ll;
 10 const ll inf = 1e18;
 11 
 12 struct node
 13 {
 14     int val;
 15     int child[2];
 16     // 0 right; 1 down
 17 }p[N*N];
 18 
 19 int n,m,q;
 20 int get(int x,int y) {return x*(m+1)+(y+1);}
 21 
 22 int main()
 23 {
 24     cin >> n >> m >> q;
 25     p[get(0,0)].child[1] = get(1,0); p[get(0,0)].child[0] = get(0,1);
 26     for(int i=1;i<=m;i++) p[get(0,i)].child[0] = get(0,i+1), p[get(0,i)].child[1] = get(1,i);
 27     for(int i=1;i<=n;i++) p[get(i,0)].child[0] = get(i,1), p[get(i,0)].child[1] = get(i+1,0);
 28     for(int i=1;i<=n;i++)
 29     {
 30         for(int j=1;j<=m;j++)
 31         {
 32             scanf("%d",&p[get(i,j)].val);
 33             p[get(i,j)].child[0] = get(i,j+1);
 34             p[get(i,j)].child[1] = get(i+1,j);
 35         }
 36     }
 37     while(q--)
 38     {
 39         int a,b,c,d,h,w;
 40         scanf("%d%d%d%d%d%d",&a,&b,&c,&d,&h,&w);
 41         int p1 = 0, p2 = 0;
 42         int now = get(0,0);
 43         for(int i=1;i<a;i++) now = p[now].child[1];
 44         for(int i=1;i<b;i++) now = p[now].child[0];
 45         p1 = now;
 46         now = get(0,0);
 47         for(int i=1;i<c;i++) now = p[now].child[1];
 48         for(int i=1;i<d;i++) now = p[now].child[0];
 49         p2 = now;
 50         
 51         // 1
 52         int temp1 = p1; temp1 = p[temp1].child[1];
 53         int temp2 = p2; temp2 = p[temp2].child[1];
 54         for(int i=1;i<=h;i++)
 55         {
 56             swap(p[temp1].child[0], p[temp2].child[0]);
 57             temp1 = p[temp1].child[1];
 58             temp2 = p[temp2].child[1];
 59         }
 60         
 61         // 2
 62         temp1 = p1; temp1 = p[temp1].child[1]; for(int i=1;i<=w;i++) temp1 = p[temp1].child[0];
 63         temp2 = p2; temp2 = p[temp2].child[1]; for(int i=1;i<=w;i++) temp2 = p[temp2].child[0];
 64         for(int i=1;i<=h;i++)
 65         {
 66             swap(p[temp1].child[0], p[temp2].child[0]);
 67             temp1 = p[temp1].child[1];
 68             temp2 = p[temp2].child[1];
 69         }
 70         
 71         // 必须重新定位p1和p2
 72         now = get(0,0);
 73         for(int i=1;i<a;i++) now = p[now].child[1];
 74         for(int i=1;i<b;i++) now = p[now].child[0];
 75         p1 = now;
 76         now = get(0,0);
 77         for(int i=1;i<c;i++) now = p[now].child[1];
 78         for(int i=1;i<d;i++) now = p[now].child[0];
 79         p2 = now;
 80         
 81         // 3
 82         temp1 = p1; temp1 = p[temp1].child[0];
 83         temp2 = p2; temp2 = p[temp2].child[0];
 84         for(int i=1;i<=w;i++)
 85         {
 86             swap(p[temp1].child[1], p[temp2].child[1]);
 87             temp1 = p[temp1].child[0];
 88             temp2 = p[temp2].child[0];
 89         }
 90 
 91         // 4
 92         temp1 = p1; temp1 = p[temp1].child[0]; for(int i=1;i<=h;i++) temp1 = p[temp1].child[1];
 93         temp2 = p2; temp2 = p[temp2].child[0]; for(int i=1;i<=h;i++) temp2 = p[temp2].child[1];
 94         for(int i=1;i<=w;i++)
 95         {
 96             swap(p[temp1].child[1], p[temp2].child[1]);
 97             temp1 = p[temp1].child[0];
 98             temp2 = p[temp2].child[0];
 99         }
100     }
101     for(int i=1;i<=n;i++)
102     {
103         int now = p[get(i,0)].child[0];
104         for(int j=1;j<=m;j++)
105         {
106             printf("%d%c",p[now].val,j==m?'
':' ');
107             now = p[now].child[0];
108         }
109     }
110     return 0;
111 }
E
原文地址:https://www.cnblogs.com/zzyDS/p/6386470.html