关闭

Educational Codeforces Round 16

标签: codeforces
55人阅读 评论(0) 收藏 举报
分类:

抽空做了一下这些题。。。F题真的是不会了。。。

A. King Moves

The only king stands on the standard chess board. You are given his position in format “cd”, where c is the column from ‘a’ to ‘h’ and d is the row from ‘1’ to ‘8’. Find the number of moves permitted for the king.
这里写图片描述

King moves from the position e4

Input

The only line contains the king’s position in the format “cd”, where ‘c’ is the column from ‘a’ to ‘h’ and ‘d’ is the row from ‘1’ to ‘8’.
Output

Print the only integer x — the number of moves permitted for the king.

Example
Input

e4

Output

8

题解:简单模拟。。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LiangJiaJun main
using namespace std;
char s[4];
int x,y;
bool genc(int a){
     return (a == 8 || a == 1);
}
int LiangJiaJun(){
    scanf("%s",s+1);
    x=s[1]-'a'+1;y=s[2]-'0';
    if(genc(x)&&genc(y)){
        puts("3");
        return 0;
    }
    if( (genc(x)&&!genc(y)) || (!genc(x)&&genc(y)) ){
        puts("5");return 0;
    }
    if(!genc(x)&&!genc(y)){
        puts("8");return 0;
    }
    return 0;
}

B. Optimal Point on a Line

You are given n points on a line with their coordinates xi. Find the point x so the sum of distances to the given points is minimal.
Input

The first line contains integer n (1 ≤ n ≤ 3·105) — the number of points on the line.

The second line contains n integers xi ( - 109 ≤ xi ≤ 109) — the coordinates of the given n points.
Output

Print the only integer x — the position of the optimal point on the line. If there are several optimal points print the position of the leftmost one. It is guaranteed that the answer is always the integer.

Example
Input

4
1 2 3 4

Output

2

题意:给N个数轴上的点,从中找到一个点,使得该点到其他所有点的距离之和最小。该点在数轴上的位置。(距离相同的话取数轴坐标靠左边的点)
题解:这很显然先排个序,然后最中间的一个或两个点就是答案了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#define LiangJiaJun main
using namespace std;
int n,a[300004];
long long xp,xr;
int LiangJiaJun(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    sort(a+1,a+n+1);
    if(n % 2 == 0){
        for(int i=1;i<=n;i++)xp += abs(a[i]-a[n/2]);
        for(int i=1;i<=n;i++)xr += abs(a[i]-a[n/2+1]);
        if(xr < xp) printf("%d\n",a[n/2+1]);
        else printf("%d\n",a[n/2]);
    }
    else {
        printf("%d\n",a[n/2+1]);
    }
    return 0;
}

C. Magic Odd Square

Find an n × n matrix with different numbers from 1 to n2, so the sum in each row, column and both main diagonals are odd.
Input

The only line contains odd integer n (1 ≤ n ≤ 49).
Output

Print n lines with n integers. All the integers should be different and from 1 to n2. The sum in each row, column and both main diagonals should be odd.

Examples
Input

1

Output

1

Input

3

Output

2 1 4
3 5 7
6 9 8

题意:NOIP2015第一题。。。。。叫你填一个n*n大小的幻方,每个数字必须不重复,n是奇数。且所有数字必须在1到n^2之内。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#define LiangJiaJun main
using namespace std;
int a[54][54],n;
int LiangJiaJun(){
    scanf("%d",&n);
    a[1][n/2+1] = 1;
    int nowx = 1, nowy = n/2+1 , k = 2;
    while(k<=n*n){
        bool t=0;
        int p=nowx-1,q=nowy+1;
        if(p>0&&q<=n){
            if(a[p][q]==0)a[p][q]=k;else t=1;
            if(!t){nowx=p;nowy=q;++k;continue;}
        }
        if(p<=0&&q<=n){
            p=n;
            if(a[p][q]==0)a[p][q]=k;else t=1;
            if(!t){nowx=p;nowy=q;++k;continue;}
        }
        if(p>0&&q>n){
            q=1;
            if(a[p][q]==0)a[p][q]=k;else t=1;
            if(!t){nowx=p;nowy=q;++k;continue;}
        }
        p=nowx+1;q=nowy;
        nowx=p;nowy=q;
        a[p][q]=k;
        k++;
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            printf("%d ",a[i][j]);
        }
        printf("\n");
    }
    return 0;
}

D. Two Arithmetic Progressions

You are given two arithmetic progressions: a1k + b1 and a2l + b2. Find the number of integers x such that L ≤ x ≤ R and x = a1k’ + b1 = a2l’ + b2, for some integers k’, l’ ≥ 0.
Input

The only line contains six integers a1, b1, a2, b2, L, R (0 < a1, a2 ≤ 2·10^9,  - 2·10^9 ≤ b1, b2, L, R ≤ 2·10^9, L ≤ R).
Output

Print the desired number of integers x.

Examples
Input

2 0 3 3 5 21

Output

3

Input

2 4 3 0 6 17

Output

2

题意:求一共有多少个整数符合x( L<=x<=R ),使得a1 * k+b1 = a2 * l + b2 = x 的两个解k和l都为非负整数。
题解:这种题目说实话好恶心。说白了就是解方程嘛。。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#define LiangJiaJun main
using namespace std;
long long a1,b1,a2,b2,L,R;
long long gcd(long long x,long long y){return y==0?x:gcd(y,x%y);}
long long res1,res2,x,lcm,temp;
bool ok=0;
int LiangJiaJun(){
    scanf("%I64d%I64d%I64d%I64d%I64d%I64d",&a1,&b1,&a2,&b2,&L,&R);
    if(a1<a2){
        swap(a1,a2);
        swap(b1,b2);
    }
    L=max(L,max(b1,b2));
    lcm=(a1*a2)/gcd(a1,a2);
    res1=(b1%a1+a1)%a1;
    res2=(b2%a2+a2)%a2;
    temp=x=L+(a1+res1-(L%a1))%a1;
    while(x<=temp+lcm && x<=R){
        if(((x%a2)+a2)%a2 == res2){
            ok=1; break;
        }
        else x+=a1;
    }
    if(ok)printf("%I64d\n",(R-x)/lcm+1);
    else printf("0\n");
    return 0;
}

E. Generate a String

zscoder wants to generate an input file for some programming competition problem.

His input is a string consisting of n letters ‘a’. He is too lazy to write a generator so he will manually generate the input in a text editor.

Initially, the text editor is empty. It takes him x seconds to insert or delete a letter ‘a’ from the text file and y seconds to copy the contents of the entire text file, and duplicate it.

zscoder wants to find the minimum amount of time needed for him to create the input file of exactly n letters ‘a’. Help him to determine the amount of time needed to generate the input.
Input

The only line contains three integers n, x and y (1 ≤ n ≤ 107, 1 ≤ x, y ≤ 109) — the number of letters ‘a’ in the input file and the parameters from the problem statement.
Output

Print the only integer t — the minimum amount of time needed to generate the input file.

Examples
Input

8 1 1

Output

4

Input

8 1 10

Output

8

题意:可以花费x的代价使得一个数加一或者减一,或者y的代价使得他乘2,开始这个数是0,问达到n需要多少的代价
题解:有一个显然的结论,如果我要进行减一操作,那么我之前一定有一次乘二的操作。。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#define LiangJiaJun main
using namespace std;
long long f[20000004];
int n,x,y;
int LiangJiaJun(){
    scanf("%d%d%d",&n,&x,&y);
    for(int i=1;i<=n;i++)f[i]=min(f[i-1]+x,f[(i+1)/2]+y+x*(i%2==1));
    cout<<f[n]<<endl;
    return 0;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:6824次
    • 积分:806
    • 等级:
    • 排名:千里之外
    • 原创:78篇
    • 转载:1篇
    • 译文:0篇
    • 评论:4条
    最新评论