codeforces Round #568(Div.2)A B C
有点菜,只写出了三道。活不多说,上题开干。
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.
The only line of the input contains four integers aabbccdd1≤a,b,c,d≤1091≤a,b,c,d≤109
Output one integer — the minimum duration (in seconds) of the performance.
5 2 6 3
2
3 1 5 6
8
8 3 3 2
2
2 3 10 4
3
In the first example: in the first two seconds Konrad moves for 2 positions to the right (to the position 88|5−2|=3|5−2|=3|2−8|=6|2−8|=6|5−8|=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=;
using namespace std; int main()
{
int a[],d,sum=;
cin>>a[]>>a[]>>a[]>>d;
sort(a,a+);
if(a[]-a[]<d)sum+=abs(d-a[]+a[]);
if(a[]-a[]<d)sum+=abs(d-a[]+a[]);
printf("%d\n",sum);
return ;
}
3 seconds
256 megabytes
standard input
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.
The first line of the input contains one integer nn (1≤n≤1051≤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 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.
4
hello
hello
hello
helloo
hello
hlllloo
hello
helo
YES
YES
NO
NO
5
aa
bb
codeforces
codeforce
polycarp
poolycarpp
aaaa
aaaab
abcdefghijklmnopqrstuvwxyz
zabcdefghijklmnopqrstuvwxyz
NO
NO
YES
NO
NO
题目大意:Methodius想发一封邮件给他的朋友(应还要打印出来),但是它的键盘坏了,请你判断从键盘输入的内容,是否能按照他的想法打印出来(打印规则是可以输入重复的字符,打印机会
过滤掉多余的重复字符,但是不能有不同的字符出现,这也是判断正确与否的关键),第一行输入从键盘输入的内容,第二行输入想打印的内容,能正确打印输出“YES”,否则输出“NO”
思路:我最开始想的是遍历所有想打印的字符,与输入的匹配,匹配成功就能输出,但是踩了个坑(assdf asd),即当输入的多余字符在想打印的内容匹配完之后还有不想打印的字符,wa了
几次才反应过了。代码如下:
#include <iostream>
#include <algorithm>
int const max_n=;
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()-]!=b[b.length()-]
||a[]!=b[]){printf("NO\n");continue;}//判断首尾字符,减少运算
bool judge=false;//定义bool方便盘点
int j=,k=;//j是键盘输入串的下标,k是匹配的字符数(当匹配足够时,能打印)
for(int i=;i<a.length();i++)
{
while(j<b.length())
{
if(a[i]==b[j]){k++;j++;break;}//匹配一个字符,跳出该次循环,计数器加一
if(b[j]!=a[i-]){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()-]!=b[j]){judge=false;break;}
j++;
}
if(judge)printf("YES\n");
else printf("NO\n");
}
return ;
}
2 seconds
256 megabytes
standard input
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 MMmaxti≤Mmaxti≤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.
The first line of the input contains two integers nn and MM (1≤n≤1001≤n≤100, 1≤M≤1001≤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 (1≤ti≤1001≤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.
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.
7 15
1 2 3 4 5 6 7
0 0 0 0 0 2 3
5 100
80 40 40 40 60
0 1 1 2 3
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=;
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=;i<n;i++){scanf("%d",&num[i].a);x[i]=num[i].a;}//输入的同时,给数组赋值
int sum=;//某个同学之前的所有人都参加考试花费的时间
for(int i=;i<n;i++)
{
sum+=num[i].a;//时间累加
num[i].b=;//先设置不能通过考试人数为零
if(sum>m&&sum!=m){//当第i+1同学考试时间不够时
int s=,t=sum;//s为最小放弃考试机会的人
sort(x,x+i);//将第i+1同学前的所有人考试时间排序
t-=num[i].a;//把该同学的考试时间从已使用时间内去掉,
for(int j=i-;j>=;j--)//从时间最长的同学开始
{
t-=x[j];//已使用时间,逐个去掉占用时间长的铜须
s++;//计数器加1
if(t+num[i].a<=m)break;//当第i+1名同学有足够时间参与考试时,退出本次循环
}
num[i].b=s;
}
}
for(int i=;i<n;i++)printf("%d ",num[i].b);
return ;
}
codeforces Round #568(Div.2)A B C的更多相关文章
- Codeforces Round #568 (Div. 2)B
B. Email from Polycarp 题目链接:http://codeforces.com/contest/1185/problem/B 题目: Methodius received an e ...
- Codeforces Round #568 (Div. 2)A
A. Ropewalkers 题目链接:http://codeforces.com/contest/1185/problem/A 题目: Polycarp decided to relax on hi ...
- Codeforces Round #568 (Div. 2) D. Extra Element
链接: https://codeforces.com/contest/1185/problem/D 题意: A sequence a1,a2,-,ak is called an arithmetic ...
- Codeforces Round #568 (Div. 2) C2. Exam in BerSU (hard version)
链接: https://codeforces.com/contest/1185/problem/C2 题意: The only difference between easy and hard ver ...
- Codeforces Round #568 (Div. 2) B. Email from Polycarp
链接: https://codeforces.com/contest/1185/problem/B 题意: Methodius received an email from his friend Po ...
- Codeforces Round #568 (Div. 2) A.Ropewalkers
链接: https://codeforces.com/contest/1185/problem/A 题意: Polycarp decided to relax on his weekend and v ...
- Codeforces Round #568 (Div. 2) G1. Playlist for Polycarp (easy version) (状压dp)
题目:http://codeforces.com/contest/1185/problem/G1 题意:给你n给选项,每个选项有个类型和价值,让你选择一个序列,价值和为m,要求连续的不能有两个相同的类 ...
- Codeforces Round #568 Div. 2
没有找到这场div3被改成div2的理由. A:签到. #include<bits/stdc++.h> using namespace std; #define ll long long ...
- Codeforces Round #568 (Div. 2) G2. Playlist for Polycarp (hard version)
因为不会打公式,随意就先将就一下? #include<cstdio> #include<algorithm> #include<iostream> #include ...
随机推荐
- Java多次启动相同jar程序
背景 现在很多软件都支持集群部署,但是测试环境通常资源有限,所以一般通过单台机器模拟集群部署(使用不同端口,运行相同jar包),本文的目的就是通过多种方式实现此需求. 两个程序 1.jar程序 ① s ...
- linux驱动由浅入深系列:高通sensor架构实例分析之三(adsp上报数据详解、校准流程详解)【转】
本文转载自:https://blog.csdn.net/radianceblau/article/details/76180915 本系列导航: linux驱动由浅入深系列:高通sensor架构实例分 ...
- [oracle/java/sql]用于上十万批量数据插入Oracle表的Java程序
程序下载:https://files.cnblogs.com/files/xiandedanteng/LeftInnerNotExist20191222.rar 原理:Oracle的Insert al ...
- RunTime.getRunTime().addShutdownHook的用法
今天在阅读Tomcat源码的时候,catalina这个类中使用了下边的代码,不是很了解,所以google了一下,然后测试下方法,Tomcat中的相关代码如下: Runtime.getRuntime() ...
- Python10大热门项目
文章地址:https://baijiahao.baidu.com/s?id=1625230403885659615&wfr=spider&for=pc 今天给大家盘点一下实验楼最热门的 ...
- python安装 werkzeug
1 pip install werkzeug Werkzeug是一个WSGI工具包,他可以作为一个Web框架的底层库.这里稍微说一下, werkzeug 不是一个web服务器,也不是一个web ...
- FreeSWITCH视频直播
大家都知道,如今,视频直播比较火啊. 今天,在FreeSWITCH精英群里分享了FreeSWITCH做视频直播相关的技术. 首先,要做直播就得有好机器.笔者买了一台阿里云的主机,买的是按量付费的,4核 ...
- deformable conv
在原feature map上经过卷积生成与原feature map一样w.h大小的feature map,但是channel变为2倍,即2N.2N代表的是每个像素x.y两个方向的偏移量. 这个偏移量生 ...
- Java地址:
GitHub:https://github.com/nanchen2251 个人博客:https://nanchen2251.github.io/ 简书地址:http://www.jianshu.co ...
- 抓包 抓nodejs的包 抓浏览器的包 抓手机的包
应用场景: 确认接口是能用的,但自己使用时就是不行,参数有没有传正确?格式对不对?傻傻分不清. 抓包工具:这里演示 charles , 常用的还有 Fiddler, HttpWatch, WireSh ...