您的位置:首页 > 其它

Szucoder Round #2 (div.2)

2014-12-16 16:35 127 查看
地址:http://codeforces.com/problemset/problem/158/A

A. NextRound
time limitper test
3 seconds
memorylimit per test
256 megabytes
input
standardinput
output
standard output
"Contestant who earns a score equal to or greater than the k-th place finisher's score will advance to the next round, as long asthe contestant earns
a positive score..." — an excerpt from contest rules.

A total of n participants tookpart in the contest (n ≥ k),
and you already knowtheir scores. Calculate how many participants will advance to the next round.

Input

The first line of the input contains two integers n and k (1 ≤ k ≤ n ≤ 50)
separated by a single space.

The second line contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 100),
where ai isthe score earned by the participant who got the i-th
place. The given sequence is non-increasing (that is, for all i from 1 to n - 1 the
following condition is fulfilled: ai ≥ ai + 1).

Output

Output the number of participants who advance to the next round.

Sample test(s)

input

8 5

10 9 8 7 7 7 5 5
output

6
input

4 2

0 0 0 0
output

0
Note

In the first example the participant on the 5th place earned 7 points.As the participant on the 6th place also earned 7 points, there are 6advancers.

In the second example nobody got a positive score.

题目中有一个重要条件是当比赛选手分数大于零时才算数。分别可以通过计数器或下标来计数。

比赛时程序:

#include <iostream>
using namespace std;

int main()
{
int i,n,k,a[105],total=0;
bool flag=false;
scanf("%d%d",&n,&k);
for (i=0;i<n;i++)
{
scanf("%d",&a[i]);
if (a[i]!=0) flag=true;
}
if (flag)
{
for (i=0;i<n;i++)
if (a[i]>=a[k-1] && a[i]>0) total++;
printf("%d\n",total);
}
else printf("0\n");
return 0;
}

参考后的程序:

#include <iostream>
using namespace std;

int main()
{
int n,i,k,a[120],total=0;
scanf("%d%d",&n,&k);
memset(a,0,sizeof(a));
for (i=0;i<n;i++) scanf("%d",&a[i]);
if (a[k-1]>0)
{
for (i=k;i<n;i++)
{
if (a[i]<a[k-1]) break;
total++;
}
printf("%d\n",i);
}
else
{
for (i=k-2;i>=0;i--)
{
if (a[i]>0) break;
}
printf("%d\n",i+1);
}
return 0;
}

教训:
问题中涉及第kth大的数,且必须开数组时,下标从1开始比从0开始会避免不必要的错误。


地址:http://codeforces.com/problemset/problem/71/A

B. Way TooLong Words
time limitper test
2 seconds
memorylimit per test
256megabytes
input
standardinput
output
standard output
Sometimes some words like "localization" or"internationalization" are so long that writing
them many times in one text is quitetiresome.

Let's consider a word too long, if its length is strictlymore than 10 characters. All toolong words should be replaced with a special
abbreviation.

This abbreviation is made like this: we write down the first and thelast letter of a word and between them we write the number of letters betweenthe first and the last letters. That number is in decimal system and doesn'tcontain
any leading zeroes.

Thus, "localization" will be spelt as "l10n", and "internationalization»
will be spelt as "i18n".

You are suggested to automatize the process of changing the words withabbreviations. At that all too long words should be replaced by the abbreviationand the words that are not too long should not undergo any changes.

Input

The first line contains an integer n (1 ≤ n ≤ 100). Each of the following n lines
contains one word. All the words consist of lowercase Latinletters and possess the lengths of from 1 to 100 characters.

Output

Print n lines. The i-th line should contain the result of replacing
of the i-th word from the input data.

Sample test(s)

input

4

word

localization

internationalization

pneumonoultramicroscopicsilicovolcanoconiosis
output

word

l10n

i18n

p43s
解题思路:
字符串处理

比赛时程序:

#include <iostream>
using namespace std;

int main()
{
char st[105];
int n,len;
scanf("%d",&n);
while (n--)
{
scanf("%s",&st);
len=strlen(st);
if (len>10)
{
printf("%c%d%c\n",st[0],len-2,st[len-1]);
}
else printf("%s\n",st);
}
return 0;
}


地址:http://codeforces.com/problemset/problem/268/B

C. Buttons
time limitper test
1 second
memory limitper test
256megabytes
input
standardinput
output
standard output
Manao is trying to open a rather challenging lock. The lock has n buttons on it and to open it, you should press the buttons in acertain order to open
the lock. When you push some button, it either stayspressed into the lock (that means that you've guessed correctly and pushed thebutton that goes next in the sequence), or all pressed buttons return to theinitial position. When all buttons are pressed into
the lock at once, the lockopens.

Consider an example with three buttons. Let's say that the openingsequence is: {2, 3, 1}. If you first press buttons 1 or 3, the buttons unpressimmediately. If you first press button 2, it stays pressed. If you press 1after 2,
all buttons unpress. If you press 3 after 2, buttons 3 and 2 staypressed. As soon as you've got two pressed buttons, you only need to pressbutton 1 to open the lock.

Manao doesn't know the opening sequence. But he is really smart and heis going to act in the optimal way. Calculate the number of times he's got topush a button in order to open the lock in the worst-case scenario.

Input

A single line contains integer n (1 ≤ n ≤ 2000) — the number of buttons
the lock has.

Output

In a single line print the number of times Manao has to push a button inthe worst-case scenario.

Sample test(s)

input

2
output

3
input

3
output

7
Note

Consider the first test sample. Manao can fail his first push and pushthe wrong button. In this case he will already be able to guess the right onewith his second push. And his third push will push the second right button.Thus,
in the worst-case scenario he will only need 3 pushes.

解题思路:
记当已经按对i个按钮时,总共用num秒。还剩n-i个按钮没按,则按第i+1个按钮时要总共要把前i个按n-i遍,然后才按对第i+1个按钮。由于从样例看出按错时不计时。所以sum=sum+i*(n-i)+1

比赛时程序:

#include <iostream>
using namespace std;

int main()
{
int n,i,sum=0;
scanf("%d",&n);

for (i=1;i<=n;i++)
{
sum+=i*(n-i)+1;
}
printf("%d\n",sum);
return 0;
}


地址:http://codeforces.com/problemset/problem/118/B

D. Presentfrom Lena
time limitper test
2 seconds
memorylimit per test
256megabytes
input
standardinput
output
standard output
Vasya's birthday is approaching and Lena decided to sew a patternedhandkerchief to him as a present. Lena chose digits from 0 to nas
the pattern. The digitswill form a rhombus. The largest digit n should be located in the centre. The digits should decrease asthey approach the edges. For example, for n = 5 the
handkerchief pattern should look like that:

0

0 1 0

0 1 2 1 0

0 1 2 3 2 1 0

0 1 2 3 4 3 2 1 0

0 1 2 3 4 5 4 3 2 1 0

0 1 2 3 4 3 2 1 0

0 1 2 3 2 1 0

0 1 2 1 0

0 1 0

0

Your task is to determine the way the handkerchief will look like by thegiven n.

Input

The first line contains the single integer n (2 ≤ n ≤ 9).

Output

Print a picture for the given n. You should strictly observe the number of spaces before the firstdigit on each line. Every two adjacent digits in
the same line should beseparated by exactly one space. There should be no spaces after the last digitat the end of each line.

Sample test(s)

input

2
output

0

0 1 0

0 1 2 1 0

0 1 0

0
input

3
output

0

0 1 0

0 1 2 1 0

0 1 2 3 2 1 0

0 1 2 1 0

0 1 0

0
比赛时程序:
#include <iostream>
using namespace std;

void pr(int);

int main()
{
int n,i,j;
scanf("%d",&n);

for (i=0;i<n;i++)
{
for (j=(n-i)*2;j;j--)
printf(" ");
pr(i);
}
for (i=n;i>=0;i--)
{
for (j=(n-i)*2;j;j--)
printf(" ");
pr(i);
}
return 0;
}

void pr(int x)
{
int i;
printf("0");
if (x!=0)
{
for (i=1;i<x;i++)
printf(" %d",i);
for (i=x;i>=0;i--)
printf(" %d",i);
}
printf("\n");
}

改良程序:
#include <iostream>
using namespace std;

void rep(int);
void rep2(int);
void pr(int);

int n;

int main()
{
scanf("%d",&n);
rep(n);
rep2(0);
return 0;
}

void rep(int i)
{
if (i<0) return;
rep(i-1);
pr(i);
}

void rep2(int i)
{
if (i==n) return;
rep2(i+1);
pr(i);
}

void pr(int j)
{
int i;
for (i=0;i<n-j;i++)
printf("  ");
printf("0");
for (i=1;i<j;i++)
printf(" %d",i);
for (i=j;i>0;i--)
printf(" %d",i);
if (j) printf(" 0");
printf("\n");
}


地址:http://codeforces.com/problemset/problem/4/C

E.Registration system
time limitper test
5 seconds
memorylimit per test
64megabytes
input
standardinput
output
standard output
A new e-mail service "Berlandesk" is going to be opened inBerland in the near future. The site administration wants to launch theirproject as soon as possible, that's why they ask you to help. You're suggestedto implement the
prototype of site registration system. The system should workon the following principle.

Each time a new user wants to register, he sends to the system a requestwith his name. If such a name does
not exist in the system database, it is inserted into thedatabase, and the user gets the response OK, confirming the successfulregistration. If the name already
exists in the system database, the system makes up a newuser name, sends it to the user as a prompt and also inserts the promptinto the database. The new name is formed by the following rule. Numbers,starting with 1, are appended one after another
to name(name1, name2, ...), among these numbers
the least i is found so that namei does not yet exist inthe database.

Input

The first line contains number n (1 ≤ n ≤ 105).
The following n lines contain therequests to the system. Each request is a non-empty line, and consists of notmore than 32 characters, which are all lowercase Latin letters.

Output

Print n lines, which aresystem responses to the requests: OK in case of successfulregistration,
or a prompt with a new name, if the requested name is alreadytaken.

Sample test(s)

input

4

abacaba

acaba

abacaba

acab
output

OK

OK

abacaba1

OK
input

6

first

first

second

second

third

third
output

OK

first1

OK

second1

OK

third1
解题思路:
没学过字典树和图,只能用数组单干,结果超时。

比赛时程序(超时):
#include <iostream>
#include <cstring>
#include <string>
using namespace std;

string st[100005];
int a[100005];

int main()
{
char st1[10];
int n,i,j,l=-1;
scanf("%d",&n);

ios::sync_with_stdio(false);
memset(a,0,sizeof(a));
for (i=0;i<n;i++)
{
cin >> st[++l];
for (j=0;j<l;j++)
if (st[l]==st[j]) break;
if (j==i) printf("OK\n");
else
{
a[j]++;
sprintf(st1,"%d",a[j]);
st[i]+=st1;
cout << st[i] << endl;
}
}
return 0;
}

参考后程序:
#include <iostream>
#include <string>
#include <map>
using namespace std;

int main()
{
ios::sync_with_stdio(false);
int n,temp;
string st;

scanf("%d",&n);
map<string,int> point;
while (n--)
{
cin >> st;
temp=point[st]++;
if (temp==0)
cout<<"OK"<<endl;
else
cout<<st<<temp<<endl;
}
return 0;
}

//使用map
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: