Codeforces709
A
Kolya is going to make fresh orange juice. He has n oranges of sizes a1, a2, ..., an. Kolya will put them in the juicer in the fixed order, starting with orange of size a1, then orange of size a2 and so on. To be put in the juicer the orange must have size not exceeding b, so if Kolya sees an orange that is strictly greater he throws it away and continues with the next one.
The juicer has a special section to collect waste. It overflows if Kolya squeezes oranges of the total size strictly greater than d. When it happens Kolya empties the waste section (even if there are no more oranges) and continues to squeeze the juice. How many times will he have to empty the waste section?
The first line of the input contains three integers n, b and d (1 ≤ n ≤ 100 000, 1 ≤ b ≤ d ≤ 1 000 000) — the number of oranges, the maximum size of the orange that fits in the juicer and the value d, which determines the condition when the waste section should be emptied.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 1 000 000) — sizes of the oranges listed in the order Kolya is going to try to put them in the juicer.
Print one integer — the number of times Kolya will have to empty the waste section.
#include<stdio.h>
#include<stdlib.h>
#include<string>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#define il inline
#define re register
using namespace std;
int n,b,d,a[],c,ans;
int main(){
scanf("%d%d%d",&n,&b,&d);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
if(a[i]>b) continue;
c+=a[i];
if(c>d){
c=;ans++;
}
}
cout<<ans;
return ;
}
B
Vasya takes part in the orienteering competition. There are n checkpoints located along the line at coordinates x1, x2, ..., xn. Vasya starts at the point with coordinate a. His goal is to visit at least n - 1 checkpoint in order to finish the competition. Participant are allowed to visit checkpoints in arbitrary order.
Vasya wants to pick such checkpoints and the order of visiting them that the total distance travelled is minimized. He asks you to calculate this minimum possible value.
The first line of the input contains two integers n and a (1 ≤ n ≤ 100 000, - 1 000 000 ≤ a ≤ 1 000 000) — the number of checkpoints and Vasya's starting position respectively.
The second line contains n integers x1, x2, ..., xn ( - 1 000 000 ≤ xi ≤ 1 000 000) — coordinates of the checkpoints.
Print one integer — the minimum distance Vasya has to travel in order to visit at least n - 1 checkpoint.
#include<stdio.h>
#include<stdlib.h>
#include<string>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#define il inline
#define re register
using namespace std;
int n,x,a[];
int main(){
scanf("%d%d",&n,&x);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
sort(a+,a+n+);
if(n==){
cout<<"";return ;
}
if(x<a[]){
cout<<abs(a[n-]-x);return ;
}
if(x>a[n]){
cout<<abs(a[]-x);return ;
}
if(n==){
cout<<min(abs(a[]-x),abs(a[n]-x));return ;
}
cout<<min(min(abs(x-a[]),abs(a[n]-x))+abs(a[]-a[n]),min(abs(x-a[]),abs(x-a[n-]))+abs(a[]-a[n-]));
return ;
}
C
You are given a non-empty string s consisting of lowercase English letters. You have to pick exactly one non-empty substring of s and shift all its letters 'z' 'y' 'x' 'b' 'a' 'z'. In other words, each character is replaced with the previous character of English alphabet and 'a' is replaced with 'z'.
What is the lexicographically minimum string that can be obtained from s by performing this shift exactly once?
The only line of the input contains the string s (1 ≤ |s| ≤ 100 000) consisting of lowercase English letters.
Print the lexicographically minimum string that can be obtained from s by shifting letters of exactly one non-empty substring.
#include<stdio.h>
#include<stdlib.h>
#include<string>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#define il inline
#define re register
using namespace std;
char s[];
int n,a,b;
il bool chk(){
for(int i=;i<=n;i++)
if(s[i]!='a') return false;
return true;
}
int main(){
scanf("%s",s+);n=strlen(s+);
if(chk()){
for(int i=;i<n;i++)
printf("a");
cout<<'z';return ;
}
a=;
while(s[a]=='a') a++;
for(int i=;i<a;i++) printf("%c",s[i]);
for(int i=a;i<=n;i++){
if(s[i]!='a'){
printf("%c",s[i]-);
}
else{
for(int j=i;j<=n;j++)
printf("%c",s[j]);
return ;
}
}
return ;
}
D
For each string s consisting of characters '0' and '1' one can define four integers a00, a01, a10 and a11, where axy is the number ofsubsequences of length 2 of the string s equal to the sequence {x, y}.
In these problem you are given four integers a00, a01, a10, a11 and have to find any non-empty string s that matches them, or determine that there is no such string. One can prove that if at least one answer exists, there exists an answer of length no more than1 000 000.
The only line of the input contains four non-negative integers a00, a01, a10 and a11. Each of them doesn't exceed 10^9.
If there exists a non-empty string that matches four integers from the input, print it in the only line of the output. Otherwise, print "Impossible". The length of your answer must not exceed 1 000 000.
这个嘛,预处理要多少个1和0,然后01和10加起来的数目是固定的,因此满足一个就好了。
#include<stdio.h>
#include<stdlib.h>
#include<string>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#define il inline
#define re register
using namespace std;
typedef long long ll;
ll n=-,a1,a2,a3,a4,s=-,t=-,a,p;
int main(){
cin>>a1>>a2>>a3>>a4;a=a1+a2+a3+a4;
for(ll i=;i<=;i++){
if(i*(i-)/==a){
n=i;break;
}
}
if(n==-){
cout<<"Impossible";return ;
}
if(a1==a){
for(int i=;i<=n;i++)
printf("");
return ;
}
if(a4==a){
for(int i=;i<=n;i++)
printf("");
return ;
}
for(ll i=;i<=;i++){
if(i*(i-)/==a1){
s=i;break;
}
}
for(ll i=;i<=;i++){
if(i*(i-)/==a4){
t=i;break;
}
}
if(s==-&&t==-){
cout<<"Impossible";return ;
}
if(a2+a3!=s*t){
cout<<"Impossible";return ;
}
p=a2/s+(a2%s>);
if(p>t){
cout<<"Impossible";return ;
}
for(int i=;i<=t-p;i++)
printf("");
for(int i=;i<=s;i++){
printf("");
if(a2%s==i){
printf("");
}
}
for(int i=;i<=a2/s;i++)
printf("");
return ;
}
E
Tree is a connected acyclic graph. Suppose you are given a tree consisting of n vertices. The vertex of this tree is called centroid if the size of each connected component that appears if this vertex is removed from the tree doesn't exceed .
You are given a tree of size n and can perform no more than one edge replacement. Edge replacement is the operation of removing one edge from the tree (without deleting incident vertices) and inserting one new edge (without adding new vertices) in such a way that the graph remains a tree. For each vertex you have to determine if it's possible to make it centroid by performing no more than one edge replacement.
The first line of the input contains an integer n (2 ≤ n ≤ 400 000) — the number of vertices in the tree. Each of the next n - 1 lines contains a pair of vertex indices ui and vi (1 ≤ ui, vi ≤ n) — endpoints of the corresponding edge.
Print n integers. The i-th of them should be equal to 1 if the i-th vertex can be made centroid by replacing no more than one edge, and should be equal to 0 otherwise.
如果有两个重心,则把重心边破开,把其中一边接到另外一边即可
如果有一个重心,则找到重心最大的一个子树,把它断开和当前点接上。
如果当前点在那个子树上,就和次大的接上。
#include<stdio.h>
#include<stdlib.h>
#include<string>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#define il inline
#define re register
using namespace std;
const int N=;
struct edge{int next,to;
} e[N];
int g[N],n,M,cn,c[],fir,sec,color[N],s[N];
il void addedge(int x,int y){
e[++M]=(edge){g[x],y};g[x]=M;
}
il void dfs1(int h,int fa){
bool flag=true;
for(int i=g[h];i;i=e[i].next){
if(e[i].to==fa) continue;
dfs1(e[i].to,h);
if(s[e[i].to]>n/) flag=false;
}
if(n-s[h]<=n/&&flag==true){
c[cn++]=h;
}
}
il void dfs2(int h,int fa){
s[h]=;
for(int i=g[h];i;i=e[i].next){
if(fa==e[i].to||e[i].to==c[]) continue;
dfs2(e[i].to,h);s[h]+=s[e[i].to];
}
}
il void dfs3(int h,int fa){
for(int i=g[h];i;i=e[i].next){
if(fa==e[i].to||e[i].to==c[]) continue;
color[e[i].to]=;dfs3(e[i].to,h);
}
}
il void predo(){
dfs2(,);
dfs1(,);
}
int main(){
scanf("%d",&n);
for(int i=,x,y;i<n;i++){
scanf("%d%d",&x,&y);
addedge(x,y);
addedge(y,x);
}
predo();
if(cn==){
for(int i=;i<=n;i++)
printf("1 ");
return ;
}
dfs2(c[],);
for(int j=g[c[]];j;j=e[j].next){
if(e[j].to==c[]||e[j].to==c[]) continue;
if(s[e[j].to]>s[fir]){
sec=fir;
fir=e[j].to;
}
else if(s[e[j].to]>s[sec]){
sec=e[j].to;
}
}
color[fir]=;dfs3(fir,);
for(int i=;i<=n;i++){
if(i==c[]){
printf("1 ");continue;
}
if(color[i]){
if(n-s[sec]-s[i]<=n/){
printf("1 ");
}
else printf("0 ");
}
else{
if(n-s[fir]-s[i]<=n/){
printf("1 ");
}
else{
printf("0 ");
}
}
}
return ;
}
Codeforces709的更多相关文章
随机推荐
- L018-crond的生产场景经验小节
L018-crond的生产场景经验小节 怎么说呢,其实L018这节课还是巩固crond的知识,前半堂课主要是解决上堂课老师留的作业(在L017已经更新,拉到最后),然后剩下的半堂客主要是讲解了一些生产 ...
- C#随堂
顺序语句 上到下执行 分支语句 if else switch() { case 1: Console.WriteLine(1); break; case 2: Console.WriteLine ...
- 阿里云Ubuntu 14.04 + Nginx + .net core + MySql
前段时间帮朋友写了一个网站,现在做一个记录. .Net Core 安装: curl https://packages.microsoft.com/keys/microsoft.asc | gpg -- ...
- selenium+Java,xpath定位方法详解(搬运留存)
用xpath绝对路径比较费事费力,还容易报错,下面几种模糊定位比较灵活好用 driver.findElement(By.xpath("//*[@id='J_login_form']/dl/d ...
- JUC——线程同步锁(锁处理机制简介)
锁处理机制简介 juc的开发框架解决的核心问题是并发访问和数据安全操作问题,当进行并发访问的时候如果对于锁的控制不当,就会造成死锁这样的阻塞问题. 为了解决这样的缺陷,juc里面重新针对于锁的概念进行 ...
- 运输层(TCP/UDP)详解
TCP和UDP的区别: tcp是面向连接的可靠的传输协议 udp是非连接的不可靠的传输协议 TCP组成 可以看到虽然tcp是面向字节流的,但是其传输的基本单位还是报文(tcp首部和数据,ip报文和ud ...
- 基于C#的机器学习--机器学习的基本知识
机器学习的基本知识 ,…用n个观测值测量.但我们不再对Y的预测感兴趣,因为我们不再有Y了,我们唯一感兴趣的是在已有的特征上发现数据模式: 在前面的图中,我们可以看到这样的数据本身更适合于非线性方法 ...
- python基础知识-8-三元和一行代码(推导式)
python其他知识目录 1.三元运算(三目运算) 三元运算符就是在赋值变量的时候,可以直接加判断,然后赋值格式:[on_true] if [expression] else [on_false]re ...
- ES6的新特性(11)——Class 的继承
Class 的继承 简介 Class 可以通过extends关键字实现继承,这比 ES5 的通过修改原型链实现继承,要清晰和方便很多. class Point { } class ColorPoint ...
- python实现中文验证码识别方法(亲测通过)
验证码截图如下: # coding:utf-8from PIL import Image,ImageEnhanceimport pytesseract#上面都是导包,只需要下面这一行就能实现图片文字识 ...