Codeforces Round #263 (Div. 2)
第一次cf,就是第一题開始没怎么理解题意,还好三题都对了.
A. Appleman and Easy Task
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Toastman came up with a very easy task. He gives it to Appleman, but Appleman doesn't know how to solve it. Can you help him?
Given a n × n checkerboard. Each cell of the board has either character 'x', or character 'o'. Is it true that each cell of the board has even number of adjacent cells with 'o'? Two cells of the board are adjacent if they share a side.
Input
The first line contains an integer n (1 ≤ n ≤ 100). Then n lines follow containing the description of the checkerboard. Each of them contains n characters (either 'x' or 'o') without spaces.
Output
Print "YES" or "NO" (without the quotes) depending on the answer to the problem.
Sample test(s)
input
3
xxo
xox
oxx
output
YES
input
4
xxxo
xoxo
oxox
xxxx
output
NO
/********************** author : Grant Yuan time : 2014/8/26 sourcr : cf 263 (Div. 2) ***********************/ #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> using namespace std; int n,ans; char a[107][107]; int check(int i,int j) { if(i>=0&&i<n&&j>=0&&j<n&&a[i][j]=='o') return 1; return 0; } int main() { while(~scanf("%d",&n)) { int ans; int flag=1; memset(a,0,sizeof(a)); for(int i=0;i<n;i++) scanf("%s",a[i]); for(int i=0;i<n;i++) for(int j=0;j<n;j++) { ans=0; if(check(i-1,j)) ans++; if(check(i,j-1)) ans++; if(check(i,j+1)) ans++; if(check(i+1,j)) ans++; if(ans%2!=0) flag=0; } if(flag) printf("YES\n"); else printf("NO\n"); } return 0; }
/********************** author : Grant Yuan time : 2014/8/26 sourcr : cf 263 (Div. 2) ***********************/ #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; int n,m; long long a[27]; char s[100007]; int main() { while(~scanf("%d%d",&n,&m)) { long long ans=0; memset(a,0,sizeof(a)); scanf("%s",s); int l=strlen(s); for(int i=0;i<l;i++) { a[s[i]-'A']++; } sort(a,a+26); for(int i=26;i>=0;i--) { if(m>=a[i]) {ans+=(long long)a[i]*a[i];m-=a[i];} else {ans+=(long long)m*m;break;} } printf("%I64d\n",ans); } return 0; }
C. Appleman and Toastman
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Appleman and Toastman play a game. Initially Appleman gives one group of n numbers to the Toastman, then they start to complete the following tasks:
Each time Toastman gets a group of numbers, he sums up all the numbers and adds this sum to the score. Then he gives the group to the Appleman.
Each time Appleman gets a group consisting of a single number, he throws this group out. Each time Appleman gets a group consisting of more than one number, he splits the group into two non-empty groups (he can do it in any way) and gives each of them to Toastman.
After guys complete all the tasks they look at the score value. What is the maximum possible value of score they can get?
Input
The first line contains a single integer n (1 ≤ n ≤ 3·105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the initial group that is given to Toastman.
Output
Print a single integer — the largest possible score.
Sample test(s)
input
3
3 1 5
output
26
input
1
10
output
10
Note
Consider the following situation in the first example. Initially Toastman gets group [3, 1, 5] and adds 9 to the score, then he give the group to Appleman. Appleman splits group [3, 1, 5] into two groups: [3, 5] and [1]. Both of them should be given to Toastman.
When Toastman receives group [1], he adds 1 to score and gives the group to Appleman (he will throw it out). When Toastman receives group [3, 5], he adds 8 to the score and gives the group to Appleman. Appleman splits [3, 5] in the only possible way: [5] and
[3]. Then he gives both groups to Toastman. When Toastman receives [5], he adds 5 to the score and gives the group to Appleman (he will throws it out). When Toastman receives [3], he adds 3 to the score and gives the group to Appleman (he will throws it out).
Finally Toastman have added 9 + 1 + 8 + 5 + 3 = 26 to the score. This is the optimal sequence of actions.
/********************** author : Grant Yuan time : 2014/8/26 sourcr : cf 263 (Div. 2) ***********************/ #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> #include<queue> using namespace std; long long ans; long long q[3*100007]; int n; long long sum; int main() { while(~scanf("%d",&n)){ ans=0;sum=0; for(int i=0;i<n;i++) { scanf("%I64d",&q[i]); } sort(q,q+n); int cnt=2; for(int i=0;i<n;i++) { ans+=q[i]*cnt; cnt++; } ans-=q[n-1]; printf("%I64d\n",ans); } return 0; }