B. The Queue
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Finally! Vasya have come of age and that means he can finally get a passport! To do it, he needs to visit the passport office, but it's not that simple. There's only one receptionist at the passport office and people can queue up long before it actually opens. Vasya wants to visit the passport office tomorrow.

He knows that the receptionist starts working after ts minutes have passed after midnight and closes after tf minutes have passed after midnight (so that (tf - 1) is the last minute when the receptionist is still working). The receptionist spends exactly t minutes on each person in the queue. If the receptionist would stop working within t minutes, he stops serving visitors (other than the one he already serves).

Vasya also knows that exactly n visitors would come tomorrow. For each visitor Vasya knows the point of time when he would come to the passport office. Each visitor queues up and doesn't leave until he was served. If the receptionist is free when a visitor comes (in particular, if the previous visitor was just served and the queue is empty), the receptionist begins to serve the newcomer immediately.

"Reception 1"

For each visitor, the point of time when he would come to the passport office is positive. Vasya can come to the office at the time zero (that is, at midnight) if he needs so, but he can come to the office only at integer points of time. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and stand in the queue after the last of them.

Vasya wants to come at such point of time that he will be served by the receptionist, and he would spend the minimum possible time in the queue. Help him!

Input

The first line contains three integers: the point of time when the receptionist begins to work ts, the point of time when the receptionist stops working tf and the time the receptionist spends on each visitor t. The second line contains one integer n — the amount of visitors (0 ≤ n ≤ 100 000). The third line contains positive integers in non-decreasing order — the points of time when the visitors arrive to the passport office.

All times are set in minutes and do not exceed 1012; it is guaranteed that ts < tf. It is also guaranteed that Vasya can arrive at the passport office at such a point of time that he would be served by the receptionist.

Output

Print single non-negative integer — the point of time when Vasya should arrive at the passport office. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and queues up the last. If there are many answers, you can print any of them.

Examples
input
10 15 2
2
10 13
output
12
input
8 17 3
4
3 4 5 8
output
2
Note

In the first example the first visitor comes exactly at the point of time when the receptionist begins to work, and he is served for two minutes. At 12 minutes after the midnight the receptionist stops serving the first visitor, and if Vasya arrives at this moment, he will be served immediately, because the next visitor would only come at 13 minutes after midnight.

In the second example, Vasya has to come before anyone else to be served.

题意:

服务员工作时间是ts~tf,每服务一个人需要t时间,已知n个人来排队等待服务的时间,问我什么时候去才能使我的排队时间最少,如果我到达的时间和某一个人相同就会排到最后

//用tim记录服务员下一次可以接受服务的时间,每输入一个人,最好的情况是他来的时间晚于
//服务员下次服务的时间,这样我可以直接去接受服务。其次是如果他前一时间没有人来,
//我去到他前面等一会接受服务。
#include<bits/stdc++.h>
using namespace std;
long long ts,tf,t,n;
int main()
{
cin>>ts>>tf>>t>>n;
long long tim=ts,x,y,pre=-,ans,maxt=1e12+;
for(int i=;i<=n;i++){
cin>>x;
if(tim>tf||maxt==) continue;
y=x-;
if(y>=&&x<=tim&&y>pre){
if(tim-x+<maxt) {ans=y;maxt=tim-x+;}
}
if(x>tim){
ans=tim;maxt=;
}
tim+=t;
pre=x;
}
if(tim+t<=tf&&maxt!=) ans=tim;//如果最后还有剩余的服务时间
cout<<ans<<endl;
return ;
}
C. Garland
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Once at New Year Dima had a dream in which he was presented a fairy garland. A garland is a set of lamps, some pairs of which are connected by wires. Dima remembered that each two lamps in the garland were connected directly or indirectly via some wires. Furthermore, the number of wires was exactly one less than the number of lamps.

There was something unusual about the garland. Each lamp had its own brightness which depended on the temperature of the lamp. Temperatures could be positive, negative or zero. Dima has two friends, so he decided to share the garland with them. He wants to cut two different wires so that the garland breaks up into three parts. Each part of the garland should shine equally, i. e. the sums of lamps' temperatures should be equal in each of the parts. Of course, each of the parts should be non-empty, i. e. each part should contain at least one lamp.

Help Dima to find a suitable way to cut the garland, or determine that this is impossible.

While examining the garland, Dima lifted it up holding by one of the lamps. Thus, each of the lamps, except the one he is holding by, is now hanging on some wire. So, you should print two lamp ids as the answer which denote that Dima should cut the wires these lamps are hanging on. Of course, the lamp Dima is holding the garland by can't be included in the answer.

Input

The first line contains single integer n (3 ≤ n ≤ 106) — the number of lamps in the garland.

Then n lines follow. The i-th of them contain the information about the i-th lamp: the number lamp ai, it is hanging on (and 0, if is there is no such lamp), and its temperature ti ( - 100 ≤ ti ≤ 100). The lamps are numbered from 1 to n.

Output

If there is no solution, print -1.

Otherwise print two integers — the indexes of the lamps which mean Dima should cut the wires they are hanging on. If there are multiple answers, print any of them.

Examples
input
6
2 4
0 5
4 2
2 1
1 1
4 2
output
1 4
input
6
2 4
0 6
4 2
2 1
1 1
4 2
output
-1
Note

The garland and cuts scheme for the first example:


 题意:
给出一棵树,每个点有权值,问是否存在两条边把树分成权值相等的三部分,输出边的序号。
代码:
//算出每一棵子树包含的权值付给根节点,当某一根节点值是sum/3时记录相关边,并去掉这颗子树。
//题目的特殊输入,边的编号就是改边作为儿子节点的编号。
#include<bits/stdc++.h>
using namespace std;
int n,cnt[],s[];
int a,b,sum=,nu;
vector<int>p[];
int dfs(int x,int fx){
int len=p[x].size();
for(int i=;i<len;i++)
cnt[x]+=dfs(p[x][i],x);
if(cnt[x]==sum/&&fx!=&&nu<=) {s[++nu]=x;return ;}
return cnt[x];
}
int main()
{
scanf("%d",&n);
nu=;
for(int i=;i<=n;i++){
scanf("%d%d",&a,&b);
cnt[i]=b;
sum+=b;
p[a].push_back(i);
}
if(sum%) printf("-1\n");
else{
cnt[]=dfs(p[][],);
if(nu<) printf("-1\n");
else printf("%d %d\n",s[],s[]);
}
return ;
}

Codeforces Round #398 (Div. 2) B,C的更多相关文章

  1. Codeforces Round #398 (Div. 2)

    Codeforces Round #398 (Div. 2) A.Snacktower 模拟 我和官方题解的命名神相似...$has$ #include <iostream> #inclu ...

  2. Codeforces Round #398 (Div. 2) A. Snacktower 模拟

    A. Snacktower 题目连接: http://codeforces.com/contest/767/problem/A Description According to an old lege ...

  3. Codeforces Round #398 (Div. 2) C. Garland —— DFS

    题目链接:http://codeforces.com/contest/767/problem/C 题解:类似于提着一串葡萄,用剪刀剪两条藤,葡萄分成了三串.问怎样剪才能使三串葡萄的质量相等. 首先要做 ...

  4. Codeforces Round #398 (div.2)简要题解

    这场cf时间特别好,周六下午,于是就打了打(谁叫我永远1800上不去div1) 比以前div2的题目更均衡了,没有太简单和太难的...好像B题难度高了很多,然后卡了很多人. 然后我最后做了四题,E题感 ...

  5. Codeforces Round #398 (Div. 2) A,B,C,D

    A. Snacktower time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  6. Codeforces Round #398 (Div. 2) A B C D 模拟 细节 dfs 贪心

    A. Snacktower time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  7. 【DFS】Codeforces Round #398 (Div. 2) C. Garland

    设sum是所有灯泡的亮度之和 有两种情况: 一种是存在结点U和V,U是V的祖先,并且U的子树权值和为sum/3*2,且U不是根,且V的子树权值和为sum/3. 另一种是存在结点U和V,他们之间没有祖先 ...

  8. 【枚举】【贪心】 Codeforces Round #398 (Div. 2) B. The Queue

    卡题意……妈的智障 一个人的服务时间完整包含在整个工作时间以内. 显然,如果有空档的时间,并且能再下班之前完结,那么直接输出即可,显然取最左侧的空档最优. 如果没有的话,就要考虑“挤掉”某个人,就是在 ...

  9. 【暴力】Codeforces Round #398 (Div. 2) A. Snacktower

    题意不复述. 用个bool数组记录一下,如果某一天,当前剩下的最大的出现了的话,就输出一段. #include<cstdio> using namespace std; int n; bo ...

随机推荐

  1. NOIP2012 普及组真题 4.13校模拟

    考试状态: 我今天抽签看了洛谷的… 这我能怂???凶中带吉,我怕考试??我!不!怕! 看着整个机房的男同学们,我明白我是不会触发我的忌了.很好,开刷. A. [NOIP2012普及组真题] 质因数分解 ...

  2. c# winform 服务器提交了协议冲突. Section=ResponseStatusLine

    [转] 最近在用.net写一个网络蜘蛛,发现对有的网站用HttpWebrequest抓取网页的时候会报错,捕获异常提示:"服务器提交了协议冲突 Section=ResponseStatusL ...

  3. Java学习个人备忘录之关键字static

    被static标记的东西会放在内存中被共享的,对象用到时,就会来取的. class Person { String name; //成员变量,实例变量 static String country = ...

  4. Alpha项目冲刺_博客链接合集

    组员 学号 林泽宇(队长) 211606317 李涵 211606365 尹海川 211606388 郏敏杰 211606307 何永康 211606362 陈炳旭 211606353 苏宇翔 211 ...

  5. LintCode-105.复制带随机指针的链表

    复制带随机指针的链表 给出一个链表,每个节点包含一个额外增加的随机指针可以指向链表中的任何节点或空的节点. 返回一个深拷贝的链表. 挑战 可否使用O(1)的空间 标签 哈希表 链表 优步 code / ...

  6. 百度安卓sdk开发

    一 key问题 1 在百度地图api控制台申请key的流程主要用到了app包,开发工具的开发sha1和发布sha1值,这2个值的获取就非常关键了. 一般来说我们都是在windows上开发安卓,使用an ...

  7. jquery delayLoading.js插件的延迟加载效果和图片延迟加载

    1.首页给大家介绍一下这款插件的主要用途 主要应用于图片的延迟加载,而且可以变换不同的延迟加载效果,适合相册图片.有做相册的可以考虑应用. 2.兼容IE7以上都兼容,其他的浏览器也兼容.所以说兼容性还 ...

  8. 安装django 提示ImportError: No module named setuptools

    安装django前要先安装setuptools 先安装一些必要的包,否则会报错:Python build finished, but the necessary bits to build these ...

  9. 7款很棒的 HTML5 视频播放器

    做个连接:http://www.cnblogs.com/lhb25/archive/2011/06/27/7-great-html-5-video-player-scripts.html

  10. deep learning2

    九.Deep learning的常用模型或者方法 9.1.AutoEncoder自动编码器 Deep Learning最简单的一种方法是利用人工神经网络的特点,人工神经网络(ANN)本身就是具有层次结 ...