码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces710

时间:2016-08-24 17:00:30      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:

【未完待续】

A

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

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string>
 4 #include<string.h>
 5 #include<iostream>
 6 #include<algorithm>
 7 #include<queue>
 8 #include<math.h>
 9 #include<vector>
10 #include<map>
11 #include<set>
12 #define il inline
13 #define re register
14 using namespace std;
15 string a;
16 int main(){
17     cin>>a;
18     if(a[0]==a||a[0]==h){
19         if(a[1]==1||a[1]==8)
20             cout<<"3";
21         else cout<<"5";
22     }
23     else{
24         if(a[1]==1||a[1]==8)
25             cout<<"5";
26         else cout<<"8";
27     }
28     return 0;
29 }

B

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·10^5) — the number of points on the line.

The second line contains n integers xi ( - 10^9 ≤ xi ≤ 10^9) — 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.

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string>
 4 #include<string.h>
 5 #include<iostream>
 6 #include<algorithm>
 7 #include<queue>
 8 #include<math.h>
 9 #include<vector>
10 #include<map>
11 #include<set>
12 #define il inline
13 #define re register
14 using namespace std;
15 int n,a[1000001];
16 int main(){
17     scanf("%d",&n);
18     for(int i=1;i<=n;i++)
19         scanf("%d",&a[i]);
20     sort(a+1,a+n+1);
21     if(n&1){
22         cout<<a[n/2+1];
23     }
24     else{
25         cout<<a[n/2];
26     }
27     return 0;
28 }

C

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.

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string>
 4 #include<string.h>
 5 #include<iostream>
 6 #include<algorithm>
 7 #include<queue>
 8 #include<math.h>
 9 #include<vector>
10 #include<map>
11 #include<set>
12 #define il inline
13 #define re register
14 using namespace std;
15 int x=1,y,cnt=1,n,a[101][101];
16 int main(){
17     cin>>n;y=n/2+1;
18     while(cnt<=n*n){
19         a[x][y]=(cnt++);
20         if(x==1){
21             if(y==n){
22                 x=2;y=n;
23             }
24             else{
25                 x=n;y++;
26             }
27         }
28         else if(y==n){
29             x--;y=1;
30         }
31         else{
32             if(a[x-1][y+1]>0){
33                 x++;
34             }
35             else{
36                 x--;y++;
37             }
38         }
39     }
40     for(int i=1;i<=n;i++){
41         for(int j=1;j<=n;j++)
42             cout<<a[i][j]<<" ";
43         cout<<endl;
44     }
45     return 0;
46 }

E

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

可以花费x的代价使得一个数加一或者减一,或者y的代价使得他乘2,问达到n需要多少的代价

这道题嘛,非常巧妙,看dp方程就ok了,主要是利用了减一之前必须乘2的性质【废话】

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string>
 4 #include<string.h>
 5 #include<iostream>
 6 #include<algorithm>
 7 #include<queue>
 8 #include<math.h>
 9 #include<vector>
10 #include<map>
11 #include<set>
12 #define il inline
13 #define re register
14 using namespace std;
15 typedef long long ll;
16 ll f[10000001];
17 int n,x,y;
18 int main(){
19     scanf("%d%d%d",&n,&x,&y);
20     for(int i=1;i<=n;i++){
21         f[i]=min(f[i-1]+x,f[(i+1)>>1]+y+x*(i&1));
22     }
23     printf("%I64d",f[n]);
24     return 0;
25 }

 

Codeforces710

标签:

原文地址:http://www.cnblogs.com/ExiledPoet/p/5803303.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!