天天看点

Educational Codeforces Round 49 (Rated for Div. 2)

好心痛啊!!虽然B题没开longlong,只把ans开了longlongWA了,C题最后一分钟交的没交上,赛后交了一发秒过,但是还是说明了菜哇!赛后找了下LY大佬,发现D题也很简单,而且她一个半小时就做完了四道,真的是膜拜大佬!

在此夸一下帅气漂亮优秀卓越的LY大佬!!!太酷了。

题目地址:http://codeforces.com/contest/1027

写下题解:

A. Palindromic Twist

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a string s

consisting of n lowercase Latin letters. n

is even.

For each position i

(1≤i≤n) in string s

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 s

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 s

a palindrome by applying the aforementioned changes to every position. Print "YES" if string s

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 T

(1≤T≤50

) — the number of strings in a testcase.

Then 2T

lines follow — lines (2i−1) and 2i of them describe the i-th string. The first line of the pair contains a single integer n (2≤n≤100, n is even) — the length of the corresponding string. The second line of the pair contains a string s, consisting of n

lowercase Latin letters.

Output

Print T

lines. The i-th line should contain the answer to the i-th string of the input. Print "YES" if it's possible to make the i

-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".

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=100000;
typedef long long LL;
int main()
{
    int t;string h;
    int n;
    int flag=0;
    scanf("%d",&t);
    while(t--){
        flag=0;
        scanf("%d",&n);
        cin>>h;
        for(int i=0;i<n/2;i++){
            int j=n-1-i;
            if(h[i]=='a'){
                if(h[j]!='a'){
                if(h[j]!='c')
                {
                    printf("NO\n"); flag=1;
                    break;
                }
                }
            }
            else if(h[i]=='z'){
                if(h[j]!='z'){
                if(h[j]!='x'){
                    printf("NO\n"); flag=1;
                    break;
                }
                }
            }
            else{
                int ii=int(h[i]);
                int jj=int(h[j]);
                if(h[i]!=h[j]){
                if(abs(ii-jj)!=2){
                    printf("NO\n");
                    flag=1;
                    break;
                }
            }
        }
    }
    if(flag)
        continue;
     printf("YES\n");
}
}
/*
5
6
abccba
2
cf
4
adfa
8
abaazaba
2
ml
*/
           

B:

B. Numbers on the Chessboard

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a chessboard of size n×n

. It is filled with numbers from 1 to n2 in the following way: the first ⌈n22⌉ numbers from 1 to ⌈n22⌉ are written in the cells with even sum of coordinates from left to right from top to bottom. The rest n2−⌈n22⌉ numbers from ⌈n22⌉+1 to n2 are written in the cells with odd sum of coordinates from left to right from top to bottom. The operation ⌈xy⌉ means division x by y

rounded up.

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

and the right board is the chessboard which is given for n=5

.

Educational Codeforces Round 49 (Rated for Div. 2)

You are given q

queries. The i-th query is described as a pair xi,yi. The answer to the i-th query is the number written in the cell xi,yi (xi is the row, yi is the column). Rows and columns are numbered from 1 to n

.

Input

The first line contains two integers n

and q (1≤n≤109, 1≤q≤105

) — the size of the board and the number of queries.

The next q

lines contain two integers each. The i-th line contains two integers xi,yi (1≤xi,yi≤n) — description of the i

-th query.

Output

For each query from 1

to q print the answer to this query. The answer to the i-th query is the number written in the cell xi,yi (xi is the row, yi is the column). Rows and columns are numbered from 1 to n. Queries are numbered from 1 to q

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.

longlong哇!!

而且 我写麻烦了。

我的AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
LL n;
LL ans,N;
LL xpp(LL x,LL y){
    ans=0;
      if(n%2==0){
            int num=n/2;///一行num个
            if((x+y)%2==0)
            {
                ans+=num*(x-1);
                if(y%2==0)
                    ans+=y/2;
                else
                    ans+=y/2+1;
            }
            else{///x+y是ji数
                if((n*n)%2!=0)
                    N=n*n/2+1;
                else
                    N=n*n/2;
                ans+=N;///从N开始加
                ans+=num*(x-1);///一行num个
                if(y%2==0){///x是ji
                    ans+=y/2;
                }
                else{///x是偶
                    ans+=(y/2+1);
                }
            }
        }
        else{
            if((n*n)%2!=0)
                    N=n*n/2+1;
            else
                    N=n*n/2;

            int num2=n/2+1+n/2;///两行的
            if((x+y)%2==0){
                if((x-1)%2==0)
                    ans+=((x-1)/2)*num2;
                else{
                    ans+=((x-2)/2*num2)+(n/2+1);
                }///前边x-1行算完了
                if(x%2==0)
                    ans+=y/2;
                else{
                    ans+=y/2+1;
                }
            }
            else{
                ans+=N;
                if((x-1)%2==0)
                    ans+=(x-1)/2*num2;
                else{
                    ans+=(x-2)/2*num2+(n/2+1);
                }///前边x-1行算完了
                if(x%2==1){///y是ou
                    ans+=y/2;
                }
                else{
                    ans+=y/2;
                }
            }
        }
        return ans;
}
int main()
{
    LL q,x,y;
    scanf("%lld",&n);
    scanf("%lld",&q);
    while(q--){
        scanf("%lld%lld",&x,&y);
        cout<<xpp(x,y)<<endl;
    }
}
           

简单代码:

#include<iostream>
using namespace std;
typedef long long ll;
ll n,q,a,b,ans;
int main(){
	cin>>n>>q;
	while(q--){
		cin>>a>>b;
		ans=(a-1)*n+b+1;
		if((a+b)%2)ans+=n*n;
		cout<<ans/2<<endl;
	}
}
           

真的是。。。

C:由公式推得,就是x/y+y/x最小值就好了

代码:

#include<bits/stdc++.h>
using namespace std;
int a[1000010];
int cmp(int x,int y){
    return x>y;
}
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
        }
        sort(a+1,a+1+n,cmp);
        int last1=-1;
        int flag=0;
        double minn=100000;
        int shou,wei;
        for(int i=2;i<=n;i++){

           /* if(a[i]==a[i-1] && a[i]==a[i+1] &&a[i]==a[i+2] && i+2<=n )
            {
                flag=1;
                cout<<a[i]<<" "<<a[i]<<" "<<a[i]<<" "<<a[i]<<endl;
                break;
            }
            */
            if(a[i]==a[i-1]){

                if(last1!=-1)
                {
                    double lala=a[i]*1.0/last1+last1*1.0/a[i];
                    if(minn>lala)
                    {
                        shou=a[i];
                        wei=last1;
                        minn=lala;
                   ///     cout<<"shou:"<<shou<<"   wei:"<<wei<<endl;
                    }
                    last1=a[i];
                }
                else
                    last1=a[i];
                i++;
            }
        }
        if(!flag){
            cout<<shou<<" "<<shou<<" "<<wei<<" "<<wei<<endl;
        }
    }
    return 0;
}
           

还有没有A的第四道题,另外写一篇博客。。。D题链接