CodeForces - 372CWatching Fireworks is Fun+DP+單調隊列優化

【題目描述】
CodeForces - 372CWatching Fireworks is Fun
題目的大概意思就是在一個編號為1…n的街道上現在按照時間順序放煙花,每個煙花獲得的幸福感為b?abs(a?x)b-abs(a-x)b?abs(a?x),x為觀看煙花的位置,為了提升我們的幸福感,我們可能會移動,每個時間單位可以移動d長度,現在問我們如果可以從任何一個地點開始觀看煙花,那么最后幸福感最大是多少
【題目分析】
連我這樣不太會DP的人都能看出來這是一個DP,按照放煙花的順序dp[i][j]=max(dp[i?1][k])+b?abs(a[i]?x)dp[i][j]=max ( dp[i-1][k] )+b-abs(a[i]-x)dp[i][j]=max(dp[i?1][k])+b?abs(a[i]?x),其中k為所有可以到達j位置的點,即i?t?d&lt;=k&lt;=i+t?di-t*d&lt;=k&lt;=i+t*di?t?d<=k<=i+t?d,t是距離上次放煙花的時間差
可是這樣做的話就需要對每一個煙花都遍歷一個很大的區間,應該會超時,所以我們需要進行優化。
我們對于每個煙花,我們都 用一個隊列從前往后計算每個位置,隊列中保存的是能到達當前位置的所有區域中幸福感最大的(按照從前往后的順序),如果后面某個位置的幸福感比前面的大,就會將前面的彈出,再將后面的放進去,因為對于再往后的位置來講,后面這個幸福感更大的位置更有用(前面的能到的后面的一定能到,后面能到的前面的不一定能到,而且前面的值還沒有后面的大,所以就不用考慮他了,這也算是一種貪心吧)
可能這樣說有點繞,可以先看代碼,注意理解雙重循環的部分,再回來看就應該很好理解了。
為了優化空間,我們用一個二維的數組滾動的保存數據,s0保存的是還沒有放這個煙花的幸福感,s1保存的是放了煙花后的幸福感,對于下一個煙花,將s0和s1調換就可以了,最后s0保存的就是最后的結果,查找最大值就可以了。
【AC代碼】

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<climits>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;typedef long long ll;
const int MAXN=150005;
ll dp[2][MAXN];
ll a,b,t,n,m,d,s0,s1,tt=1,step;int main()
{scanf("%lld%lld%lld",&n,&m,&d);s0=0; s1=1;while(m--){deque<int> q;scanf("%lld%lld%lld",&a,&b,&t);step=(t-tt)*d; tt=t;for(int i=1,j=1;i<=n;i++){for(;j<=i+step&&j<=n;j++){while(!q.empty() && dp[s0][q.back()]<=dp[s0][j]) q.pop_back();	//后面的值還比前面的大,前面的就沒用了q.push_back(j);	//不管有沒有前面的彈出,后面的暫時都是有用的,除非更后面的將他擠出去}while(!q.empty() && q.front()<i-step) q.pop_front();	//如果隊列剛開始的地方已經不能到達位置i,就彈出。雖然可能他的幸福感很高,但是對后面的值已經沒有影響了。dp[s1][i]=dp[s0][q.front()]+b-abs(a-i);}swap(s0,s1);}ll ans=dp[s0][1];for(int i=2;i<=n;i++){if(dp[s0][i]>ans) ans=dp[s0][i];}printf("%lld",ans);return 0;
}

【參考博客】
https://www.cnblogs.com/yehs/p/11331813.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/384092.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/384092.shtml
英文地址,請注明出處:http://en.pswp.cn/news/384092.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

雙向鏈表的基本操作

1.雙向鏈表的數據結構 typedef char DLinkType;typedef struct DLinkNode { DLinkType data; struct DLinkNode* next; struct DLinkNode* prev; }DLinkNode; 雙向帶頭結點的鏈表有三個成員&#xff0c; 一個是數據&#xff0c; 一個是指針 next 指向當前結點的下一個結點&…

匿名管道

1.進程通信的目的 (1) 數據傳輸: 一個進程需要將它的數據傳輸給另一個進程 ????(2) 資源共享: 多個進程之間共享同樣的資源 ????(3) 通知事件: 一個進程需要向另一個或一組進程發送消息, 通知它們發生了什么事情 2.管道 管道是一種進程之間通信的一種方式, 我們把從…

Currency Exchange——最短路Bellman-Ford算法

【題目描述】 Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange operations only with these currencies. There can be several points specializing in the sam…

C++實現String類

http://blog.csdn.net/randyjiawenjie/article/details/6709539 C實現String類&#xff0c;還沒有完成&#xff0c;待繼續。 有以下注意的點&#xff1a; &#xff08;1&#xff09;賦值操作符返回的是一個MyString&&#xff0c;而重載的返回的是一個MyString。其中的原因…

POJ 3370 Halloween treats——鴿巢原理+思維

【題目描述】 POJ 3370 Halloween treats Description Every year there is the same problem at Halloween: Each neighbour is only willing to give a certain total number of sweets on that day, no matter how many children call on him, so it may happen that a chi…

將信號量代碼生成靜態庫以及動態庫

1.信號量相關代碼生成靜態庫 2.信號量相關代碼生成動態庫

Wormholes——Bellman-Ford判斷負環

【題目描述】 While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is BEFORE you entered the wormhole! Each of…

C++11 標準新特性:Defaulted 和 Deleted 函數

https://www.ibm.com/developerworks/cn/aix/library/1212_lufang_c11new/index.html Defaulted 函數 背景問題 C 的類有四類特殊成員函數&#xff0c;它們分別是&#xff1a;默認構造函數、析構函數、拷貝構造函數以及拷貝賦值運算符。這些類的特殊成員函數負責創建、初始化、…

順序表實現棧相關操作

1.棧的相關概念 棧是一種特殊的線性表, 其中只允許在固定的一端進行插入和刪除元素.進行數據插入和刪除的一端叫做棧頂, 另一端成為棧底. 不含任何元素的棧稱為空棧, 棧又稱為先進先出的線性表. 2. 順序棧的結構 3. 順序棧的具體操作 (1). 數據結構 typedef char SeqStackTyp…

MPI Maelstrom——Dijkstra

【題目描述】 BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distributed shared memory machine with a hierarchical communication subsystem. Valentine McKee’s research advisor, Jack Swigert, has asked her to bench…

雙向帶環帶頭結點的鏈表實現棧

1. 數據結構 利用帶頭結點帶環的結點實現棧的相關操作.因此, 每一個結點包括了一個前驅, 一個后繼, 還有一個數據成員 typedef char DLinkStackType;typedef struct DLinkStack {DLinkStackType data;struct DLinkStack* next;struct DLinkStack* prev; }DLinkStack;2. 初始化…

Cow Contest——Floyed+連通性判斷

【題目描述】 N (1 ≤ N ≤ 100) cows, conveniently numbered 1…N, are participating in a programming contest. As we all know, some cows code better than others. Each cow has a certain constant skill rating that is unique among the competitors. The contest …

C++11 標準新特性:委派構造函數

https://www.ibm.com/developerworks/cn/rational/1508_chenjing_c11/index.html陳 晶2015 年 8 月 11 日發布WeiboGoogle用電子郵件發送本頁面 1本文首先介紹了在委派構造函數提出之前類成員構造所面臨的問題&#xff0c;再結合實例介紹了委派構造函數的用法&#xff0c;并說明…

順序表實現隊列

一. 隊列相關概念 隊列是只允許在一段進行插入元素, 在另一端進行刪除元素的線性表,即只允許對隊列進行尾插,頭刪的操作.隊列具有先進先出, 后進后出的特性. ???????? 1.初始化 void SeqQueInit(SeqQue* q) {if(q NULL){return;//非法輸入}q -> head 0;q -> …

Arbitrage——判斷正環Bellman-Ford/SPFA

【題目描述】 Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a currency into more than one unit of the same currency. For example, suppose that 1 US Dollar buys 0.5 British pound, 1 British pound buys 10.0 French …

鏈表實現隊列

上篇博客是用順序表實現隊列, 現在用雙向帶頭結點帶環鏈表實現對隊列的出隊列, 入隊列, 取隊首元素, 以及銷毀隊列的相關操作 1.初始化鏈表 void DLinkQueInit(DLinkQue** q) {if(q NULL){return;//非法輸入}if(*q NULL){return;//非法輸入帶頭結點的鏈表至少有一個傀儡結點…

HDU - 1796——容斥原理+二進制枚舉

【題目描述】 Now you get a number N, and a M-integers set, you should find out how many integers which are small than N, that they can divided exactly by any integers in the set. For example, N12, and M-integer set is {2,3}, so there is another set {2,3,4,…

數據結構學習(二)——單鏈表的操作之頭插法和尾插法創建鏈表

http://blog.csdn.net/abclixu123/article/details/8210109 鏈表也是線性表的一種&#xff0c;與順序表不同的是&#xff0c;它在內存中不是連續存放的。在C語言中&#xff0c;鏈表是通過指針相關實現的。而單鏈表是鏈表的其中一種&#xff0c;關于單鏈表就是其節點中有數據域和…

信號的基本概念以及信號的產生

一. 信號產生的場景 1. 用戶輸入命令, 在shell 啟動一個前臺進程 ???? 2. 當用戶按一下 Ctrl C 的時候,從鍵盤產生一個硬件中斷 ???? 3. 此時CPU 正在執行這個進程的帶代碼, 則該進程的執行代碼暫停執行, CPU 從用戶態切換到內核態處理該硬件中斷. ???? 4. 中斷…

HDU - 1028——母函數入門

【題目描述】 “Well, it seems the first problem is too easy. I will let you know how foolish you are later.” feng5166 says. “The second problem is, given an positive integer N, we define an equation like this: Na[1]a[2]a[3]…a[m]; a[i]>0,1<m<N;…