2016-2017 ACM-ICPC Northeastern European regional Contest problem E. Expect to Wait

Source: Internet
Author: User

Title Source: http://codeforces.com/group/aUVPeyEnI2/contest/229509
Time limit: 2s
Space limit: 512MB
Main topic:
In a station there are several people waiting for the vehicle in the queue, asking everyone to wait for the expectation of time
First given N and Q, followed by N-Line operations:
"+ t K": at t time there are k individuals joining the queue waiting for the vehicle
"-t K": at t time there is k personal bus leaving the queue
Then there's the Q number that indicates how many cars are waiting in the station at the beginning of the hour.
Find the waiting time for each person asking for the corresponding, and output "INFINITY" If someone never waits for the car
Examples:

Code:

#include <algorithm> #include <iostream> #include <cstring> #include <vector> #include < cstdio> #include <string> #include <cmath> #include <queue> #include <set> #include <map > #include <complex>using namespace std;typedef long long ll;typedef long double db;typedef pair<int,int> p Ii;typedef vector<int> VI; #define DE (x) cout << #x << "=" << x << endl#define Rep (i,a,b) for (int i= (a);i< (b); i++) #define ALL (x) (x). Begin (), (x). End () #define SZ (x) (int) (x). Size () #define MP Make_pair#define Pb push_back#define fi first#define se second#define pi acos ( -1.0) #define MEM0 (a) memset (A,0,sizeof (a)) #define MEMF (b) Me Mset (b,false,sizeof (b)) #define LL long long#define EPS 1e-10#define inf 1e17#define maxn 101010int N, q;int A[maxn], T[max    N];int S[maxn];int B[MAXN], Cnt;long long ans[maxn];struct node{int x, ID;    BOOL operator < (const node &AMP;RHS) const{return x < rhs.x; }}c[maxn];boOL cmp (int i, int j) {return s[i] > S[j];}    int main () {freopen ("expect.in", "R", stdin);    Freopen ("Expect.out", "w", stdout);    scanf ("%d%d", &n, &q);        for (int i = 1; I <= n; i++) {char op[5];        scanf ("%s%d%d", OP, &t[i], &a[i]);    if (op[0] = = '-') a[i] =-a[i];        } for (int i = 1; I <= n; i++) {s[i] = S[i-1] + a[i];    if (i < n) t[i] = t[i+1]-t[i]; }//for (int i = 1; I <= n; i++) {//printf ("s[%d] =%d, t[%d] =%d\n", I, s[i], I, t[i]);/} Long Long    sum1 = 0, sum2 = 0;            for (int i = 1; I <= n; i++) {if (S[i] < 0) {b[++cnt] = i;            Sum1 + = 1ll* (-s[i]) *t[i];        Sum2 + = T[i];    }} sort (b + 1, B + CNT + 1, CMP);    int j = 1;    A long long k = 0;        for (int i = 1; I <= Q; i++) {scanf ("%d", &c[i].x);    C[i].id = i;    } sort (c + 1, c + q + 1);        for (int i = 1; I <= Q; i++) {int x = c[i].x; while (J <= CNT && (-s[b[j]]) <= x) {sum1-= 1ll* (-s[b[j]]) * T[B[J]];            SUM2-= t[b[j]];        ++j;        } if (S[n] + x < 0) {Ans[c[i].id] =-1;        } else{Ans[c[i].id] = sum1-x * SUM2;        }} for (int i = 1; I <= Q; i++) {if (ans[i] = = 1) printf ("infinity\n");    else printf ("%lld\n", Ans[i]); } return 0;}

2016-2017 ACM-ICPC Northeastern European regional Contest problem E. Expect to Wait

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.