洛谷P2672 推销员

洛谷P2672 推销员
noip2015普及 T4
堆 贪心

 1 #include <bits/stdc++.h>
 2 #define LL long long
 3 #define GG LL
 4 #define For(i, j, k) for(register int i=j; i<=k; i++)
 5 #define Dow(i, j, k) for(register int i=j; i>=k; i--)
 6 using namespace std;
 7 inline GG read() {
 8     GG x = 0, f = 1;
 9     char ch = getchar();
10     while(ch<'0'||ch>'9') { if(ch=='-') f = -1; ch = getchar(); }
11     while(ch>='0'&&ch<='9') { x = x*10+ch-48; ch = getchar(); }
12     return x * f;
13 }
14 void write(GG x) {
15     if(x<0) putchar('-'), x = -x;
16     if(x>9) write(x/10);
17     putchar(x%10+48);
18 }
19 inline void writeln(GG x) { write(x); putchar('\n'); }
20 
21 const int N = 100011; 
22 int n; 
23 struct node{
24     int id; 
25     LL val; 
26     friend bool operator <(node a, node b) {
27         return a.val < b.val; 
28     }
29 }; 
30 struct arr{
31     int pos; 
32     LL val; 
33 }a[N];
34 LL ans; 
35 
36 bool cmp_pos(arr a, arr b) {
37     return a.pos < b.pos; 
38 }
39 
40 priority_queue<node> Ql; 
41 priority_queue<node> Qr;  
42 
43 inline void work() {
44     int mid = 0; 
45     LL Mx = -1; 
46     For(i, 1, n) 
47         if(2ll*a[i].pos+a[i].val > Mx) {
48             Mx = 2ll*a[i].pos+a[i].val; 
49             mid = i; 
50         }
51     ans = 2ll*a[mid].pos+a[mid].val; 
52     writeln(ans); 
53     For(i, 1, mid-1) Ql.push( (node){i, a[i].val } ); 
54     For(i, mid+1, n) Qr.push( (node){i, 2ll*a[i].pos+a[i].val } ); 
55     For(i, 2, n) {
56         int l, r;  LL M1, M2; 
57         if(!Ql.empty()) l = Ql.top().id; 
58         else l = -1; 
59         while(!Qr.empty() && a[mid].pos > a[Qr.top().id].pos) Qr.pop(); 
60         if(!Qr.empty()) r = Qr.top().id; 
61         else r = -1; 
62         M1 = a[l].val; M2 = 2ll*a[r].pos+a[r].val-2ll*a[mid].pos; 
63         if(l!=-1 && (r==-1 || M1 > M2)) {
64             ans += M1; 
65             Ql.pop(); 
66         }
67         else {
68             ans += M2; 
69             For(i, mid+1, r-1) Ql.push( (node){ i, a[i].val} ); 
70             Qr.pop(); 
71         }
72         writeln(ans); 
73     }
74 }
75 
76 int main() {
77     n = read(); 
78     For(i, 1, n) a[i].pos=read(); 
79     For(i, 1, n) a[i].val=read(); 
80     sort(a+1, a+n+1, cmp_pos); 
81     work();  
82 }

 

posted @ 2018-02-27 09:46  third2333  阅读(196)  评论(0编辑  收藏  举报