hdu5380 貪心+雙端佇列
http://acm.hdu.edu.cn/showproblem.php?pid=5380
Problem Description
There are n+1 cities on a line. They are labeled from city 0 to city n. Mph has to start his travel from city 0, passing city 1,2,3...n-1 in order and finally arrive city n. The distance between city i and city 0 is ai.
Mph loves candies and when he travels one unit of distance, he should eat one unit of candy. Luckily, there are candy shops in the city and there are infinite candies in these shops. The price of buying and selling candies in city i is buyi and selli per
unit respectively. Mph can carry at most C unit of candies.
Now, Mph want you to calculate the minimum cost in his travel plan.
Now, Mph want you to calculate the minimum cost in his travel plan.
Input
There are multiple test cases.
The first line has a number T, representing the number of test cases.
For each test :
The first line contains two numbers N and C (N≤2×105,C≤106)
The second line contains N numbers a1,a2,...,an. It is guaranteed that ai>ai−1 for each 1<i<=N .
Next N+1 line : the i-th line contains two numbers buyi−1 and selli−1 respectively. (selli−1≤buyi−1≤106)
The sum of N in each test is less than 3×105.
The first line has a number T, representing the number of test cases.
For each test :
The first line contains two numbers N and C (N≤2×105,C≤106)
The second line contains N numbers a1,a2,...,an. It is guaranteed that ai>ai−1 for each 1<i<=N .
Next N+1 line : the i-th line contains two numbers buyi−1 and selli−1 respectively. (selli−1≤buyi−1≤106)
The sum of N in each test is less than 3×105.
Output
Each test case outputs a single number representing your answer.(Note: the answer can be a negative number)
Sample Input
1
4 9
6 7 13 18
10 7
8 4
3 2
5 4
5 4
Sample Output
105
/**
hdu5380 貪心+雙端佇列
題目大意:一個人從0走到n知道ai為i節點到0的距離,沒行走單位距離要消耗一顆糖,在所有節點中可以進行買糖和賣糖價格為sell[i]和buy[i],問走到n節點話費最小為多少
解題思路:從0開始,每次都把當前攜帶的糖的數量為C,到下一個節點,如果賣的價格高的話就把當前口袋裡剩的價錢較低買的換成當前點賣的價格(因為當前剩的糖是多餘的
走到最後是要被退掉的,所以我們此舉把退的價格抬高了),然後把前一個節點到當前節點路上消耗的糖在現在買回來,保持攜帶糖為C,走到n後把所有剩的糖退掉
思想有點難理解,需要好好想一想,貼上標程
*/
#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
using namespace std;
typedef long long LL;
const int MAXN=200000+1000;
struct que
{
int val,cnt;
} Q[MAXN*2];
int l,r,tot;
LL ans;
void Max(int v)
{
int num=0;
while(l<=r&&Q[l].val<v)
{
num+=Q[l].cnt;
l++;
}
if(num)
{
--l;
Q[l].cnt=num;
Q[l].val=v;
}
}
void Min(int v)
{
while(l<=r&&Q[r].val>v)
{
ans-=1LL*Q[r].val*Q[r].cnt;
tot+=Q[r].cnt;
--r;
}
}
void Del(int v)
{
while(v)
{
int t=min(Q[l].cnt,v);
Q[l].cnt-=t;
v-=t;
if(Q[l].cnt==0)++l;
}
}
int A[MAXN],n,c,sell[MAXN],buy[MAXN];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&c);
A[0]=0;
for(int i=1; i<=n; i++)scanf("%d",&A[i]);
for(int i=0; i<=n; i++)scanf("%d%d",&buy[i],&sell[i]);
l=r=n;
--r;
ans=0;
for(int i=0; i<n; i++)
{
//將買入價小於賣出價的合併
Max(sell[i]);
//補充使得滿油
tot=(i==0)?c:A[i]-A[i-1];
//將買入價大於當前買入價的油都退了,更新答案並計算需要補充的油tot
Min(buy[i]);
//將買入的油數量和單價入佇列
Q[++r].val=buy[i];
Q[r].cnt=tot;
ans+=1LL*buy[i]*tot;
//消化從i...i+1這個點的油(最便宜的
Del(A[i+1]-A[i]);
}
//更新最後一個點
Max(sell[n]);
//把多餘的油退掉
for(int i=l; i<=r; i++)ans-=1LL*Q[i].val*Q[i].cnt;
printf("%I64d\n",ans);
}
return 0;
}
相關文章
- 雙端佇列佇列
- 單調佇列雙端佇列佇列
- 8.13(優先佇列貪心維護+打表找規律+對頂堆優先佇列+DFS減枝+貪心dp)佇列
- CodeForces - 960B:Minimize the error(優先佇列+貪心)Error佇列
- Sunscreen POJ - 3614(防曬油) 貪心-優先佇列佇列
- [USACO 2009 Feb Gold] Fair Shuttle (貪心+優先佇列)GoAI佇列
- python雙端佇列的原理分析Python佇列
- 三、資料結構演算法-棧、佇列、優先佇列、雙端佇列資料結構演算法佇列
- PTA 雙端佇列 資料結構佇列資料結構
- 資料結構之「雙端佇列」資料結構佇列
- 佇列-單端佇列佇列
- ArrayDeque雙端佇列 使用&實現原理分析佇列
- ArrayDeque(JDK雙端佇列)原始碼深度剖析JDK佇列原始碼
- python資料結構與演算法——棧、佇列與雙端佇列Python資料結構演算法佇列
- 【力扣】最大子陣列和(貪心)力扣陣列
- 遞推,遞迴,貪心,列舉思想遞迴
- 貪心
- LeetCode題解:641. 設計迴圈雙端佇列,使用佇列,JavaScript,詳細註釋LeetCode佇列JavaScript
- L2-014 列車排程【貪心】
- 面經手冊 · 第9篇《佇列是什麼?什麼是雙端佇列、延遲對列、阻塞佇列,全是知識盲區!》佇列
- 前端學習 資料結構與演算法 快速入門 系列 —— 佇列和雙端佇列前端資料結構演算法佇列
- 反悔貪心
- Supermarket(貪心)
- huawei0821筆試第二題筆記:雙端佇列deque筆試筆記佇列
- HDU - 6003 Problem Buyer題解(貪心選擇演算法,鴿巢原理,優先佇列維護)演算法佇列
- 貪心例題
- 貪心+搜尋
- 全網最適合入門的物件導向程式設計教程:41 Python 常用複合資料型別-佇列(FIFO、LIFO、優先順序佇列、雙端佇列和環形佇列)物件程式設計Python資料型別佇列
- 025 通過連結串列學Rust之使用棧實現雙端佇列Rust佇列
- 025 透過連結串列學Rust之使用棧實現雙端佇列Rust佇列
- HDU 5821 Ball(貪心)
- 貪心模式記錄模式
- 反悔貪心雜題
- 貪心演算法演算法
- 貪心、構造合集
- 佇列、阻塞佇列佇列
- [藍橋杯][演算法提高VIP]最大乘積 貪心 雙指標演算法指標
- 貪心-刪數問題
- 貪心-*活動選擇