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的更多相关文章
随机推荐
- 润乾报表整合到Tomcat服务器的部署过程
转载自:http://www.cnblogs.com/avivaye/archive/2012/11/16/2773681.html 使用第三方的报表设计器/服务器来快速的开发报表. 润乾服务器是使用 ...
- Pandas v0.23.4手册汉化
Pandas手册汉化 此页面概述了所有公共pandas对象,函数和方法.pandas.*命名空间中公开的所有类和函数都是公共的. 一些子包是公共的,其中包括pandas.errors, pandas. ...
- javaweb(三十七)——获得MySQL数据库自动生成的主键
测试脚本如下: 1 create table test1 2 ( 3 id int primary key auto_increment, 4 name varchar(20) 5 ); 测试代码: ...
- 「Leetcode」14. Longest Common Prefix(Java)
分析 与其说是算法题,不如说是语言特性题. 这题要是对Java的String相关函数掌握的比较熟练,写起来的速度(各种意义上)就会很快. 大致的思路都是一致的,差不到哪里去,无非是枚举长度.值得一提的 ...
- Python环境搭建和pycharm安装
Python环境搭建和pycharm安装 本人安装环境为Windows10系统,下载的Python版本为3.4社区版本,可参考 1.下载Python3.4版本 官网:https://www.pytho ...
- 第七篇 Postman+Node.js+Newman+Jenkins实现自动化测试
今天终于不咋忙了,学习整理一下一直想做却没实现的事儿,这事已经折磨团队半年之久了.因为项目是B端业务的测试,测试过程中需要生产大量的测试数据,而且都是跨多个系统的测试,对于后置流程的测试,这些同学往往 ...
- win2003系统网络安装——基于linux+pxe+dhcp+tftp+samba+ris
原文发表于:2010-09-16 转载至cu于:2012-07-21 一.原理简介 PXE(preboot execute environment)工作于Client/Server的网络模式,支持工作 ...
- Redis的数据类型以及每种数据类型的使用场景
人就是很奇怪的动物,很简单的问题往往大家都容易忽略,当我们在使用分布式缓存Redis的时候,一个最简单的问题Redis的数据类型以及每种数据类型的使用场景是什么? 是不是觉得这个问题很基础?我也这么觉 ...
- linux 命令自动补全包
linux 其他知识目录 rhel7如果使用最小化安装后,tab键默认是不能自动补全命令的 执行yum install bash-completion之后重启系统正常.
- Linux sync命令的作用分析
Sync命令 在用reboot命令启动unix系统后,系统提示出错信息,部分应用程序不能正常工作.经仔细检查系统文件,并和初始的正确备份进行比较,发现某些文件确实被破坏了,翻来覆去找不到文件遭破坏 ...