[NOIP2015] Salesman

Source: Internet
Author: User

[NOIP2015] Salesman

Question Description

Amin, a salesman, was instructed to sell their company's products to Screw street. Screw Street is a dead end, the exit is the same as the entrance, the side of the street is a fence and the other side is a resident. Screw Street A total of N households, the first household to the entrance of the distance of Si meters. As there are many households in the same house, there may be a number of households with the same distance from the entrance. Amin will enter from the entrance, in turn to screw street X household marketing products, and then go out of the original road. Amin will accumulate 1 points of fatigue value every 1 meters, and sell products to the first household will accumulate Ai point fatigue value. Amin is a workaholic, he wants to know, for the different X, in the premise of not taking the extra road, he can accumulate the maximum number of points of fatigue value.

Input

The first line has a positive integer n, which indicates the number of street households, and the next line has N positive integers, of which the I-integer Si indicates that the first household to the inlet distance guarantees the S1<=S2<=S3....<10 8-time side. The next line has N integers, where the I integer Ai indicates that selling the product to the first household will accumulate fatigue values. Guarantee the ai<=10 3-time side.

Output

Output N lines, one positive integer per line, where the line I integer represents the fatigue value accumulated when x=i, Amin.

Input example

5 1 2 3 4 5 1 2 3 4 5

Output example

 the  +  A  -  -

Data size and conventions

1<= N <= 1000000

Exercises

NOIP Universal group also has dignity!

It is not difficult to think that the optimal scheme of x= I must be based on the optimal scheme of X = I-1, plus one more publicity object.

Consider the choice of X = 1 o'clock, which is obviously the largest of all households in AI + 2Si, if there are multiple residents of the same AI + 2Si, then the first choice Si min (think about why).

Then the sequence is divided into the left and right two parts, the choice to get the contribution of AI, the choice of the rights of the tenants to obtain AI + 2 (si-t) contribution, T represents the current dividing line to the alley entrance distance, note that the right part of the contribution of the size of the relationship did not change initially, Just re-order the contribution of the left household . A new priority queue can then be built to join all the tenants on the left, discarding the elements in the original priority queue that are divided into the left part. Since the dividing line is shifted to the right, each element is added two times, deleted two times, and the total time complexity is O (NLOGN).

#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <stack > #include <vector> #include <queue> #include <cstring> #include <string> #include <map > #include <set>using namespace std;const int buffersize = 1 << 16;char buffer[buffersize], *head, *TAIL;INL        ine Char Getchar () {if (Head = = Tail) {int L = fread (buffer, 1, buffersize, stdin);    Tail = (Head = buffer) + L; } return *head++;}    int read () {int x = 0, f = 1; char tc = GetChar ();    while (!isdigit (TC)) {if (TC = = '-') f =-1; tc = GetChar ();}    while (IsDigit (TC)) {x = x * + TC-' 0 '; tc = GetChar ();} return x * f;} #define MAXN 1000010#define LL long longint N, S[maxn], A[maxn];bool has[maxn];struct heapnode {int id, x; LL Val;bool operator < (const heapnode& t) const {return val! = t.val? val < t.val:x > t.x;}} ; Heapnode Max (Heapnode A, Heapnode b) {if (a < b) return b;return A;} Priority_Queue <HeapNode> Q, Q2;int Main () {n = read (), for (int i = 1; I <= n; i++) s[i] = read (); for (int i = 1; I <= N; i++) A[i] = read (); while (! Q.empty ()) Q.pop (); while (! Q2.empty ()) Q2.pop (); for (int i = 1; I <= n; i++) Q.push ((Heapnode) {i, s[i], (LL) a[i] + 2ll * s[i]}); int T = 0, Tid = 0; LL ans = 0;for (int i = 1; I <= n; i++) {Heapnode u; u = (heapnode) {0, 0, 0};if (! Q.empty ()) {u = q.top (); Q.pop (); while (u.x <= T &&! Q.empty ()) U = q.top (), Q.pop (), if (u.x > t && q.empty ()) U = (heapnode) {0, 0, 0};else u.val-= 2ll * T;} Heapnode v; v = (Heapnode) {0, 0, 0};if (! Q2.empty ()) v = q2.top (), Q2.pop (); u = Max (U, v); Has[u.id] = 1;ans + = u.val;printf ("%lld\n", ans); if (u.x > t) {T = U.x;for (++tid; Tid <= n && s[tid] <= T; tid++) if (!has[tid]) Q2.push ((Heapnode) {Tid, S[tid], A[tid]});}} return 0;}

[NOIP2015] Salesman

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.