Educational Codeforces Round 34 题解

来源:互联网 发布:mac 返回快捷键 编辑:程序博客网 时间:2024/06/06 10:06

这里写图片描述

A. Hungry Student Problem

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Ivan’s classes at the university have just finished, and now he wants to go to the local CFK cafe and eat some fried chicken.

CFK sells chicken chunks in small and large portions. A small portion contains 3 chunks; a large one — 7 chunks. Ivan wants to eat exactly x chunks. Now he wonders whether he can buy exactly this amount of chicken.

Formally, Ivan wants to know if he can choose two non-negative integers a and b in such a way that a small portions and b large ones contain exactly x chunks.

Help Ivan to answer this question for several values of x!

Input
The first line contains one integer n (1 ≤ n ≤ 100) — the number of testcases.

The i-th of the following n lines contains one integer xi (1 ≤ xi ≤ 100) — the number of chicken chunks Ivan wants to eat.

Output
Print n lines, in i-th line output YES if Ivan can buy exactly xi chunks. Otherwise, print NO.

Example
input
2
6
5
output
YES
NO
Note
In the first example Ivan can buy two small portions.

In the second example Ivan cannot buy exactly 5 chunks, since one small portion is not enough, but two small portions or one large is too much.

暴力枚举3和7的数量
#include<bits/stdc++.h>using namespace std;typedef long long ll;const int maxn = 1e5+5;int n;int a[1005];int main(){//    freopen("data.txt","r",stdin);//    ios_base::sync_with_stdio(false);//    cin >> T;    cin >> n;    int t;    for(int i=0;i<n;i++)    {        cin >> t;        int f = 0;        for(int k=0;k<=t;k++)        {            if(t-k*3<0)            {                break;            }            if((t-k*3)%7==0)            {                f=1;                break;            }        }        if(f)        {            cout <<"YES"<<endl;        }        else        {            cout <<"NO"<<endl;        }    }    return 0;}

B. The Modcrab

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Vova is again playing some computer game, now an RPG. In the game Vova’s character received a quest: to slay the fearsome monster called Modcrab.

After two hours of playing the game Vova has tracked the monster and analyzed its tactics. The Modcrab has h2 health points and an attack power of a2. Knowing that, Vova has decided to buy a lot of strong healing potions and to prepare for battle.

Vova’s character has h1 health points and an attack power of a1. Also he has a large supply of healing potions, each of which increases his current amount of health points by c1 when Vova drinks a potion. All potions are identical to each other. It is guaranteed that c1 > a2.

The battle consists of multiple phases. In the beginning of each phase, Vova can either attack the monster (thus reducing its health by a1) or drink a healing potion (it increases Vova’s health by c1; Vova’s health can exceed h1). Then, if the battle is not over yet, the Modcrab attacks Vova, reducing his health by a2. The battle ends when Vova’s (or Modcrab’s) health drops to 0 or lower. It is possible that the battle ends in a middle of a phase after Vova’s attack.

Of course, Vova wants to win the fight. But also he wants to do it as fast as possible. So he wants to make up a strategy that will allow him to win the fight after the minimum possible number of phases.

Help Vova to make up a strategy! You may assume that Vova never runs out of healing potions, and that he can always win.

Input
The first line contains three integers h1, a1, c1 (1 ≤ h1, a1 ≤ 100, 2 ≤ c1 ≤ 100) — Vova’s health, Vova’s attack power and the healing power of a potion.

The second line contains two integers h2, a2 (1 ≤ h2 ≤ 100, 1 ≤ a2 < c1) — the Modcrab’s health and his attack power.

Output
In the first line print one integer n denoting the minimum number of phases required to win the battle.

Then print n lines. i-th line must be equal to HEAL if Vova drinks a potion in i-th phase, or STRIKE if he attacks the Modcrab.

The strategy must be valid: Vova’s character must not be defeated before slaying the Modcrab, and the monster’s health must be 0 or lower after Vova’s last action.

If there are multiple optimal solutions, print any of them.

Examples
input
10 6 100
17 5
output
4
STRIKE
HEAL
STRIKE
STRIKE
input
11 6 100
12 5
output
2
STRIKE
STRIKE
Note
In the first example Vova’s character must heal before or after his first attack. Otherwise his health will drop to zero in 2 phases while he needs 3 strikes to win.

In the second example no healing needed, two strikes are enough to get monster to zero health and win with 6 health left.

贪心,先给自己加血,然后攻击敌人,首先要算出攻击敌人的最小次数,然后再算出加血的最小次数.
#include<bits/stdc++.h>using namespace std;typedef long long ll;const int maxn = 1e5+5;int h1,a1,c1;int h2,a2;int main(){//    freopen("data.txt","r",stdin);//    ios_base::sync_with_stdio(false);    cin >> h1>>a1>>c1;    cin >> h2>>a2;    int x;    if(h2%a1)    {        x = h2/a1+1;    }    else    {        x = h2/a1;    }    int add=0;    int dec = (x-1)*a2;    if(h1>dec)    {        add = 0;    }    else    {        while(h1<=dec)        {            h1 +=c1;            dec+=a2;            add++;        }    }    cout << add+x<<endl;    while(add--)    {        cout <<"HEAL"<<endl;    }    while(x--)    {        cout <<"STRIKE"<<endl;    }    return 0;}

C. Boxes Packing

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Mishka has got n empty boxes. For every i (1 ≤ i ≤ n), i-th box is a cube with side length ai.

Mishka can put a box i into another box j if the following conditions are met:

i-th box is not put into another box;
j-th box doesn’t contain any other boxes;
box i is smaller than box j (ai < aj).
Mishka can put boxes into each other an arbitrary number of times. He wants to minimize the number of visible boxes. A box is called visible iff it is not put into some another box.

Help Mishka to determine the minimum possible number of visible boxes!

Input
The first line contains one integer n (1 ≤ n ≤ 5000) — the number of boxes Mishka has got.

The second line contains n integers a1, a2, …, an (1 ≤ ai ≤ 109), where ai is the side length of i-th box.

Output
Print the minimum possible number of visible boxes.

Examples
input
3
1 2 3
output
1
input
4
4 2 4 3
output
2
Note
In the first example it is possible to put box 1 into box 2, and 2 into 3.

In the second example Mishka can put box 2 into box 3, and box 4 into box 1.

先排序,然后从最小的盒子放,暴力枚举个数,放过的不放
#include<bits/stdc++.h>using namespace std;typedef long long ll;const int maxn = 1e5+5;int n;int a[10005];int f[10005];int main(){//    freopen("data.txt","r",stdin);    ios_base::sync_with_stdio(false);    cin >> n;    for(int i=0;i<n;i++)    {        cin >> a[i];    }    sort(a,a+n);    int ans = 0;    int cur=0;    for(int i=0;i<n;i++)    {        if(f[i]) continue;        ans++;        cur = a[i];        f[i]=1;        for(int j=i+1;j<n;j++)        {            if(a[j]>cur&&f[j]==0)            {                cur = a[j];                f[j]=1;            }        }    }    cout << ans<<endl;    return 0;}

D. Almost Difference

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Let’s denote a function

You are given an array a consisting of n integers. You have to calculate the sum of d(ai, aj) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.

Input
The first line contains one integer n (1 ≤ n ≤ 200000) — the number of elements in a.

The second line contains n integers a1, a2, …, an (1 ≤ ai ≤ 109) — elements of the array.

Output
Print one integer — the sum of d(ai, aj) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.

Examples
input
5
1 2 3 1 3
output
4
input
4
6 6 5 5
output
0
input
4
6 6 4 4
output
-8
Note
In the first example:

d(a1, a2) = 0;
d(a1, a3) = 2;
d(a1, a4) = 0;
d(a1, a5) = 2;
d(a2, a3) = 0;
d(a2, a4) = 0;
d(a2, a5) = 0;
d(a3, a4) =  - 2;
d(a3, a5) = 0;
d(a4, a5) = 2.

有1e6的数据,想到n*log(n)的复杂度,从第一个开始枚举,用map存储其前面出现过数的个数,n*log(n)查询即可,具体看代码.ps这题的ans超出了long long的范围,这个点坑了好多人,虽然比赛时发了声明,但还是有好多人没注意到.
#include<bits/stdc++.h>using namespace std;typedef long long ll;const int maxn = 1e5+5;map<ll,ll> mm;ll a[2000006];int main(){//    freopen("data.txt","r",stdin);//    ios_base::sync_with_stdio(false);    int n;    cin >> n;    long double ans= 0;    long double sum = 0;    for(int i=1;i<=n;i++)    {        cin >> a[i];        ans += ( a[i]*( i-1- mm[ a[i]-1 ] - mm[ a[i]+1 ]-mm[a[i]] ) - (sum -( mm[ a[i]-1 ]*(a[i]-1) + mm[ a[i]+1 ]*(a[i]+1) + mm[a[i]] *(a[i]) )));        mm[ a[i] ]++;        sum += a[i];    }     cout << fixed << setprecision(0) << ans << endl;    return 0;}

E. Swapping Characters

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
We had a string s consisting of n lowercase Latin letters. We made k copies of this string, thus obtaining k identical strings s1, s2, …, sk. After that, in each of these strings we swapped exactly two characters (the characters we swapped could be identical, but they had different indices in the string).

You are given k strings s1, s2, …, sk, and you have to restore any string s so that it is possible to obtain these strings by performing aforementioned operations. Note that the total length of the strings you are given doesn’t exceed 5000 (that is, k·n ≤ 5000).

Input
The first line contains two integers k and n (1 ≤ k ≤ 2500, 2 ≤ n ≤ 5000, k · n ≤ 5000) — the number of strings we obtained, and the length of each of these strings.

Next k lines contain the strings s1, s2, …, sk, each consisting of exactly n lowercase Latin letters.

Output
Print any suitable string s, or -1 if such string doesn’t exist.

Examples
input
3 4
abac
caab
acba
output
acab
input
3 4
kbbu
kbub
ubkb
output
kbub
input
5 4
abcd
dcba
acbd
dbca
zzzz
output
-1
Note
In the first example s1 is obtained by swapping the second and the fourth character in acab, s2 is obtained by swapping the first and the second character, and to get s3, we swap the third and the fourth character.

In the second example s1 is obtained by swapping the third and the fourth character in kbub, s2 — by swapping the second and the fourth, and s3 — by swapping the first and the third.

In the third example it’s impossible to obtain given strings by aforementioned operations.

这是一道比较巧妙的题,我刚开始的想法是暴力枚举,但是n^3的时间复杂度显然是超时的,后来灵光一闪,发现如果两个字符串不同,那么不相同的字符串必有一个是从原串置换过来的,只需确定不相同的位置,然后对两个字符串都枚举另一个位置,再进行验证一下,就OK了,这样的时间复杂度为n^2.
#include<bits/stdc++.h>using namespace std;typedef long long ll;const int maxn = 1e5+5;int k,n;string s;vector<string> a;int num[30];int tnum[30];int er  = 0;bool check(string st,int xx){    for(int j=0;j<k;j++)    {        int ctt = 0;        if(j==xx) continue;        for(int ii=0;ii<n;ii++)        {            if(st[ii]!=a[j][ii])            {                ctt++;                if(ctt>=3)                {                    return false;                }            }        }        if(ctt==1) return false;        if(ctt==0&&er==0) return false;    }    return true;}int main(){//    freopen("data.txt","r",stdin);    ios_base::sync_with_stdio(false);    cin >> k>> n;    for(int i=0;i<k;i++)    {        cin >> s;        a.push_back(s);    }    for(int i=0;i<n;i++)    {        num[a[0][i]-'a']++;        if(num[a[0][i]-'a']>=2)        {            er = 1;        }    }    for(int i=0;i<k;i++)    {        memset(tnum,0,sizeof(tnum));        for(int j=0;j<n;j++)        {            tnum[a[i][j]-'a']++;        }        for(int j=0;j<=26;j++)        {            if(num[j]!=tnum[j])            {                cout << -1<<endl;                return 0;            }        }    }    int f = 0;    int pos = -1;    for(int i=1;i<k;i++)    {        if(a[i]!=a[0])        {            f=1;            pos = i;            break;        }    }    if(!f)    {        swap(a[0][0],a[0][1]);        cout << a[0]<<endl;        return 0;    }    int px = 0;    for(int i=0;i<n;i++)    {        if(a[0][i]!=a[pos][i])        {            px = i;            break;        }    }    for(int i=0;i<n;i++)    {        if(i==px)        {            continue;        }        swap(a[0][i],a[0][px]);        if(check(a[0],0))        {            cout << a[0]<<endl;            return 0;        }        swap(a[0][i],a[0][px]);    }//    cout <<"flag"<<endl;    for(int i=0;i<n;i++)    {        if(i==px)        {            continue;        }        swap(a[pos][i],a[pos][px]);        if(check(a[pos],pos))        {            cout << a[pos]<<endl;            return 0;        }        swap(a[pos][i],a[pos][px]);    }    cout << -1<<endl;    return 0;}