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 (1≤i≤n1≤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 (1≤T≤501≤T≤50) — the number of strings in a testcase.

Then 2T2T lines follow — lines (2i−1)(2i−1) and 2i2i of them describe the ii-th string. The first line of the pair contains a single integer nn (2≤n≤1002≤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

56abccba2cf4adfa8abaazaba2ml

output

Copy

YESNOYESNONO

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 n2−⌈n22⌉n2−⌈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 (1≤n≤1091≤n≤109, 1≤q≤1051≤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 (1≤xi,yi≤n1≤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 51 14 44 33 22 4

output

Copy

1816134

input

Copy

5 42 14 23 33 4

output

Copy

169720

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 (T≥1T≥1) — the number of lists of sticks in the testcase.

Then 2T2T lines follow — lines (2i−1)(2i−1) and 2i2i of them describe the ii-th list. The first line of the pair contains a single integer nn (4≤n≤1064≤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 (1≤aj≤1041≤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

347 2 2 782 8 1 4 8 2 1 555 5 5 5 5

output

Copy

2 7 7 22 2 1 15 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 2⋅7=142⋅7=14, perimeter is 2(2+7)=182(2+7)=18. 18214≈23.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 (1≤n≤2⋅1051≤n≤2⋅105) — the number of rooms in the dormitory.

The second line contains nn integers c1,c2,…,cnc1,c2,…,cn (1≤ci≤1041≤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 (1≤ai≤n1≤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

51 2 3 2 101 3 4 3 3

output

Copy

3

input

Copy

41 10 2 102 4 2 2

output

Copy

10

input

Copy

71 1 1 1 1 1 12 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:

  • 1→2→2→…1→2→2→…;
  • 2→2→…2→2→…;
  • 3→2→2→…3→2→2→…;
  • 4→3→2→2→…4→3→2→2→…;
  • 5→6→7→6→…5→6→7→6→…;
  • 6→7→6→…6→7→6→…;
  • 7→6→7→…7→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 }

原文地址:https://www.cnblogs.com/xingkongyihao/p/9501558.html

时间: 2024-11-06 07:35:37

Educational Codeforces Round 49 (Rated for Div. 2) ABCD的相关文章

Educational Codeforces Round 49 (Rated for Div. 2)

C - Minimum Value Rectangle 题意:给n根木棒,选4根组成长方形,使得这个长方形的周长的平方比上其面积最小. 题解:对那个式子求导,发现对于同一个长来说,是长和宽越接近,上式越小.那么排序之后每个和他附近的一个组装一下就行了. map<int, int> m; vector<int> v; void test_case() { int n; scanf("%d", &n); m.clear(); for(int i = 1; i

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

题目链接:https://codeforces.com/contest/1093 A. Dice Rolling 题意: 有一个号数为2-7的骰子,现在有一个人他想扔到几就能扔到几,现在问需要扔多少次,能使扔出的总和等于xi. 题解: 由于是special judge,模拟一下搞搞就行了= = 代码如下: #include <bits/stdc++.h> using namespace std; int main(){ int t; cin>>t; int n; while(t--

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

A. Digits Sequence Dividing 题意:给你一个数字串,只包含1-9,让你至少分成两段,使得每一段的数字大于前一段的数字: 解:对n特判,如果n为2,那么比较一下两个数字大小,如果n>2,那么就可以直接分成两部分,第一部分1个数字,剩下都为第二部分: #include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=1e3+10; const int mod=9982

Educational Codeforces Round 36 (Rated for Div. 2)

Educational Codeforces Round 36 (Rated for Div. 2) F. Imbalance Value of a Tree You are given a tree T consisting of n vertices. A number is written on each vertex; the number written on vertex i is ai. Let's denote the function I(x,?y) as the differ

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars There are n pillars aligned in a row and numbered from 1 to n. Initially each pillar contains exactly one disk. The i-th pillar contains a disk having radius ai. You can move these disks

Educational Codeforces Round 71 (Rated for Div. 2) A - There Are Two Types Of Burgers

原文链接:https://www.cnblogs.com/xwl3109377858/p/11404050.html Educational Codeforces Round 71 (Rated for Div. 2) A - There Are Two Types Of Burgers There are two types of burgers in your restaurant — hamburgers and chicken burgers! To assemble a hamburg

Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations

原文链接:https://www.cnblogs.com/xwl3109377858/p/11405773.html Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations You are given a sequence of n pairs of integers: (a1,b1),(a2,b2),…,(an,bn). This sequence is called bad if it is

Educational Codeforces Round 36 (Rated for Div. 2) 题解

Educational Codeforces Round 36 (Rated for Div. 2) 题目的质量很不错(不看题解做不出来,笑 Codeforces 920C 题意 给定一个\(1\)到\(n\)组成的数组,只可以交换某些相邻的位置,问是否可以将数组调整为升序的 解题思路 首先如果每个数都能通过交换到它应该到的位置,那么就可以调整为升序的. 但实际上交换是对称的,如果应该在的位置在当前位置前方的数都交换完成,那么整体就是排好序的,因为不可能所有不在相应位置的数都在相应位置的后方.

Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes

Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://codeforces.com/contest/985/problem/E Description Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome w