2017.2.18Codeforces Round #398 (Div. 2)

妈蛋考的好萎啊啊啊啊

不过神奇的rank升了1……

T1唯一的得分……还看错了……水体不解释

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

According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.

Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.

However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.

Write a program that models the behavior of Ankh-Morpork residents.

Input

The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.

The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.

Output

Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.

Examples
Input
3
3 1 2
Output
3
 
2 1
Input
5
4 5 1 2 3
Output
 
5 4
 
 
3 2 1
Note

In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.

总结经验就是打CF一定要先看Note!!!

T2没有调试出来……最悲伤……其实不难,但是忘记了终止条件和0的特判……

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.

附AC代码

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cstring>
 4 #include<iostream>
 5 #include<algorithm>
 6 #define N 100010
 7 #define RG register
 8 #define Inf 99999999999999999LL
 9 using namespace std;
10 typedef long long LL;
11 int n;
12 LL ts,tf,t,pre,st,now,ans=Inf,loc,tim[N];
13 inline LL Min(RG const LL &a,RG const LL &b){return a>b?b:a;}
14 inline LL gll(){
15     RG LL x=0;RG bool flag=0;RG char c=getchar();
16     while((c<'0'||c>'9')&&c!='-') c=getchar();
17     if(c=='-') c=getchar(),flag=1;
18     while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
19     return flag?-x:x;
20 }
21 inline int gi(){
22     RG int x=0;RG char c=getchar();
23     while(c<'0'||c>'9') c=getchar();
24     while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
25     return x;
26 }
27 inline void work(){
28     ts=gll();tf=gll();t=gll();n=gi();pre=ts;loc=ts;
29     if(n) for (RG int i=1;i<=n;++i) tim[i]=gll();
30     for (RG int i=0;i<=n;++i){
31     if(i<1){
32         if(tim[1]==0) continue;
33         st=Min(tim[i+1]-1,pre);
34         now=pre-st;
35     }
36     else if(i<n){
37         pre+=t;
38         if(pre>(tf-t)+1) break;
39         if(tim[i+1]==tim[i]) continue;
40         st=Min(tim[i+1]-1,pre);
41         now=pre-st;
42     }
43     else{
44         pre+=t;
45         if(pre>(tf-t)+1) break;
46         st=pre;
47         now=pre-st;
48     }
49     if(now<ans){
50         loc=st;
51         ans=now;
52         if(!ans) break;
53     }
54     }
55     cout<<loc<<endl;
56 }
57 int main(){
58     //freopen("B.in","r",stdin);
59     //freopen("B.out","w",stdout);
60     work();
61     return 0;
62 }
View Code
D. Cartons of milk
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Olya likes milk very much. She drinks k cartons of milk each day if she has at least k and drinks all of them if she doesn't. But there's an issue — expiration dates. Each carton has a date after which you can't drink it (you still can drink it exactly at the date written on the carton). Due to this, if Olya's fridge contains a carton past its expiry date, she throws it away.

Olya hates throwing out cartons, so when she drinks a carton, she chooses the one which expires the fastest. It's easy to understand that this strategy minimizes the amount of cartons thrown out and lets her avoid it if it's even possible.

Milk. Best before: 20.02.2017.

The main issue Olya has is the one of buying new cartons. Currently, there are n cartons of milk in Olya's fridge, for each one an expiration date is known (how soon does it expire, measured in days). In the shop that Olya visited there are m cartons, and the expiration date is known for each of those cartons as well.

Find the maximum number of cartons Olya can buy so that she wouldn't have to throw away any cartons. Assume that Olya drank no cartons today.

Input

In the first line there are three integers n, m, k (1 ≤ n, m ≤ 106, 1 ≤ k ≤ n + m) — the amount of cartons in Olya's fridge, the amount of cartons in the shop and the number of cartons Olya drinks each day.

In the second line there are n integers f1, f2, ..., fn (0 ≤ fi ≤ 107) — expiration dates of the cartons in Olya's fridge. The expiration date is expressed by the number of days the drinking of this carton can be delayed. For example, a 0 expiration date means it must be drunk today, 1 — no later than tomorrow, etc.

In the third line there are m integers s1, s2, ..., sm (0 ≤ si ≤ 107) — expiration dates of the cartons in the shop in a similar format.

Output

If there's no way for Olya to drink the cartons she already has in her fridge, print -1.

Otherwise, in the first line print the maximum number x of cartons which Olya can buy so that she wouldn't have to throw a carton away. The next line should contain exactly x integers — the numbers of the cartons that should be bought (cartons are numbered in an order in which they are written in the input, starting with 1). Numbers should not repeat, but can be in arbitrary order. If there are multiple correct answers, print any of them.

没想到这题如此之水……sort加二分答案即可。

 1 #include<cmath>
 2 #include<queue>
 3 #include<cstdio>
 4 #include<vector>
 5 #include<cstdlib>
 6 #include<cstring>
 7 #include<iostream>
 8 #include<algorithm>
 9 #define N 1000010
10 #define M 1000010
11 #define RG register
12 #define inf 0x3f3f3f3f
13 #define Inf 99999999999999999LL
14 using namespace std;
15 typedef long long LL;
16 LL sum;
17 struct node{
18     int val,num;
19 }shop[M];
20 int n,m,k,l,r,ans=inf,point1,point2,date,fri[N];
21 inline bool cmp(RG const node &a,RG const node &b){return a.val<b.val;}
22 inline int gi(){
23     RG int x=0;RG char c=getchar();
24     while(c<'0'||c>'9') c=getchar();
25     while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
26     return x;
27 }
28 inline bool check(RG int mid){
29     point1=1,point2=m-mid+1,date=sum=0;
30     while(1){
31     //cout<<point1<<' '<<point2<<' '<<date<<' '<<sum<<endl;
32     if(fri[point1]<=shop[point2].val&&fri[point1]>=date){
33         ++sum;
34         ++point1;
35     }
36     else if(shop[point2].val<=fri[point1]&&shop[point2].val>=date){
37         ++sum;
38         ++point2;
39     }
40     else return 0;
41     if(sum==k) sum=0,++date;
42     if(point1==n+1&&point2==m+1) break;
43     }
44     return 1;
45 }
46 inline void work(){
47     n=gi();m=gi();k=gi();r=m;
48     //cout<<n<<' '<<m<<' '<<k<<endl;
49     for (RG int i=1;i<=n;++i) fri[i]=gi();
50     for (RG int i=1;i<=m;++i) shop[i].val=gi(),shop[i].num=i;
51     sort(fri+1,fri+n+1);
52     sort(shop+1,shop+m+1,cmp);
53     fri[n+1]=shop[m+1].val=inf;
54     while(l<=r){
55     RG int mid=(l+r)>>1;
56     //cout<<l<<' '<<r<<' '<<mid<<endl;
57     if(check(mid)) ans=mid,l=mid+1;
58     else           r=mid-1;
59     }
60     if(ans==inf) printf("-1
");
61     else{
62         printf("%d
",ans);
63     if(ans)
64         for (RG int i=m;i>m-ans;--i)
65                 printf("%d ",shop[i].num);
66     putchar('
');
67     }
68 }
69 int main(){
70     //freopen("D.in","r",stdin);
71     //freopen("D.out","w",stdout);
72     work();
73     //fclose(stdin);
74     //fclose(stdout);
75     return 0;
76 }
View Code
原文地址:https://www.cnblogs.com/Super-Nick/p/6414038.html