Educational Codeforces Round 49 (Rated for Div. 2) ABCD

A. Palindromic Twist

You are given a string ss consisting of nn lowercase Latin letters. nn is even.

For each position ii (1in1≤i≤n) in string ss you are required to change the letter on this position either to the previous letter in alphabetic order or to the next one (letters 'a' and 'z' have only one of these options). Letter in every position must be changed exactly once.

For example, letter 'p' should be changed either to 'o' or to 'q', letter 'a' should be changed to 'b' and letter 'z' should be changed to 'y'.

That way string "codeforces", for example, can be changed to "dpedepqbft" ('c' → 'd', 'o' → 'p', 'd' → 'e', 'e' → 'd', 'f' → 'e', 'o' →'p', 'r' → 'q', 'c' → 'b', 'e' → 'f', 's' → 't').

String ss is called a palindrome if it reads the same from left to right and from right to left. For example, strings "abba" and "zz" are palindromes and strings "abca" and "zy" are not.

Your goal is to check if it's possible to make string ss a palindrome by applying the aforementioned changes to every position. Print "YES" if string ss can be transformed to a palindrome and "NO" otherwise.

Each testcase contains several strings, for each of them you are required to solve the problem separately.

Input

The first line contains a single integer TT (1T501≤T≤50) — the number of strings in a testcase.

Then 2T2T lines follow — lines (2i1)(2i−1) and 2i2i of them describe the ii-th string. The first line of the pair contains a single integer nn (2n1002≤n≤100, nn is even) — the length of the corresponding string. The second line of the pair contains a string ss, consisting of nnlowercase Latin letters.

Output

Print TT lines. The ii-th line should contain the answer to the ii-th string of the input. Print "YES" if it's possible to make the ii-th string a palindrome by applying the aforementioned changes to every position. Print "NO" otherwise.

Example
input
Copy
5
6
abccba
2
cf
4
adfa
8
abaazaba
2
ml
output
Copy
YES
NO
YES
NO
NO
Note

The first string of the example can be changed to "bcbbcb", two leftmost letters and two rightmost letters got changed to the next letters, two middle letters got changed to the previous letters.

The second string can be changed to "be", "bg", "de", "dg", but none of these resulting strings are palindromes.

The third string can be changed to "beeb" which is a palindrome.

The fifth string can be changed to "lk", "lm", "nk", "nm", but none of these resulting strings are palindromes. Also note that no letter can remain the same, so you can't obtain strings "ll" or "mm".

模拟题,没看到必须变,wa了一次

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 char s[110];
 4 bool ok(int x, int y) {
 5     set<char> st;
 6     if(s[x] == 'a') {
 7         st.insert('b');
 8     } else if(s[x] == 'z') {
 9         st.insert('y');
10     } else {
11         st.insert(s[x]+1);
12         st.insert(s[x]-1);
13     }
14     if(s[y] == 'a') {
15         if(st.count('b')) return true;
16     } else if(s[y] == 'z') {
17         if(st.count('y')) return true;
18     } else {
19         if(st.count(s[y]-1) || st.count(s[y] + 1)) return true;
20     }
21     return false;
22 }
23 int main() {
24     int t, n;
25     cin >> t;
26     while(t--) {
27         cin >> n >> s;
28         bool flag = true;
29         for(int i = 0; i < n/2; i ++) {
30             if(!ok(i, n-i-1)) {
31                 flag = false;
32             }
33         }
34         if(flag) printf("YES\n");
35         else printf("NO\n");
36     }
37     return 0;
38 }

B. Numbers on the Chessboard

You are given a chessboard of size n×nn×n. It is filled with numbers from 11 to n2n2 in the following way: the first n22⌈n22⌉ numbers from 11 to n22⌈n22⌉are written in the cells with even sum of coordinates from left to right from top to bottom. The rest n2n22n2−⌈n22⌉ numbers from n22+1⌈n22⌉+1 to n2n2are written in the cells with odd sum of coordinates from left to right from top to bottom. The operation xy⌈xy⌉ means division xx by yy rounded up.

For example, the left board on the following picture is the chessboard which is given for n=4n=4 and the right board is the chessboard which is given for n=5n=5.

You are given qq queries. The ii-th query is described as a pair xi,yixi,yi. The answer to the ii-th query is the number written in the cell xi,yixi,yi (xixiis the row, yiyi is the column). Rows and columns are numbered from 11 to nn.

Input

The first line contains two integers nn and qq (1n1091≤n≤109, 1q1051≤q≤105) — the size of the board and the number of queries.

The next qq lines contain two integers each. The ii-th line contains two integers xi,yixi,yi (1xi,yin1≤xi,yi≤n) — description of the ii-th query.

Output

For each query from 11 to qq print the answer to this query. The answer to the ii-th query is the number written in the cell xi,yixi,yi (xixi is the row, yiyi is the column). Rows and columns are numbered from 11 to nn. Queries are numbered from 11 to qq in order of the input.

Examples
input
Copy
4 5
1 1
4 4
4 3
3 2
2 4
output
Copy
1
8
16
13
4
input
Copy
5 4
2 1
4 2
3 3
3 4
output
Copy
16
9
7
20
Note

Answers to the queries from examples are on the board in the picture from the problem statement.

全称if判断过的 0.0

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 
 5 int main() {
 6     ll n, q, x, y;
 7     cin >> n >> q;
 8     while(q--) {
 9         cin >> x >> y;
10         ll ans = 0;
11         if((x+y)&1) {
12             if(n&1) {
13                 if(x&1) {
14                     ans += (x-1)*n/2;
15                     ans += y/2;
16                 } else {
17                     ans += (x-2)*n/2;
18                     ans += n/2;
19                     ans += (y+1)/2;
20                 }
21             } else {
22                 if(x&1) {
23                     ans += (x-1)*n/2;
24                     ans += y/2;
25                 } else {
26                     ans += (x-2)*n/2;
27                     ans += n/2;
28                     ans += (y+1)/2;
29                 }
30             }
31             if(n&1) ans += n*n/2+1;
32             else ans += (n*n)/2;
33         } else {
34             if(n&1) {
35                 if(x&1) {
36                     ans += (x-1)*n/2;
37                     ans += (y+1)/2;
38                 } else {
39                     ans += (x-2)*n/2;
40                     ans += n/2+1;
41                     ans += y/2;
42                 }
43             } else {
44                 if(x&1) {
45                     ans += (x-1)*n/2;
46                     ans += (y+1)/2;
47                 } else {
48                     ans += (x-2)*n/2;
49                     ans += n/2;
50                     ans += y/2;
51                 }
52             }
53         }
54         printf("%lld\n",ans);
55     }
56     return 0;
57 }

C. Minimum Value Rectangle

You have nn sticks of the given lengths.

Your task is to choose exactly four of them in such a way that they can form a rectangle. No sticks can be cut to pieces, each side of the rectangle must be formed by a single stick. No stick can be chosen multiple times. It is guaranteed that it is always possible to choose such sticks.

Let SS be the area of the rectangle and PP be the perimeter of the rectangle.

The chosen rectangle should have the value P2SP2S minimal possible. The value is taken without any rounding.

If there are multiple answers, print any of them.

Each testcase contains several lists of sticks, for each of them you are required to solve the problem separately.

Input

The first line contains a single integer TT (T1T≥1) — the number of lists of sticks in the testcase.

Then 2T2T lines follow — lines (2i1)(2i−1) and 2i2i of them describe the ii-th list. The first line of the pair contains a single integer nn (4n1064≤n≤106) — the number of sticks in the ii-th list. The second line of the pair contains nn integers a1,a2,,ana1,a2,…,an (1aj1041≤aj≤104) — lengths of the sticks in the ii-th list.

It is guaranteed that for each list there exists a way to choose four sticks so that they form a rectangle.

The total number of sticks in all TT lists doesn't exceed 106106 in each testcase.

Output

Print TT lines. The ii-th line should contain the answer to the ii-th list of the input. That is the lengths of the four sticks you choose from the ii-th list, so that they form a rectangle and the value P2SP2S of this rectangle is minimal possible. You can print these four lengths in arbitrary order.

If there are multiple answers, print any of them.

Example
input
Copy
3
4
7 2 2 7
8
2 8 1 4 8 2 1 5
5
5 5 5 5 5
output
Copy
2 7 7 2
2 2 1 1
5 5 5 5
Note

There is only one way to choose four sticks in the first list, they form a rectangle with sides 22 and 77, its area is 27=142⋅7=14, perimeter is 2(2+7)=182(2+7)=18. 1821423.14318214≈23.143.

The second list contains subsets of four sticks that can form rectangles with sides (1,2)(1,2), (2,8)(2,8) and (1,8)(1,8). Their values are 622=18622=18, 20216=2520216=25 and 1828=40.51828=40.5, respectively. The minimal one of them is the rectangle (1,2)(1,2).

You can choose any four of the 55 given sticks from the third list, they will form a square with side 55, which is still a rectangle with sides (5,5)(5,5).

数学题,让长和宽之比趋近于1时,值最小

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 const int N = 1e6+10;
 5 ll a[N], cnt = 0, b[N];
 6 int main() {
 7     int t, n;
 8     cin >> t;
 9     while(t--) {
10         scanf("%d", &n);
11         cnt = 0;
12         for(int i = 1; i <= n; i ++) scanf("%lld", &a[i]);
13         sort(a+1,a+1+n);
14         for(int i = 1; i < n; i ++) {
15             if(a[i] == a[i+1]) {
16                 b[cnt++] = a[i];
17                 i++;
18             }
19         }
20         ll ans1 = b[0], ans2 = b[1];
21         for(int i = 2; i < cnt; i ++) {
22             if(ans1*b[i] < ans2*b[i-1]) {
23                 ans1 = b[i-1];
24                 ans2 = b[i];
25             }
26         }
27         printf("%lld %lld %lld %lld\n",ans1,ans1,ans2,ans2);
28     }
29     return 0;
30 }

D. Mouse Hunt

Medicine faculty of Berland State University has just finished their admission campaign. As usual, about 80%80% of applicants are girls and majority of them are going to live in the university dormitory for the next 44 (hopefully) years.

The dormitory consists of nn rooms and a single mouse! Girls decided to set mouse traps in some rooms to get rid of the horrible monster. Setting a trap in room number ii costs cici burles. Rooms are numbered from 11 to nn.

Mouse doesn't sit in place all the time, it constantly runs. If it is in room ii in second tt then it will run to room aiai in second t+1t+1 without visiting any other rooms inbetween (i=aii=ai means that mouse won't leave room ii). It's second 00 in the start. If the mouse is in some room with a mouse trap in it, then the mouse get caught into this trap.

That would have been so easy if the girls actually knew where the mouse at. Unfortunately, that's not the case, mouse can be in any room from 11 to nn at second 00.

What it the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from?

Input

The first line contains as single integers nn (1n21051≤n≤2⋅105) — the number of rooms in the dormitory.

The second line contains nn integers c1,c2,,cnc1,c2,…,cn (1ci1041≤ci≤104) — cici is the cost of setting the trap in room number ii.

The third line contains nn integers a1,a2,,ana1,a2,…,an (1ain1≤ai≤n) — aiai is the room the mouse will run to the next second after being in room ii.

Output

Print a single integer — the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from.

Examples
input
Copy
5
1 2 3 2 10
1 3 4 3 3
output
Copy
3
input
Copy
4
1 10 2 10
2 4 2 2
output
Copy
10
input
Copy
7
1 1 1 1 1 1 1
2 2 2 3 6 7 6
output
Copy
2
Note

In the first example it is enough to set mouse trap in rooms 11 and 44. If mouse starts in room 11 then it gets caught immideately. If mouse starts in any other room then it eventually comes to room 44.

In the second example it is enough to set mouse trap in room 22. If mouse starts in room 22 then it gets caught immideately. If mouse starts in any other room then it runs to room 22 in second 11.

Here are the paths of the mouse from different starts from the third example:

  • 1221→2→2→…;
  • 222→2→…;
  • 3223→2→2→…;
  • 43224→3→2→2→…;
  • 56765→6→7→6→…;
  • 6766→7→6→…;
  • 7677→6→7→…;

So it's enough to set traps in rooms 22 and 66.

求环中的最小值之和。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int N = 2e5+10;
 4 int c[N], a[N], fa[N];
 5 int vis[N];
 6 int main() {
 7     int t, n;
 8     cin >> n;
 9     for(int i = 1; i <= n; i ++) fa[i] = i;
10     for(int i = 1; i <= n; i ++) cin >> c[i];
11     for(int i = 1; i <= n; i ++) cin >> a[i];
12     int ans = 0;
13     for(int i = 1; i <= n; i ++) {
14         if(!vis[i]) {
15             int tmp = i;
16             while(true) {
17                 if(vis[tmp]) break;
18                 vis[tmp] = 2;
19                 tmp = a[tmp];
20             }
21             if(vis[tmp] == 2) {
22                 int cnt = c[tmp];
23                 int tmp1 = a[tmp];
24                 while(tmp1 != tmp) {
25                     cnt = min(c[tmp1], cnt);
26                     tmp1 = a[tmp1];
27                 }
28                 ans += cnt;
29             }
30             tmp = i;
31             while(vis[tmp] != 1) {
32                 vis[tmp] = 1;
33                 tmp = a[tmp];
34 
35             }
36         }
37     }
38     cout << ans << endl;
39     return 0;
40 
41 }

 

posted @ 2018-08-19 16:16  starry_sky  阅读(376)  评论(0编辑  收藏  举报