Codeforces Round #387 (Div. 2)

来源:互联网 发布:c语言编程简单乘法 编辑:程序博客网 时间:2024/06/01 20:40


A. Display Size
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

A big company decided to launch a new series of rectangular displays, and decided that the display must have exactly n pixels.

Your task is to determine the size of the rectangular display — the number of lines (rows) of pixels a and the number of columns of pixelsb, so that:

  • there are exactly n pixels on the display;
  • the number of rows does not exceed the number of columns, it means a ≤ b;
  • the difference b - a is as small as possible.
Input

The first line contains the positive integer n (1 ≤ n ≤ 106) — the number of pixels display should have.

Output

Print two integers — the number of rows and columns on the display.

Examples
input
8
output
2 4
input
64
output
8 8
input
5
output
1 5
input
999999
output
999 1001
Note

In the first example the minimum possible difference equals 2, so on the display should be 2 rows of 4 pixels.

In the second example the minimum possible difference equals 0, so on the display should be 8 rows of 8 pixels.

In the third example the minimum possible difference equals 4, so on the display should be 1 row of 5 pixels.


A  【水题】:

#include<cstdio>#include<cmath>int main(){    int n;scanf("%d",&n);    int x;    x=0;    for (int i=1;i*i<=n;i++)    {        if (n%i==0)        {            x=i;        }    }    printf("%d %d\n",x,n/x);    return 0;}


B. Mammoth's Genome Decoding
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

The process of mammoth's genome decoding in Berland comes to its end!

One of the few remaining tasks is to restore unrecognized nucleotides in a found chain s. Each nucleotide is coded with a capital letter of English alphabet: 'A', 'C', 'G' or 'T'. Unrecognized nucleotides are coded by a question mark '?'. Thus, s is a string consisting of letters 'A', 'C', 'G', 'T' and characters '?'.

It is known that the number of nucleotides of each of the four types in the decoded genome of mammoth in Berland should be equal.

Your task is to decode the genome and replace each unrecognized nucleotide with one of the four types so that the number of nucleotides of each of the four types becomes equal.

Input

The first line contains the integer n (4 ≤ n ≤ 255) — the length of the genome.

The second line contains the string s of length n — the coded genome. It consists of characters 'A', 'C', 'G', 'T' and '?'.

Output

If it is possible to decode the genome, print it. If there are multiple answer, print any of them. If it is not possible, print three equals signs in a row: "===" (without quotes).

Examples
input
8AG?C??CT
output
AGACGTCT
input
4AGCT
output
AGCT
input
6????G?
output
===
input
4AA??
output
===
Note

In the first example you can replace the first question mark with the letter 'A', the second question mark with the letter 'G', the third question mark with the letter 'T', then each nucleotide in the genome would be presented twice.

In the second example the genome is already decoded correctly and each nucleotide is exactly once in it.

In the third and the fourth examples it is impossible to decode the genom.



B【水题】 :

#include<cstdio>#include<cmath>int main(){    int n;scanf("%d",&n);    char ch[300];    char cc[4];    cc[0]='A';cc[1]='C';cc[2]='G';cc[3]='T';    scanf("%s",ch);    int shu[4];    for (int i=0;i<4;i++)        shu[i]=0;    if (n%4)    {        printf("===\n");    }    else    {        for (int i=0;i<n;i++)        {            if (ch[i]=='A')                shu[0]++;            else if (ch[i]=='C')                shu[1]++;            else if (ch[i]=='G')                shu[2]++;            else if (ch[i]=='T')                shu[3]++;        }        bool fafe=true;        for (int i=0;i<4;i++)            if (shu[i]>n/4)            fafe=false;        if (!fafe)        {            printf("===\n");        }        else        {            for (int i=0;i<n;i++)            {                if (ch[i]=='?')                {                    for (int j=0;j<4;j++)                    {                        if (shu[j]<n/4)                        {                            ch[i]=cc[j];                            shu[j]++;                            break;                        }                    }                }            }            printf("%s\n",ch);        }    }    return 0;}

C. Servers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There are n servers in a laboratory, each of them can perform tasks. Each server has a unique id — integer from 1 to n.

It is known that during the day q tasks will come, the i-th of them is characterized with three integers: ti — the moment in seconds in which the task will come, ki — the number of servers needed to perform it, and di — the time needed to perform this task in seconds. All tiare distinct.

To perform the i-th task you need ki servers which are unoccupied in the second ti. After the servers begin to perform the task, each of them will be busy over the next di seconds. Thus, they will be busy in seconds ti, ti + 1, ..., ti + di - 1. For performing the task, kiservers with the smallest ids will be chosen from all the unoccupied servers. If in the second ti there are not enough unoccupied servers, the task is ignored.

Write the program that determines which tasks will be performed and which will be ignored.

Input

The first line contains two positive integers n and q (1 ≤ n ≤ 1001 ≤ q ≤ 105) — the number of servers and the number of tasks.

Next q lines contains three integers each, the i-th line contains integers tiki and di (1 ≤ ti ≤ 1061 ≤ ki ≤ n1 ≤ di ≤ 1000) — the moment in seconds in which the i-th task will come, the number of servers needed to perform it, and the time needed to perform this task in seconds. The tasks are given in a chronological order and they will come in distinct seconds.

Output

Print q lines. If the i-th task will be performed by the servers, print in the i-th line the sum of servers' ids on which this task will be performed. Otherwise, print -1.

Examples
input
4 31 3 22 2 13 4 3
output
6-110
input
3 23 2 35 1 2
output
33
input
8 61 3 204 2 16 5 510 1 115 3 621 8 8
output
6930-11536
Note

In the first example in the second 1 the first task will come, it will be performed on the servers with ids 12 and 3 (the sum of the ids equals 6) during two seconds. In the second 2 the second task will come, it will be ignored, because only the server 4 will be unoccupied at that second. In the second 3 the third task will come. By this time, servers with the ids 12 and 3 will be unoccupied again, so the third task will be done on all the servers with the ids 123 and 4 (the sum of the ids is 10).

In the second example in the second 3 the first task will come, it will be performed on the servers with ids 1 and 2 (the sum of the ids is 3) during three seconds. In the second 5 the second task will come, it will be performed on the server 3, because the first two servers will be busy performing the first task.




C 【模拟】:

#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>using namespace std;struct node{    int t,k,d,hao,sum;}pp[100200];bool cmp1(node x, node y){    return x.t<y.t;}bool cmp2(node x, node y){    return x.hao<y.hao;}int main(){    int n,q;scanf("%d%d",&n,&q);    for (int i=0;i<q;i++)    {        scanf("%d%d%d",&pp[i].t,&pp[i].k,&pp[i].d);        pp[i].hao=i;pp[i].sum=0;    }    sort(pp,pp+q,cmp1);    int qi[120];    for (int i=0;i<=n;i++)        qi[i]=0;    for (int i=0;i<q;i++)    {        int lp=0;        for (int j=1;j<=n;j++)        {            if (qi[j]<pp[i].t)                lp++;        }        if (lp>=pp[i].k)        {            for (int j=1;j<=n;j++)            {                if (qi[j]<pp[i].t)                {                    pp[i].k--;pp[i].sum+=j;                    qi[j]=pp[i].t+pp[i].d-1;                }                if (pp[i].k==0) break;            }        }        else            pp[i].sum=-1;    }    sort(pp,pp+q,cmp2);    for (int i=0;i<q;i++)        printf("%d\n",pp[i].sum);    return 0;}




D :

orz ------ing------

开始是夏季---还有最后一段区间需要最后考虑---

D. Winter Is Coming
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

The winter in Berland lasts n days. For each day we know the forecast for the average air temperature that day.

Vasya has a new set of winter tires which allows him to drive safely no more than k days at any average air temperature. After k days of using it (regardless of the temperature of these days) the set of winter tires wears down and cannot be used more. It is not necessary that these k days form a continuous segment of days.

Before the first winter day Vasya still uses summer tires. It is possible to drive safely on summer tires any number of days when the average air temperature is non-negative. It is impossible to drive on summer tires at days when the average air temperature is negative.

Vasya can change summer tires to winter tires and vice versa at the beginning of any day.

Find the minimum number of times Vasya needs to change summer tires to winter tires and vice versa to drive safely during the winter. At the end of the winter the car can be with any set of tires.

Input

The first line contains two positive integers n and k (1 ≤ n ≤ 2·1050 ≤ k ≤ n) — the number of winter days and the number of days winter tires can be used. It is allowed to drive on winter tires at any temperature, but no more than k days in total.

The second line contains a sequence of n integers t1, t2, ..., tn ( - 20 ≤ ti ≤ 20) — the average air temperature in the i-th winter day.

Output

Print the minimum number of times Vasya has to change summer tires to winter tires and vice versa to drive safely during all winter. If it is impossible, print -1.

Examples
input
4 3-5 20 -3 0
output
2
input
4 2-5 20 -3 0
output
4
input
10 62 -5 1 3 0 0 -4 -3 1 0
output
3
Note

In the first example before the first winter day Vasya should change summer tires to winter tires, use it for three days, and then change winter tires to summer tires because he can drive safely with the winter tires for just three days. Thus, the total number of tires' changes equals two.

In the second example before the first winter day Vasya should change summer tires to winter tires, and then after the first winter day change winter tires to summer tires. After the second day it is necessary to change summer tires to winter tires again, and after the third day it is necessary to change winter tires to summer tires. Thus, the total number of tires' changes equals four.



#include<cstdio>#include<cstring>#include<algorithm>using namespace std;struct node{    int k,ll,you;}pp[200000];bool a[300000];bool cmp(node x, node y){    if (x.you==y.you)    return x.ll<y.ll;    return x.you>y.you;}int main(){    int n,k,lp=0,aa,kai=0;    scanf("%d%d",&n,&k);    for (int i=1;i<=n;i++)    {        scanf("%d",&aa);        if (aa<0)        {            a[i]=false;            lp++;            if (!kai) kai=i;        }        else        a[i]=true;    }    if (lp>k)    {        printf("-1\n");    }    else if (lp==k)    {        int ans=0;        if (kai)        for (int i=kai;i<=n;i++)        {            if (i==kai) ans++;            else {                if (a[i]!=a[i-1])                    ans++;            }        }        printf("%d\n",ans);    }    else    {        if (!kai)        {            printf("0\n");            return 0;        }        int kk=k-lp;        bool fafe=false;        int kp=0;        a[n+1]=false;        for (int i=kai;i<=n+1;i++)        {            if (a[i])            {                if (!fafe)                {                    pp[kp].k=i;                    fafe=true;                }            }            else            {                if (fafe)                {                    pp[kp].ll=i-pp[kp].k;                    pp[kp].you=1;                    kp++;                    fafe=false;                }            }        }        if (a[n])            pp[kp-1].you=0;        sort(pp,pp+kp,cmp);        for (int i=0;i<kp;i++)        {            if (kk>=pp[i].ll)            {                for (int j=pp[i].k;j<pp[i].k+pp[i].ll;j++)                    a[j]=false;                kk-=pp[i].ll;            }        }        int ans=0;        for (int i=kai;i<=n;i++)        {            if (i==kai) ans++;            else {                if (a[i]!=a[i-1])                    ans++;            }        }        printf("%d\n",ans);    }    return 0;}/*10 62 -5 1 3 0 0 -4 -3 1 0Output4Answer3Checker Logwrong answer expected '3', found '4'*//*50 36 20 17 19 15 17 3 17 5 16 20 18 9 19 18 18 2 -3 11 11 5 15 4 18 16 16 19 11 20 17 2 1 11 14 18 -8 13 17 19 9 9 20 19 20 19 5 12 19 6 9Output5Answer4Checker Logwrong answer expected '4', found '5'*/






E. Comments
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A rare article in the Internet is posted without a possibility to comment it. On a Polycarp's website each article has comments feed.

Each comment on Polycarp's website is a non-empty string consisting of uppercase and lowercase letters of English alphabet. Comments have tree-like structure, that means each comment except root comments (comments of the highest level) has exactly one parent comment.

When Polycarp wants to save comments to his hard drive he uses the following format. Each comment he writes in the following format:

  • at first, the text of the comment is written;
  • after that the number of comments is written, for which this comment is a parent comment (i. e. the number of the replies to this comments);
  • after that the comments for which this comment is a parent comment are written (the writing of these comments uses the same algorithm).
All elements in this format are separated by single comma. Similarly, the comments of the first level are separated by comma.

For example, if the comments look like:

then the first comment is written as "hello,2,ok,0,bye,0", the second is written as "test,0", the third comment is written as "one,1,two,2,a,0,b,0". The whole comments feed is written as: "hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0". For a given comments feed in the format specified above print the comments in a different format:

  • at first, print a integer d — the maximum depth of nesting comments;
  • after that print d lines, the i-th of them corresponds to nesting level i;
  • for the i-th row print comments of nesting level i in the order of their appearance in the Policarp's comments feed, separated by space.
Input

The first line contains non-empty comments feed in the described format. It consists of uppercase and lowercase letters of English alphabet, digits and commas.

It is guaranteed that each comment is a non-empty string consisting of uppercase and lowercase English characters. Each of the number of comments is integer (consisting of at least one digit), and either equals 0 or does not contain leading zeros.

The length of the whole string does not exceed 106. It is guaranteed that given structure of comments is valid.

Output

Print comments in a format that is given in the statement. For each level of nesting, comments should be printed in the order they are given in the input.

Examples
input
hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0
output
3hello test one ok bye two a b 


input
a,5,A,0,a,0,A,0,a,0,A,0
output
2a A a A a A 


input
A,3,B,2,C,0,D,1,E,0,F,1,G,0,H,1,I,1,J,0,K,1,L,0,M,2,N,0,O,1,P,0
output
4A K M B F H L N O C D G I P E J 

Note

The first example is explained in the statements.




E 【模拟】:

#include<cstdio>#include<vector>#include<cstring>#include<algorithm>using namespace std;char ch[2000000];struct node{    int cc;    int shu;    int ff;}pp[300000];vector <int > ve[300000];int shu[300000],wei[300000],kp,ans;void dfs(int ff,int deep,int i){    if (i==kp) return ;    if (ff==0)    {        deep=1;        pp[i].ff=ff;        ans=max(ans,deep);        ve[deep].push_back(i);        dfs(i,deep+1,i+1);    }    else    {        if (pp[ff].shu==0)        {            dfs(pp[ff].ff,deep-1,i);        }        else        {            pp[ff].shu--;            pp[i].ff=ff;            ans=max(ans,deep);            ve[deep].push_back(i);            dfs(i,deep+1,i+1);        }    }}int main(){    scanf("%s",ch);    memset(shu,0,sizeof(shu));    memset(wei,0,sizeof(wei));    int ll=strlen(ch);    ch[ll]=',';    ch[ll+1]=0;    ll++;    int lp=0;    bool fafe=true,f2=true;    char cha[20];int ccc=0,cl=0;    kp=1;memset(cha,0,sizeof(cha));    for (int i=0;i<ll;i++)    {        if (ch[i]!=',')        {            if (fafe)            {                if (f2)                {                    pp[kp].cc=i;                    f2=false;                }            }            else ccc=ccc*10+ch[i]-48;        }        else        {            if (fafe)            {                fafe=false;                cl=0;                memset(cha,0,sizeof(cha));            }            else            {                fafe=true;                f2=true;                pp[kp].shu=ccc;                kp++;ccc=0;            }        }    }    lp=1;int x;    dfs(0,0,1);    printf("%d\n",ans);    for (int i=1;i<=ans;i++)    {        int jl=ve[i].size();        for (int j=0;j<jl;j++)        {            x=pp[ve[i][j]].cc;            while (ch[x]!=',')                printf("%c",ch[x++]);            printf("%c",j==jl-1?'\n':' ');        }    }    return 0;}






0 0
原创粉丝点击