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.
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!
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.
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.
10 15 2
2
10 13
12
8 17 3
4
3 4 5 8
2
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=-1,ans,maxt=1e12+9; for(int i=1;i<=n;i++){ cin>>x; if(tim>tf||maxt==0) continue; y=x-1; if(y>=0&&x<=tim&&y>pre){ if(tim-x+1<maxt) {ans=y;maxt=tim-x+1;} } if(x>tim){ ans=tim;maxt=0; } tim+=t; pre=x; } if(tim+t<=tf&&maxt!=0) ans=tim;//如果最后还有剩余的服务时间 cout<<ans<<endl; return 0; }
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.
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.
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.
6
2 4
0 5
4 2
2 1
1 1
4 2
1 4
6
2 4
0 6
4 2
2 1
1 1
4 2
-1
The garland and cuts scheme for the first example:
//算出每一棵子树包含的权值付给根节点,当某一根节点值是sum/3时记录相关边,并去掉这颗子树。 //题目的特殊输入,边的编号就是改边作为儿子节点的编号。 #include<bits/stdc++.h> using namespace std; int n,cnt[1000006],s[5]; int a,b,sum=0,nu; vector<int>p[1000006]; int dfs(int x,int fx){ int len=p[x].size(); for(int i=0;i<len;i++) cnt[x]+=dfs(p[x][i],x); if(cnt[x]==sum/3&&fx!=0&&nu<=2) {s[++nu]=x;return 0;} return cnt[x]; } int main() { scanf("%d",&n); nu=0; for(int i=1;i<=n;i++){ scanf("%d%d",&a,&b); cnt[i]=b; sum+=b; p[a].push_back(i); } if(sum%3) printf("-1 "); else{ cnt[0]=dfs(p[0][0],0); if(nu<2) printf("-1 "); else printf("%d %d ",s[1],s[2]); } return 0; }