-
Notifications
You must be signed in to change notification settings - Fork 0
/
luogu1016.cpp
73 lines (66 loc) · 1.83 KB
/
luogu1016.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//贪心
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
struct ss{
double d,p;
}a[10];
bool cmp(ss a,ss b){
return a.d<b.d;
}
int main(){
double d1,c,d2,p;
int n;
cin>>d1>>c>>d2>>p>>n;
for(int i=1;i<=n;i++){
cin>>a[i].d>>a[i].p;
// a[i].num=i;
}
a[n+1].d=d1;
a[0].p=p;
a[0].d=0;
sort(a,a+n+1,cmp);
//for(int i=0;i<=n;i++)cout<<a[i].p<<' '<<a[i].d<<' '<<i<<'\n';
double curr=0;
double cost=0;
for(int i=0;i<=n;){
int j=i;
while(a[j].p>=a[i].p)j++;
if(d2*c>=a[j].d-a[i].d){
double gasneeded=((a[j].d-a[i].d)/d2);
if(gasneeded>curr){
cost+=(gasneeded-curr)*a[i].p;
//cout<<"add gas at "<<i<<" for "<<(gasneeded-curr)*a[i].p<<'\n';
curr=gasneeded;
}
curr-=gasneeded;
i=j;
//continue;
}
if(d2*c<a[j].d-a[i].d){
int k=i+1;//STILL DONT KNOW WHY WHEN K=1 WILL TLE!!!
//When k=i, yy.d will always = i, making it impossible to proceed.
struct ss yy;
yy.p=999999;
while(a[k].d-a[i].d<d2*c){
//k++;
//cout<<k<<' '<<a[k].p<<' '<<yy.p<<'\n';
if(a[k].p<yy.p)yy.d=k,yy.p=a[k].p;
k++;
}
if(a[i+1].d-a[i].d>d2*c){cout<<"No Solution";return 0;}
cost+=(c-curr)*a[i].p;
//cout<<"add gas full at "<<i<<" for "<<(c-curr)*a[i].p<<'\n';
//cout<<"Heading to "<<yy.d<<'\n';
//cout<<"Best Offer "<<yy.p<<'\n';
curr=c-((a[(int)yy.d].d-a[i].d)/d2);
i=yy.d;
//continue;
}
//cout<<curr;
//cout<<cost<<'\n';
}
printf("%.2f\n",(double)cost);
return 0;
}