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的更多相关文章
随机推荐
- [bzoj1500][luogu2042][cogs339][codevs1758]维修数列(维护数列)
先给自己立一个flag 我希望上午能写完 再立一个flag 我希望下午能写完. 再立一个flag 我希望晚上能写完... 我终于A了... 6700+ms...(6728) 我成功地立了3个flag. ...
- javaweb(十一)——使用Cookie进行会话管理
一.会话的概念 会话可简单理解为:用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话. 有状态会话:一个同学来过教室,下次再来教室,我们会知道这个同学曾 ...
- javaweb(十)——HttpServletRequest对象(一)
一.HttpServletRequest介绍 HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,通过这个对象 ...
- Qt 利用XML文档,写一个程序集合 三
接上一篇https://www.cnblogs.com/DreamDog/p/9214052.html 滚动区域实现, 滚动区域可以三成分层 第一层,显示内容 中间层,滚动层 第三层,爸爸层 把我们要 ...
- 你的第一个接口测试:Python 接口测试
前言: 首先我们先明确一个概念,什么叫接口.什么叫接口测试? 接口的全称叫[Application Programming Interface 又叫API],是提供应用程序与开发人员基于某软件或硬件得 ...
- 使用Firebug或chrome-devToolBar深入学习javascript语言核心
使用Firebug和chrome-devToolBar调试页面样式或脚本是前端开发每天必做之事.这个开发神器到底能给我们带来哪些更神奇的帮助呢?这几天看的一些资料中给了我启发,能不通过Firebug和 ...
- Python异常(基础) except
为什么要异常处理机制:在程序调用层数较深时,向主调函数传递错误信息需要层层return 返回比较麻烦,用异常处理机制可以较简单的传送错误信息 什么是错误 错误是指由于逻辑或语法等导致一个程序已无法正常 ...
- [T-ARA/筷子兄弟][Little Apple]
歌词来源:http://music.163.com/#/song?id=29753511 作曲 : 筷子兄弟 [作曲 : 筷子兄弟] 作词 : K-Smith [作词 : KSmith] 编曲 : 新 ...
- Python常用模块之Pygame(手册篇:首页)
Pygame手册官方网址:http://www.pygame.org/docs/ Pygame首页 说明文档: 自述 关于Pygame的基本信息,它是什么,谁参与了以及在哪里找到它. 安装 在几个平台 ...
- 深入react技术栈解读
1. react实现virtual DOM ,如果要改变页面的内容,还是需要执行DOM操作,比原生操作DOM多了virtualDOM的操作(计算,对比等), 应该是更耗性能??? 2. react特点 ...