codeforces Round #568(Div.2)A B C

原文链接: http://www.cnblogs.com/whocarethat/p/11066587.html

  有点菜,只写出了三道。活不多说,上题开干。

A. Ropewalkers

Polycarp decided to relax on his weekend and visited to the performance of famous ropewalkers: Agafon, Boniface and Konrad.

The rope is straight and infinite in both directions. At the beginning of the performance, Agafon, Boniface and Konrad are located in positions aabbcc dd

Ropewalkers can walk on the rope. In one second, only one ropewalker can change his position. Every ropewalker can change his position exactly by 1111

You should find the minimum duration (in seconds) of the performance. In other words, find the minimum number of seconds needed so that the distance between each pair of ropewalkers can be greater or equal to dd

Ropewalkers can walk to negative coordinates, due to the rope is infinite to both sides.

Input

The only line of the input contains four integers aabbccdd1a,b,c,d1091≤a,b,c,d≤109

Output

Output one integer — the minimum duration (in seconds) of the performance.

Examples
input
Copy
5 2 6 3
output
Copy
2
input
Copy
3 1 5 6
output
Copy
8
input
Copy
8 3 3 2
output
Copy
2
input
Copy
2 3 10 4
output
Copy
3
Note

In the first example: in the first two seconds Konrad moves for 2 positions to the right (to the position 88|52|=3|5−2|=3|28|=6|2−8|=6|58|=3|5−8|=3d=3d=3, so the performance could be finished within 2 seconds.

  题目大意:三个人在一条无限延伸的绳子上(每个人的位置坐标可以抽象成一个点来处理,点可以重合), 输入他们的位置,以及最少间隔距离。要求输出每两个人之间

都达到或超过最小间隔距离需要多少秒,每秒只能有一个人动。

  思路:对位置进行排序,每次只能动一个人,那就保证排序后中间的人不动,分别计算两边的人与中间的人的距离,如果超过或等于最小间隔距离则不动,否则就移动

达到最小间隔距离。代码如下:

#include <iostream>
#include <algorithm>
#define LL long long
int const max_n=1002;
using namespace std;

int main()
{
    int a[3],d,sum=0;
    cin>>a[0]>>a[1]>>a[2]>>d;
    sort(a,a+3);
    if(a[1]-a[0]<d)sum+=abs(d-a[1]+a[0]);
    if(a[2]-a[1]<d)sum+=abs(d-a[2]+a[1]);
    printf("%d\n",sum);
    return 0;
}
B. Email from Polycarp
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Methodius received an email from his friend Polycarp. However, Polycarp's keyboard is broken, so pressing a key on it once may cause the corresponding symbol to appear more than once (if you press a key on a regular keyboard, it prints exactly one symbol).

For example, as a result of typing the word "hello", the following words could be printed: "hello", "hhhhello", "hheeeellllooo", but the following could not be printed: "hell", "helo", "hhllllooo".

Note, that when you press a key, the corresponding symbol must appear (possibly, more than once). The keyboard is broken in a random manner, it means that pressing the same key you can get the different number of letters in the result.

For each word in the letter, Methodius has guessed what word Polycarp actually wanted to write, but he is not sure about it, so he asks you to help him.

You are given a list of pairs of words. For each pair, determine if the second word could be printed by typing the first one on Polycarp's keyboard.

Input

The first line of the input contains one integer nn (1n1051≤n≤105) — the number of pairs to check. Further input contains nn descriptions of pairs.

The first line of each description contains a single non-empty word ss consisting of lowercase Latin letters. The second line of the description contains a single non-empty word tt consisting of lowercase Latin letters. The lengths of both strings are not greater than 106106.

It is guaranteed that the total length of all words ss in the input is not greater than 106106. Also, it is guaranteed that the total length of all words tt in the input is not greater than 106106.

Output

Output nn lines. In the ii-th line for the ii-th pair of words ss and tt print YES if the word tt could be printed by typing the word ss. Otherwise, print NO.

Examples
input
Copy
4
hello
hello
hello
helloo
hello
hlllloo
hello
helo
output
Copy
YES
YES
NO
NO
input
Copy
5
aa
bb
codeforces
codeforce
polycarp
poolycarpp
aaaa
aaaab
abcdefghijklmnopqrstuvwxyz
zabcdefghijklmnopqrstuvwxyz
output
Copy
NO
NO
YES
NO
NO
  题目大意:Methodius想发一封邮件给他的朋友(应还要打印出来),但是它的键盘坏了,请你判断从键盘输入的内容,是否能按照他的想法打印出来(打印规则是可以输入重复的字符,打印机会
过滤掉多余的重复字符,但是不能有不同的字符出现,这也是判断正确与否的关键),第一行输入从键盘输入的内容,第二行输入想打印的内容,能正确打印输出“YES”,否则输出“NO”
  思路:我最开始想的是遍历所有想打印的字符,与输入的匹配,匹配成功就能输出,但是踩了个坑(assdf asd),即当输入的多余字符在想打印的内容匹配完之后还有不想打印的字符,wa了
几次才反应过了。代码如下:
#include <iostream>
#include <algorithm>
int const max_n=1002;
using namespace std;
int main()
{
    int n;
    string a,b;
    scanf("%d",&n);
    while(n--)
    {
        cin>>a>>b;
        if(a.length()>b.length()||a[a.length()-1]!=b[b.length()-1]
        ||a[0]!=b[0]){printf("NO\n");continue;}//判断首尾字符,减少运算
        bool judge=false;//定义bool方便盘点
        int j=0,k=0;//j是键盘输入串的下标,k是匹配的字符数(当匹配足够时,能打印)
        for(int i=0;i<a.length();i++)
        {
            while(j<b.length())
            {
                if(a[i]==b[j]){k++;j++;break;}//匹配一个字符,跳出该次循环,计数器加一
                if(b[j]!=a[i-1]){judge=true;break;}//出现多余字符,不符合打印要求
                j++;
            }
            if(judge){judge=false;break;}//不符合打印要求时退出循环
            if(j>=b.length())break;//当键盘输入的字符比对完后退出循环
        }
        if(k==a.length())judge=true;//若想打印的内容全部匹配成功时
        if(judge)while(j<b.length())//当能打印时,判断输入的串在匹配 后面有没有多余字符
        {
            if(a[a.length()-1]!=b[j]){judge=false;break;}
            j++;
        }
        if(judge)printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}
C1. Exam in BerSU (easy version)
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The only difference between easy and hard versions is constraints.

A session has begun at Beland State University. Many students are taking exams.

Polygraph Poligrafovich is going to examine a group of nn students. Students will take the exam one-by-one in order from 11-th to nn-th. Rules of the exam are following:

  • The ii-th student randomly chooses a ticket.
  • if this ticket is too hard to the student, he doesn't answer and goes home immediately (this process is so fast that it's considered no time elapses). This student fails the exam.
  • if the student finds the ticket easy, he spends exactly titi minutes to pass the exam. After it, he immediately gets a mark and goes home.

Students take the exam in the fixed order, one-by-one, without any interruption. At any moment of time, Polygraph Poligrafovich takes the answer from one student.

The duration of the whole exam for all students is MMmaxtiMmaxti≤M

For each student iiii

For each student iii1i1 some student jj should leave, then while finding the answer for i2i2 (i2>i1i2>i1) the student jj student does not have to go home.

Input

The first line of the input contains two integers nn and MM (1n1001≤n≤100, 1M1001≤M≤100) — the number of students and the total duration of the exam in minutes, respectively.

The second line of the input contains nn integers titi (1ti1001≤ti≤100) — time in minutes that ii-th student spends to answer to a ticket.

It's guaranteed that all values of titi are not greater than MM.

Output

Print nn numbers: the ii-th number must be equal to the minimum number of students who have to leave the exam in order to ii-th student has enough time to pass the exam.

Examples
input
Copy
7 15
1 2 3 4 5 6 7
output
Copy
0 0 0 0 0 2 3 
input
Copy
5 100
80 40 40 40 60
output
Copy
0 1 1 2 3 
Note

The explanation for the example 1.

Please note that the sum of the first five exam times does not exceed M=15M=15 (the sum is 1+2+3+4+5=151+2+3+4+5=15). Thus, the first five students can pass the exam even if all the students before them also pass the exam. In other words, the first five numbers in the answer are 00.

In order for the 66-th student to pass the exam, it is necessary that at least 22 students must fail it before (for example, the 33-rd and 44-th, then the 66-th will finish its exam in 1+2+5+6=141+2+5+6=14 minutes, which does not exceed MM).

In order for the 77-th student to pass the exam, it is necessary that at least 33 students must fail it before (for example, the 22-nd, 55-th and 66-th, then the 77-th will finish its exam in 1+3+4+7=151+3+4+7=15 minutes, which does not exceed MM).

  题目大意:有n个学生排队依次进行考试(一次只能有一名同学进行考试,俺也不知道为啥,俺也不敢问),考试时间共计m,每个考生考试需要若干时间,考试时间结束后,没有时间考试的同学就不能考了。输入分两行,第一行为学生人数和考试时间,第二行输入每个考生考试需要多少时间。要去输出按照输入的考试时间来看,该同学如果想通过考试,他前面最少有几个同学不能参加考试(因为如果每个人都考的话,时间不够用)。题目保证数据正确(不会有某个学生通过考试的时间比考试总时间还长的情况)。

  思路:用一个结构体存储学生的考试时间,以及该同学通过考试最少不能参加考试的人数,在输入时将考试时间分给一个数组,依输入顺序进行挨个判断。分两种情况,一种是该同学和他之前的人都参加考试,也都能通过(时间足够),此时最少不参加考试人数为0;另一种情况即是考试时间不够用的时候了,此时,对数组排序(排序的人,应该是这位不能参与考试的同学前面的所有人,让他们中一部分人让出考试名额,让这位同学考试),优先让占用时间长的同学退出考试,直至遍历到的这位同学有足够时间通过考试,记录下有多少同学退出考试。代码如下:

#include <iostream>
#include <algorithm>
int const max_n=102;
using namespace std;
struct node {
    int a,b;//分别是考试时间,最少不能通过考试人数
}num[max_n];
int main()
{
    int n,m,x[max_n];
    scanf("%d %d",&n,&m);
    for(int i=0;i<n;i++){scanf("%d",&num[i].a);x[i]=num[i].a;}//输入的同时,给数组赋值
    int sum=0;//某个同学之前的所有人都参加考试花费的时间
    for(int i=0;i<n;i++)
    {
        sum+=num[i].a;//时间累加
        num[i].b=0;//先设置不能通过考试人数为零
        if(sum>m&&sum!=m){//当第i+1同学考试时间不够时
            int s=0,t=sum;//s为最小放弃考试机会的人
            sort(x,x+i);//将第i+1同学前的所有人考试时间排序
            t-=num[i].a;//把该同学的考试时间从已使用时间内去掉,
            for(int j=i-1;j>=0;j--)//从时间最长的同学开始
            {        
                t-=x[j];//已使用时间,逐个去掉占用时间长的铜须
                s++;//计数器加1
                if(t+num[i].a<=m)break;//当第i+1名同学有足够时间参与考试时,退出本次循环
            }
            num[i].b=s;
        }
    }
    for(int i=0;i<n;i++)printf("%d ",num[i].b);
    return 0;
}

转载于:https://www.cnblogs.com/whocarethat/p/11066587.html

猜你喜欢

转载自blog.csdn.net/weixin_30391339/article/details/94871255