【CodeForces-1041C】Coffee Break(贪心,STL,set二分维护,题意难,有坑,SJ题,构造)(知识点总结)
題干:
Recently Monocarp got a job. His working day lasts exactly?mm?minutes. During work, Monocarp wants to drink coffee at certain moments: there are?nn?minutes?a1,a2,…,ana1,a2,…,an, when he is able and willing to take a coffee break (for the sake of simplicity let's consider that each coffee break lasts exactly one minute).
However, Monocarp's boss doesn't like when Monocarp takes his coffee breaks too often. So for the given coffee break that is going to be on minute?aiai, Monocarp must choose the day in which he will drink coffee during the said minute, so that every day at least?dd?minutes pass between any two coffee breaks. Monocarp also wants to take these?nn?coffee breaks in a minimum possible number of?working?days (he doesn't count days when he is not at work, and he doesn't take coffee breaks on such days). Take into account that more than?dd?minutes pass between the end of any working day and the start of the following working day.
For each of the?nn?given minutes determine the day, during which Monocarp should take a coffee break in this minute. You have to minimize the number of days spent.
Input
The first line contains three integers?nn,?mm,?dd?(1≤n≤2?105,n≤m≤109,1≤d≤m)(1≤n≤2?105,n≤m≤109,1≤d≤m)?— the number of coffee breaks Monocarp wants to have, the length of each working day, and the minimum number of minutes between any two consecutive coffee breaks.
The second line contains?nn?distinct integers?a1,a2,…,ana1,a2,…,an?(1≤ai≤m)(1≤ai≤m), where?aiai?is some minute when Monocarp wants to have a coffee break.
Output
In the first line, write the minimum number of days required to make a coffee break in each of the?nn?given minutes.
In the second line, print?nn?space separated integers. The?ii-th of integers should be the index of the day during which Monocarp should have a coffee break at minute?aiai. Days are numbered from?11. If there are multiple optimal solutions, you may print any of them.
Examples
Input
4 5 3 3 5 1 2Output
3 3 1 1 2Input
10 10 1 10 5 7 4 6 3 2 1 9 8Output
2 2 1 1 2 2 1 2 1 1 2Note
In the first example, Monocarp can take two coffee breaks during the first day (during minutes?11?and?55,?33?minutes will pass between these breaks). One break during the second day (at minute?22), and one break during the third day (at minute?33).
In the second example, Monocarp can determine the day of the break as follows: if the minute when he wants to take a break is odd, then this break is on the first day, if it is even, then this break is on the second day.
題目大意:
最近Monocarp找到了一份工作。他的工作時間正好是m分鐘。在工作期間,Monocarp想要在特定的時刻喝咖啡:有n分鐘a1,a2,…,an,當他能夠并且愿意喝咖啡休息(為了簡單起見,讓我們考慮一下每個咖啡休息時間正好持續1分鐘)。
然而,Monocarp的老板不喜歡Monocarp經常喝咖啡休息時間。所以對于給定的咖啡休息時間是在分鐘ai上,Monocarp必須選擇他在這一分鐘內喝咖啡的日期,以便在任何兩個咖啡休息時間之間每天至少有d分鐘的時間。Monocarp還希望在最短的工作日內享受這n個咖啡休息時間(他不計算他不工作的天數,而且他在這樣的日子里不喝咖啡)。考慮到任何工作日結束到下一個工作日開始之間超過d分鐘的時間。
在給定的n分鐘內決定一天的時間,在這一分鐘內單粒種子應該喝咖啡休息。你必須盡量減少花費的天數。
解題報告:
? ? 這題的題意確實是比較那懂,大致就是有某人要在n天內喝n杯咖啡(因為m貌似沒啥用,因為就算每天只喝一杯,最多就用n天喝完啊),喝每杯咖啡都花費1單位時間,如果兩杯咖啡在一天喝,那么它們之間至少要相隔d?min,要求輸出最少幾天可以喝完,并輸出對應題目輸入的每一杯咖啡是在第幾天喝的。
答案可以很多種但是我們只需要構造最好想的,我們可以貪心找出其中一組解。最早喝的那杯咖啡在某一天中一定是第一杯,我們就直接讓它在第一天被喝,由此可以推出下一杯咖啡時間至少要 >= ai + d?+ 1,于是一直往下找即可,如果找不到就說明這一天已經不能喝了,另開一天即可。
AC代碼:
#include<bits/stdc++.h> using namespace std; const int MAX = 2e5 + 10; const int INF = 0x3f3f3f3f; int a[MAX],ans[MAX]; set<pair<int,int> > ss; set<pair<int,int> > :: iterator it; int n,m,d,cnt; int main() {cin>>n>>m>>d;for(int i = 1; i<=n; i++) {scanf("%d",a+i);ss.insert(make_pair(a[i],i));}while(!ss.empty()) {int pos = ss.begin() -> second;ans[pos] = ++cnt;ss.erase(ss.begin());while(1) {it = ss.upper_bound(make_pair(a[pos]+d,INF));if(it == ss.end()) break;pos = it->second;ans[pos] = cnt;ss.erase(it);}}printf("%d\n",cnt);for(int i = 1; i<=n; i++) printf("%d%c",ans[i],i==n ? '\n' : ' ');return 0; }總結:
? ?還是提醒一些坑:
? ? ? ? 1.用了STL就一定要注意是否為空!it迭代器是否有效!這題巧了,不需要在while判斷if(空了) break;因為你lowerbound或者upperbound的時候如果沒找到(包括了set中已經沒元素的情況了),都直接就是ss.end()了,所以這些情況直接包含在這里break了。
? ? ? ? 2.這種語句套路也很常用啊,while(不空) 搞第一個;while(1) 然后 set中二分 , if(it == ss.end()) break;? 、、 ;類似這個意思。
? ? ? ? 3.關于pair和lowerbound。這題這么寫it = ss.lower_bound(make_pair(a[pos] + d + 1,0));也可以,但是
? ? ? ? ? ? ? ? ? ? ? ? ??it = ss.upper_bound(make_pair(a[pos] + d,0));? ?就不行!!!這牽扯到set中對pair類型默認排序的問題。
? ?你如果用upperbound? , pair的second就必須用INF,或者一個特別大的數。不然就會wa。
如果是int類型的set,就沒事。
set<int> s;set<int> :: iterator itt;s.insert(1);s.insert(2);s.insert(3);itt = s.upper_bound(2);cout << *itt; //輸出3但是pair類型,對于這個題。
3 3 1
1 2 3
這個樣例就過不了。輸出的就全是1,但是顯然不符合題意。
總結
以上是生活随笔為你收集整理的【CodeForces-1041C】Coffee Break(贪心,STL,set二分维护,题意难,有坑,SJ题,构造)(知识点总结)的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 【牛客 - 272A】Phrase St
- 下一篇: 建行信用卡逾期查询 掌握好三点技巧远离逾
