Educational Codeforces Round49

  A Palindromic Twist(字符串)

  问每个字母必须向左或向右变成另一个字母,问能不能构成回文

 1 #include <iostream>
 2 #include <string.h>
 3 #include <cstdio>
 4 #include <vector>
 5 #include <stack>
 6 #include <math.h>
 7 #include <string>
 8 #include <algorithm>
 9 #include <time.h>
10 
11 #define SIGMA_SIZE 26
12 #define lson rt<<1
13 #define rson rt<<1|1
14 #define lowbit(x) (x&-x)
15 #define foe(i, a, b) for(int i=a; i<=b; i++)
16 #define fo(i, a, b) for(int i=a; i<b; i++);
17 //#pragma warning ( disable : 4996 )
18 
19 using namespace std;
20 typedef long long LL;
21 inline LL LMax(LL a, LL b) { return a>b ? a : b; }
22 inline LL LMin(LL a, LL b) { return a>b ? b : a; }
23 inline LL lgcd(LL a, LL b) { return b == 0 ? a : lgcd(b, a%b); }
24 inline LL llcm(LL a, LL b) { return a / lgcd(a, b)*b; }  //a*b = gcd*lcm
25 inline int Max(int a, int b) { return a>b ? a : b; }
26 inline int Min(int a, int b) { return a>b ? b : a; }
27 inline int gcd(int a, int b) { return b == 0 ? a : gcd(b, a%b); }
28 inline int lcm(int a, int b) { return a / gcd(a, b)*b; }  //a*b = gcd*lcm
29 const LL INF = 0x3f3f3f3f3f3f3f3f;
30 const LL mod = 998244353;
31 const double eps = 1e-8;
32 const int inf = 0x3f3f3f3f;
33 const int maxk = 1e6 + 5;
34 const int maxn = 5e5+5;
35 
36 int len;
37 char str[110];
38 
39 int main()
40 {
41     #ifndef ONLINE_JUDGE
42         freopen("input.txt", "r", stdin);
43     #endif
44     
45     int T; cin >> T;
46     while(T--)
47     {
48         scanf("%d", &len);
49         scanf("%s", str+1);
50         if (len == 1)
51         {
52             printf("YES");
53             continue;
54         }
55 
56         int mid;
57         int l, r, tmp;
58         bool ok = true;
59         mid = len/2;
60         for ( int i = 1; i <= mid; i++ )
61         {
62             l = i; r = len-i+1;
63             tmp = abs(str[l]-str[r]);
64             if (tmp > 2 || tmp == 1)
65             {
66                 //cout << tmp << endl;
67                 ok = false;
68                 break;
69             }
70         }
71         if (ok)
72             printf("YES\n");
73         else
74             printf("NO\n");
75     }
76     return 0;
77 }
View Code

  

  B Numbers on the Chessboard(模拟)

  题意简单,写起来容易错,主要是将矩阵每两层作为一大层(+N),然后判断所在点是前面有几层

 1 #include <iostream>
 2 #include <string.h>
 3 #include <cstdio>
 4 #include <vector>
 5 #include <stack>
 6 #include <math.h>
 7 #include <string>
 8 #include <algorithm>
 9 #include <time.h>
10 
11 #define SIGMA_SIZE 26
12 #define lson rt<<1
13 #define rson rt<<1|1
14 #define lowbit(x) (x&-x)
15 #define foe(i, a, b) for(int i=a; i<=b; i++)
16 #define fo(i, a, b) for(int i=a; i<b; i++);
17 //#pragma warning ( disable : 4996 )
18 
19 using namespace std;
20 typedef long long LL;
21 inline LL LMax(LL a, LL b) { return a>b ? a : b; }
22 inline LL LMin(LL a, LL b) { return a>b ? b : a; }
23 inline LL lgcd(LL a, LL b) { return b == 0 ? a : lgcd(b, a%b); }
24 inline LL llcm(LL a, LL b) { return a / lgcd(a, b)*b; }  //a*b = gcd*lcm
25 inline int Max(int a, int b) { return a>b ? a : b; }
26 inline int Min(int a, int b) { return a>b ? b : a; }
27 inline int gcd(int a, int b) { return b == 0 ? a : gcd(b, a%b); }
28 inline int lcm(int a, int b) { return a / gcd(a, b)*b; }  //a*b = gcd*lcm
29 const LL INF = 0x3f3f3f3f3f3f3f3f;
30 const LL mod = 998244353;
31 const double eps = 1e-8;
32 const int inf = 0x3f3f3f3f;
33 const int maxk = 1e6 + 5;
34 const int maxn = 5e5+5;
35 
36 LL n, q;
37 
38 int main()
39 {
40     #ifndef ONLINE_JUDGE
41         freopen("input.txt", "r", stdin);
42     #endif
43 
44     cin >> n >> q;
45     int x, y;
46     while(q--)
47     {
48         LL ans = 0;
49         LL tmp;
50         scanf("%d %d", &x, &y);
51         if ((x+y)%2==0) {
52             int h = x%2;
53             tmp = h ? x/2 : (x-1)/2;
54             if (h) {
55                 ans = n*tmp + (y/2)+1;
56             }
57             else {
58                 ans = n*tmp + (n/2)+(y/2);
59                 if (n%2) ans++;
60             }
61         }
62         else {
63             int h = x%2;
64             tmp = h ? x/2 : (x-1)/2;
65             if (h) {
66                 ans = n*tmp + (y/2);
67             }
68             else {
69                 ans = n*tmp + (n/2) + (y+1)/2;
70             }
71             if (n%2) 
72                 ans += (n*n)/2+1;
73             else 
74                 ans += (n*n)/2;
75         }
76         printf("%lld\n", ans);
77     }
78     return 0;
79 }
View Code

  

  C Minimum Value Rectangle 

  很容易推出最后结果和(a/b)+(b/a)有关,根据均值不等式肯定两个值越近越小,所以把可行边排序后每次选择枚举距离最相近的两种边选最小

 1 #include <iostream>
 2 #include <string.h>
 3 #include <cstdio>
 4 #include <vector>
 5 #include <stack>
 6 #include <math.h>
 7 #include <string>
 8 #include <algorithm>
 9 #include <time.h>
10 
11 #define SIGMA_SIZE 26
12 #define lson rt<<1
13 #define rson rt<<1|1
14 #define lowbit(x) (x&-x)
15 #define foe(i, a, b) for(int i=a; i<=b; i++)
16 #define fo(i, a, b) for(int i=a; i<b; i++)
17 //#pragma warning ( disable : 4996 )
18 
19 using namespace std;
20 typedef long long LL;
21 inline LL LMax(LL a, LL b) { return a>b ? a : b; }
22 inline LL LMin(LL a, LL b) { return a>b ? b : a; }
23 inline LL lgcd(LL a, LL b) { return b == 0 ? a : lgcd(b, a%b); }
24 inline LL llcm(LL a, LL b) { return a / lgcd(a, b)*b; }  //a*b = gcd*lcm
25 inline int Max(int a, int b) { return a>b ? a : b; }
26 inline int Min(int a, int b) { return a>b ? b : a; }
27 inline int gcd(int a, int b) { return b == 0 ? a : gcd(b, a%b); }
28 inline int lcm(int a, int b) { return a / gcd(a, b)*b; }  //a*b = gcd*lcm
29 const LL INF = 0x3f3f3f3f3f3f3f3f;
30 const LL mod = 998244353;
31 const double eps = 1e-8;
32 const int inf = 0x3f3f3f3f;
33 const int maxk = 1e6+5;
34 const int maxn = 1e6+5;
35 
36 int n, cnt;
37 int a[maxn];
38 int num[maxn];
39 
40 
41 void init()
42 {
43     cin >> n;
44     cnt = 0;
45 
46     int tmp;
47     foe(i, 1, n)
48         scanf("%d", &a[i]);
49     sort(a+1, a+1+n);
50 
51     tmp = 0;
52     foe(i, 2, n)
53     {
54         if (a[i] == a[i-1]) {
55             if (!tmp) {
56                 num[++cnt] = a[i];
57                 tmp++;
58             }
59             else {
60                 tmp = 0;
61             }
62         }
63         else {
64             tmp = 0;
65         }
66     }
67 }
68 
69 int main()
70 {
71     #ifndef ONLINE_JUDGE
72         freopen("input_2.txt", "r", stdin);
73     #endif
74 
75     int T; cin >> T;
76     while(T--)
77     {
78         init();
79 
80         double tmp, ans = inf;
81         int a, b;
82         //cout << cnt << endl;
83         fo(i, 1, cnt)
84         {
85             tmp = (double)1*num[i]/num[i+1] + (double)1*num[i+1]/num[i];
86             if ( tmp < ans )
87             {
88                 ans = tmp;
89                 a = i; b = i+1;
90             }
91         }
92         printf("%d %d %d %d\n", num[a], num[a], num[b], num[b]);
93     }
94     return 0;
95 }
View Code

  

  D Mouse Hunt

  有只耗子在n个房子内乱串,当此时刻耗子在i房间时,下一时刻耗子会移动到a[i]号房间,在i房间布置捕鼠器需要耗费c[i],开始耗子位置随机,问最少消耗多少能保证一定捕捉到老鼠

  根据题意我们需要判断连通量和环,因为耗子顺着一条路走下去最后必然在某一个点后走到原先的某点形成一个环,所以用并查集判环,在该环上寻找最小cost就行了

  1 #include <iostream>
  2 #include <string.h>
  3 #include <cstdio>
  4 #include <vector>
  5 #include <stack>
  6 #include <math.h>
  7 #include <string>
  8 #include <algorithm>
  9 #include <time.h>
 10 
 11 #define SIGMA_SIZE 26
 12 #define lson rt<<1
 13 #define rson rt<<1|1
 14 #define lowbit(x) (x&-x)
 15 #define foe(i, a, b) for(int i=a; i<=b; i++)
 16 #define fo(i, a, b) for(int i=a; i<b; i++)
 17 //#pragma warning ( disable : 4996 )
 18 
 19 using namespace std;
 20 typedef long long LL;
 21 inline LL LMax(LL a, LL b) { return a>b ? a : b; }
 22 inline LL LMin(LL a, LL b) { return a>b ? b : a; }
 23 inline LL lgcd(LL a, LL b) { return b == 0 ? a : lgcd(b, a%b); }
 24 inline LL llcm(LL a, LL b) { return a / lgcd(a, b)*b; }  //a*b = gcd*lcm
 25 inline int Max(int a, int b) { return a>b ? a : b; }
 26 inline int Min(int a, int b) { return a>b ? b : a; }
 27 inline int gcd(int a, int b) { return b == 0 ? a : gcd(b, a%b); }
 28 inline int lcm(int a, int b) { return a / gcd(a, b)*b; }  //a*b = gcd*lcm
 29 const LL INF = 0x3f3f3f3f3f3f3f3f;
 30 const LL mod = 998244353;
 31 const double eps = 1e-8;
 32 const int inf = 0x3f3f3f3f;
 33 const int maxk = 1e6+5;
 34 const int maxn = 2e5+5;
 35 
 36 int n;
 37 int cost[maxn], to[maxn];
 38 int fa[maxn];
 39 bool vis[maxn];
 40 vector<int> ans;
 41 
 42 int _find(int x)
 43 {
 44     if (x == fa[x])
 45         return fa[x];
 46     return fa[x] = _find(fa[x]);
 47 }
 48 
 49 //将y作为父节点
 50 void merge(int x, int y)
 51 {
 52     int xr = _find(x);
 53     int yr = _find(y);
 54     if (xr != yr)
 55         fa[xr] = y;
 56 }
 57 
 58 int dfs(int x, int y)
 59 {
 60     if (x == y) return cost[x];
 61     return Min(dfs(x, to[y]), cost[y]);
 62 }
 63 
 64 void init()
 65 {
 66     cin >> n;
 67     
 68     foe(i, 1, n) scanf("%d", &cost[i]);
 69     foe(i, 1, n)
 70     {
 71         scanf("%d", &to[i]);
 72         fa[i] = i; 
 73         vis[i] = false;
 74     }
 75 }
 76 
 77 int main()
 78 {
 79     #ifndef ONLINE_JUDGE
 80         freopen("input.txt", "r", stdin);
 81     #endif
 82 
 83     init();
 84     //并查集判环
 85     foe(i, 1, n) {
 86         if (_find(to[i]) == _find(i)) {
 87             vis[i] = true;
 88             continue;
 89         }
 90 
 91         merge(i, to[i]);
 92     }
 93 
 94     int ans = 0;
 95     //foe(i, 1, n)
 96     //    printf("%d\n", vis[i]);
 97     foe(i, 1, n)
 98         if (vis[i])
 99            ans += dfs(i, to[i]);
100     printf("%d\n", ans);
101     return 0;
102 }
View Code

 

posted @ 2018-08-21 11:07  LBNOQYX  阅读(238)  评论(0编辑  收藏  举报