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的更多相关文章
随机推荐
- Python小白学习之文件内建函数
文件内建函数: 2018-10-24 23:40:02 简单介绍: open()打开文件 read()读取文件(其实是输入文件里的内容到read函数,类似于get(url),所以下面的图片备注的是 ...
- TPO 02 - Desert Formation
TPO 02 - Desert Formation NOTE: 主要意思(大概就是主谓宾)用粗体标出:重要的其它用斜体: []中的是大致意思,可能与原文有关也可能无关,但不会离题 目的为训练句子/段落 ...
- 【jpeg_Class 类】使用说明
jpeg_Class类是针对图片操作类,可以获取图片属性.等比例缩略图片.裁切图片.图片上打印文字及打印水印等功能. 目录 原型 参数 返回 说明 Sub load(byVal path) path ...
- #Ubuntu 18.04 安装tensorflow-gpu 1.9
参考 https://tensorflow.google.cn/install/install_linux http://nvidia.com/cuda http://developer.nvidia ...
- C# 反射,动态编译
反射是动态获取程序集的元数据的一种技术,这句话是做.NET程序员面试题目的一个的答案,你可选择记住它,就好比高中生物学里面讲到的细胞的结构的课程时,细胞由细胞膜,细胞质和细胞核组成.根据做程序的经验, ...
- kettle_Spoon 修改共享DB连接带汉字引发的错误
win10下: kettle_Spoon 修改共享DB连接带汉字引发的错误: Unexpected problem reading shared objects from XML file : nul ...
- KETTLE设置变量
一.kettle变量类型 kettle变量分为: 1.环境变量 通过 set variables组件设置变量,范围可以是:JVM变量.作业变量.父作业变量.根作业变量.使用时通过${var}或 %%v ...
- kafka可靠性
文章转载自: http://blog.csdn.net/u013256816/article/details/71091774
- [Algorithm] A* Search Algorithm Basic
A* is a best-first search, meaning that it solves problems by searching amoung all possible paths to ...
- c# Webservice技术整理
因为平常项目中使用webservice比较少,然后就将本来不太熟悉的webservice给忘记掉了.所以再次整理如下: 百度搜索关键词 :c# webservice 1. 联接地址: http://w ...