Codeforces Round #387 (Div. 2) A---D

来源:互联网 发布:知乎周刊合集 编辑:程序博客网 时间:2024/06/04 23:25
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 npixels.

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 pixels b, 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.

题意:给你一个数n,让你求出两个数a,b使a*b==n&&a<=b
思路:水题,直接暴力就可以了,找到a,b记录b-a的差 找到最小的那个即可;
#include<bits/stdc++.h>using namespace std;int main(){long long i,a,b,n;scanf("%I64d",&n);a=1;b=n;long long min=1000001;     for(i=1;i<=sqrt(n*1.0);i++)//需要注意的是这里a最大就只能到根号n 否则 a不满足小于等于b   {  if(n%i==0)       {            if(n/i-i<min)            {       min=n/i-i;       a=i;}     }   }   b=n/a;printf("%I64d %I64d",a,b);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.


题意:给你长度为n的字符串,然后只包含五种字符 A G C T ?问号可以变为任意字母,问你能否是这个字符串中所有字母个数都相等,(每个字母都要出现,问号不剩余)
思路:既然有四种字母 那么我们知道 如果字符串长度不整除4肯定不能满足条件,输出‘===’
如果能整除的话,那么每种字母的个数都要为 n/4,如果有数量大于 n/4的也不可以;然后遍历字符串遇到问号就变为数量不够那个输出就好
#include<bits/stdc++.h>using namespace std;char s[500];int a=0,c=0,g=0,t=0,x=0;int main(){  int i,n;scanf("%d",&n);  scanf("%s",s);  int len=strlen(s);  if(len%4)  {printf("===");  }  else  {  for(i=0;i<len;i++)  {if(s[i]=='A')  a++;  else if(s[i]=='C')  c++;  else if(s[i]=='G')  g++;  else if(s[i]=='T')  t++;  else  x++;}int w=len/4;if(a>w||g>w||c>w||t>w){printf("===");}else{for(i=0;i<len;i++){   if(s[i]=='?'){ if(a<w)      { s[i]='A';      a++;  }  else if(c<w)  { s[i]='C';  c++;  }  else if(g<w)  { s[i]='G';  g++;  }  else if(t<w)  {s[i]='T';  t++;  }}}printf("%s",s);}    }   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 ti are 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, ki servers 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 ≤ n,1 ≤ 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 4will 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.

题意:给定n个服务器编号1—n,q个任务,每个任务都有一个开始时间t,需要的服务器的个数k,和任务完成需要花的时间d,求每个任务完成所需要的服务器的id号的和最小是多少!
思路:直接暴力枚举,开一个标记数组记录每个服务器当前是否空闲,如何确定当前服务器是否空闲呢?
book[i]数组中存储第i个服务器工作的结束时间  即某项任务的开始时间t+结束时间d 则下一次任务来时如果book[i]小于等于开始时间t则满足;否则不满足;
如果遍历找到的有时间完成该任务的人数小于需要完成的人数则不可以;

#include<bits/stdc++.h>using namespace std;int book[110];struct node{int t;int k;int d;}p[100010];int main(){int i,j,n,q;while(scanf("%d %d",&n,&q)!=EOF){    memset(p,0,sizeof(p));    for(i=1;i<=q;i++)scanf("%d %d %d",&p[i].t,&p[i].k,&p[i].d);  memset(book,0,sizeof(book));  for(i=1;i<=q;i++)  {int sum=0;  for(j=1;j<=n;j++)   {if(book[j]<=p[i].t)sum++;      }if(sum<p[i].k)printf("-1\n");else{int ans=0;for(j=1;j<=n;j++){if(book[j]<=p[i].t){book[j]=p[i].t+p[i].d;p[i].k--;ans+=j;}if(p[i].k==0)break;}printf("%d\n",ans);}  }}return 0;}
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. Afterk 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.


题意:冬天轮胎能在任何平均温度下行驶k天,夏天轮胎能在非负平均温度下行驶任意天,现在给出n天数和冬天轮胎可行驶k天,问行驶n天至少更换轮胎多少次。
思路:我就是纯O(n)的暴力,我们知道夏天的轮胎只能在夏天用,冬天的轮胎也可以用在夏天,那么也就是说:对于给定的冬天轮胎可使用的天数,我们先找出其中是冬天的天数,如果小于冬天的天数那么直接输出-1;
那么我们先假设温度非负时候用夏天轮胎,而只有温度为负数的时候才用冬天轮胎,这样算出来的更换次数是最多的。
其次,如果可使用的冬天数足够的话,我们为了减少换轮胎的次数,我们就将剩下的天数按照贪心的思想填在两个冬天之间的地方,也就是说如果剩余的天数可以覆盖两个冬天之间这些天,那么我们可以不用换轮胎直接行使,因为从夏天到冬天在到夏天需要更换次数为2,这样我们就可以减少两次换轮胎的次数;
但是需要注意的是我在处理时由于题目说初始默认为夏天轮胎,所以我让 a[0]=1;又为了避免没有记录最后一个负数后面的区间我让a[n+1]=-1;如果该区间是i---n的话(即最后一个数位夏天)就只能减少一次换轮胎的次数,这就可能会影响到我们最后要找的最小值。所以我们在处理时要考虑最后一个区间,和不考虑最后一个区间取最小值;
比如:
假设我们现在标记的区间长度为 3 2 2(最后一个2位最后的区间长度)次数剩余的可使用冬天轮胎的天数为6;
那么按照我们贪心的思想先填充区间小的   则一共可以减少3次;(因为最后一个只减少一次)
而不填最后一个我们能减少4次;                                  就差这一点点.....大哭
PS:当然也有某巨说 此题的区间长度可以看为重量, 中间的价值看为2  最后一个考虑为 1 然后求解...
#include<bits/stdc++.h>#define N 2*100010using namespace std;int a[N],book[N];int n,k;struct node{int l;int item;}q[N];int cmp(node x,node y){return x.l<y.l;}int main(){ int i,j;while(scanf("%d %d",&n,&k)!=EOF){memset(book,0,sizeof(book));memset(q,0,sizeof(q));    int t=0;    int w=0;    a[0]=1;for(i=1;i<=n;i++){ scanf("%d",&a[i]); if(a[i]<0) {t++; book[w++]=i; }}a[n+1]=-1;book[w]=n+1;if(t>k){printf("-1\n");}else{ k-=t; int ans=0; int num=0; for(i=1;i<=n;i++) { if(a[i]<0&&a[i-1]>=0) ans+=1; if(a[i]<0&&a[i+1]>=0&&i!=n) ans+=1; } if(k==0) {printf("%d\n",ans); } else {    int kk=k; int m=0,sum=ans;  for(j=1;j<=w;j++)    {q[m].l=book[j]-book[j-1]-1;q[m].item=j;m++;} sort(q,q+m,cmp); int flag=0; for(j=0;j<m;j++) { if(q[j].l>0&&q[j].l<=k) {k-=q[j].l; ans-=2; if(q[j].item==w) {flag=1; }}if(q[j].l>0&&q[j].l<=kk&&q[j].item!=w) {kk-=q[j].l; sum-=2;}  } if(flag==1) ans+=1; ans=min(ans,sum); printf("%d\n",ans);  }}}return 0;}


1 0
原创粉丝点击