【Codeforces Round 397】Codeforces 765A-D

来源:互联网 发布:php简历模板免费下载 编辑:程序博客网 时间:2024/06/11 20:53

A. Neverending competitions time limit per test 2 seconds memory limit
per test 512 megabytes input standard input output standard output

There are literally dozens of snooker competitions held each year, and
team Jinotega tries to attend them all (for some reason they prefer
name “snookah”)! When a competition takes place somewhere far from
their hometown, Ivan, Artsem and Konstantin take a flight to the
contest and back.

Jinotega’s best friends, team Base have found a list of their
itinerary receipts with information about departure and arrival
airports. Now they wonder, where is Jinotega now: at home or at some
competition far away? They know that:

this list contains all Jinotega's flights in this year (in arbitrary order),Jinotega has only flown from his hometown to a snooker contest and back,after each competition Jinotega flies back home (though they may attend a competition in one place several times),and finally, at the beginning of the year Jinotega was at home. 

Please help them to determine Jinotega’s location! Input

In the first line of input there is a single integer n: the number of
Jinotega’s flights (1 ≤ n ≤ 100). In the second line there is a string
of 3 capital Latin letters: the name of Jinotega’s home airport. In
the next n lines there is flight information, one flight per line, in
form “XXX->YYY”, where “XXX” is the name of departure airport “YYY” is
the name of arrival airport. Exactly one of these airports is
Jinotega’s home airport.

It is guaranteed that flights information is consistent with the
knowledge of Jinotega’s friends, which is described in the main part
of the statement. Output

If Jinotega is now at home, print “home” (without quotes), otherwise
print “contest”.

A题就是搞笑的。

#include<cstdio>int main(){    int n;    scanf("%d",&n);    if (n&1) printf("contest\n");    else printf("home\n");}

B. Code obfuscation time limit per test 2 seconds memory limit per test 512 megabytes input standard input output standard output

Kostya likes Codeforces contests very much. However, he is very
disappointed that his solutions are frequently hacked. That’s why he
decided to obfuscate (intentionally make less readable) his code
before upcoming contest.

To obfuscate the code, Kostya first looks at the first variable name
used in his program and replaces all its occurrences with a single
symbol a, then he looks at the second variable name that has not been
replaced yet, and replaces all its occurrences with b, and so on.
Kostya is well-mannered, so he doesn’t use any one-letter names before
obfuscation. Moreover, there are at most 26 unique identifiers in his
programs.

You are given a list of identifiers of some program with removed
spaces and line breaks. Check if this program can be a result of
Kostya’s obfuscation. Input

In the only line of input there is a string S of lowercase English
letters (1 ≤ |S| ≤ 500) — the identifiers of a program with removed
whitespace characters. Output

If this program can be a result of Kostya’s obfuscation, print “YES”
(without quotes), otherwise print “NO”.

B题注意理解清楚题意。

#include<cstdio>#include<cstring>char s[510];int vis[510];int main(){    int n,m=0,x;    scanf("%s",s+1);    n=strlen(s+1);    for (int i=1;i<=n;i++)        if (!vis[i])        {            x=s[i]-'a'+1;            if (x!=m+1)            {                printf("NO\n");                return 0;            }            m++;            for (int j=i;j<=n;j++) if (s[j]==s[i]) vis[j]=1;        }    printf("YES\n");}

C. Table Tennis Game 2 time limit per test 2 seconds memory limit per
test 512 megabytes input standard input output standard output

Misha and Vanya have played several table tennis sets. Each set
consists of several serves, each serve is won by one of the players,
he receives one point and the loser receives nothing. Once one of the
players scores exactly k points, the score is reset and a new set
begins.

Across all the sets Misha scored a points in total, and Vanya scored b
points. Given this information, determine the maximum number of sets
they could have played, or that the situation is impossible.

Note that the game consisted of several complete sets. Input

The first line contains three space-separated integers k, a and b
(1 ≤ k ≤ 109, 0 ≤ a, b ≤ 109, a + b > 0). Output

If the situation is impossible, print a single number -1. Otherwise,
print the maximum possible number of sets. Examples

要场数最多最好全k:0赢,剩下单独的分给另外一个人赢的一场。

#include<cstdio>int main(){    int k,a,b;    scanf("%d%d%d",&k,&a,&b);    if ((a<k&&b%k)||(b<k&&a%k)) printf("-1\n");    else printf("%d\n",a/k+b/k);}

D. Artsem and Saunders time limit per test 2 seconds memory limit per
test 512 megabytes input standard input output standard output

Artsem has a friend Saunders from University of Chicago. Saunders
presented him with the following problem.

Let [n] denote the set {1, …, n}. We will also write f: [x] → [y]
when a function f is defined in integer points 1, …, x, and all its
values are integers from 1 to y.

Now then, you are given a function f: [n] → [n]. Your task is to find
a positive integer m, and two functions g: [n] → [m], h: [m] → [n],
such that g(h(x)) = x for all , and h(g(x)) = f(x) for all , or
determine that finding these is impossible. Input

The first line contains an integer n (1 ≤ n ≤ 105).

The second line contains n space-separated integers — values
f(1), …, f(n) (1 ≤ f(i) ≤ n). Output

If there is no answer, print one integer -1.

Otherwise, on the first line print the number m (1 ≤ m ≤ 106). On the
second line print n numbers g(1), …, g(n). On the third line print m
numbers h(1), …, h(m).

If there are several correct answers, you may output any of them. It
is guaranteed that if a valid answer exists, then there is an answer
satisfying the above restrictions.

对于任何f(x)=w,必须存在p使得h(p)=w
又因为g(h(p))=p,即g(w)=p,所以h(g(w))=w
又因为h(g(w))=f(w),所以f(w)=w
反过来,只要满足上面的要求,一定可以找到可行解。只需要对每个w分配一个p

#include<cstdio>#include<algorithm>using namespace std;int f[100010],g[100010],h[100010],vis[100010],n;int main(){    int m=0;    scanf("%d",&n);    for (int i=1;i<=n;i++) scanf("%d",&f[i]);    for (int i=1;i<=n;i++)    {        if (!vis[f[i]])        {            if (f[f[i]]!=f[i])            {                printf("-1\n");                return 0;            }            vis[f[i]]=1;            g[f[i]]=++m;            h[m]=f[i];        }        g[i]=g[f[i]];    }    printf("%d\n",m);    for (int i=1;i<=n;i++) printf("%d%c",g[i],i==n?'\n':' ');    for (int i=1;i<=m;i++) printf("%d%c",h[i],i==m?'\n':' ');}
0 0