日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

      歡迎訪問 生活随笔!

      生活随笔

      當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

      编程问答

      【CodeForces-1041C】Coffee Break(贪心,STL,set二分维护,题意难,有坑,SJ题,构造)(知识点总结)

      發布時間:2023/12/10 编程问答 34 豆豆
      生活随笔 收集整理的這篇文章主要介紹了 【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 2

      Output

      3 3 1 1 2

      Input

      10 10 1 10 5 7 4 6 3 2 1 9 8

      Output

      2 2 1 1 2 2 1 2 1 1 2

      Note

      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题,构造)(知识点总结)的全部內容,希望文章能夠幫你解決所遇到的問題。

      如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

      主站蜘蛛池模板: 日韩久久精品电影 | 日韩中文字幕2019 | 亚洲欧美综合视频 | 欧洲mv日韩mv国产 | 男ji大巴进入女人视频 | 日韩av少妇 | 九九免费精品视频 | 国产女女调教女同 | 91精品国产自产91精品 | 精品日韩欧美 | 午夜人体视频 | 日韩欧美精品在线播放 | 少妇久久久久久久久久 | 一二三区精品 | 久久亚洲私人国产精品va | 精品一区二区三区免费观看 | aa毛片视频 | 久久久久久不卡 | 91精品亚洲 | 国产又大又黄的视频 | 国产av电影一区二区三区 | 福利视频导航网 | 国产激情视频 | 久久久久成人精品 | 黄色a区 | 中文理论片 | 天天干天天搞天天射 | 欧美午夜激情视频 | 久久精品网 | 综合色在线观看 | 日本成人在线不卡 | 中文字幕亚洲天堂 | 午夜在线影院 | 国产精品尤物 | 欧美精品免费一区二区 | 欧美精品久 | 久久水蜜桃 | 69视频一区| www.色妞| 免费av黄色 | 久草播放 | av国产成人 | 日韩一区二区三区视频在线观看 | 国产精品福利在线 | 日本一区二区成人 | www.超碰| 综合爱爱网 | 欧美色视 | 欧美另类一区 | 在线免费看av | 亚洲激情电影在线 | 久久九精品 | 一区二区三区免费 | 免费在线毛片 | 国产精品久久久久久久久久久久久久 | 一级片欧美 | 久久久精品 | 国产清纯在线 | 91精品国产电影 | 中文字幕一本 | 国产精品伦理一区 | 青青草狠狠干 | 国产夫妻露脸 | 亚洲综合av一区二区 | 久久aⅴ国产欧美74aaa | 在线观看黄色免费视频 | 国产裸体美女永久免费无遮挡 | 乌克兰毛片 | 四虎影| 欧美熟妇精品一区二区蜜桃视频 | 亚洲一区欧美日韩 | 天堂中文在线官网 | 成人一级生活片 | 成人一卡二卡 | 91成人破解版 | 亚洲美女精品 | 婷婷五月小说 | 91网址在线观看 | 国产视频一区二区在线观看 | 少妇激情视频 | 亚洲精品视频免费观看 | av性天堂网| 97视频免费在线 | 超碰人人人人人人 | 日本bbwbbw | 国产免费又黄又爽又色毛 | 国产色一区二区 | 亚洲网站在线观看 | 欧美福利小视频 | 亚洲色图日韩 | 精品一区电影 | 一二三区精品视频 | 天堂国产一区二区三区 | 久草日韩 | 午夜一区在线 | 国产高清一区二区三区 | 色干网| 国产美女特级嫩嫩嫩bbb | 国产免费又粗又猛又爽 |