﻿ Codeforces 635E Package Delivery【贪心】 - 鸿网互联

# Codeforces 635E Package Delivery【贪心】

## 题目链接：

http://codeforces.com/contest/635/problem/E

## 代码：

#include<cstdio>
#include<stack>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
#define pr(x) cout << #x << ": " << x << "  "
#define pl(x) cout << #x << ": " << x << endl;
#define sa(x) scanf("%d",&(x))
#define sal(x) scanf("%I64d",&(x))
#define xx first
#define yy second
#define mdzz cout<<"mdzz"<<endl;
const int maxn = 2e5 + 5, oo =0x3f3f3f3f;
typedef pair<int, int>p;
typedef long long ll;
int nt[maxn];
p s[maxn];
/*贪心 对于每个位置找最近的最小的，判断距离，选择充多少*/
int main (void)
{
int d, n, m;sa(d), sa(n), sa(m);
for(int i = 1; i <= m; i++){
int x, y;
scanf("%d%d", &x, &y);
s[i] =  p(x, y);
}
s[0] = p(d, 0);
s[m + 1] = p(0, oo);
m += 2;
sort(s, s + m);
//倒着推一遍找下一个最小的
stack<int>q;
for(int i = m - 1; i >= 0; i--){
while(!q.empty() && s[i].yy <= s[q.top()].yy) q.pop();
if(q.empty()) nt[i] = -1;
else nt[i] = q.top();
q.push(i);
}
ll ans = 0;
int now = n;//还剩多少
for(int i = 0; i < m; i++){
if(now < 0) return puts("-1"), 0;
if(nt[i] == -1){//最小的了,加到走到最后
}else add = s[nt[i]].xx - s[i].xx;
ans += (add - now) * 1ll *  s[i].yy;