zoukankan      html  css  js  c++  java
  • ural2014 Zhenya moves from parents

    Zhenya moves from parents

    Time limit: 1.0 second
    Memory limit: 64 MB
    Zhenya moved from his parents’ home to study in other city. He didn’t take any cash with him, he only took his father’s credit card with zero balance on it. Zhenya succeeds in studies at the University and sometimes makes a little money on the side as a Maths tutor. As he makes his own money he spends only it, and when it is over he uses the credit card. Every time he gets or spends money, he sends a letter to his father, where he puts the following two things.
    1. The date when it took place
    2. The sum of earned or spent money
    Every time receiving a letter from Zhenya, his father calculates the debt on the credit card at the moment. But here a problem arises. The point is that Russian Post delivers letters in an order different to the one they were sent in.
    For example, in the first Zhenya’s letter the father read that on September 10 Zhenya spent one thousand rubles. He thought that his son had used the credit card, and now the debt is one thousand rubles. However the next day came a letter with the information that on September 9 Zhenya earned five hundred rubles. It means that half of the money he spent on September 10 was his own, and the debt on the credit card is just five hundred rubles.
    Help Zhenya’s father with his account management.

    Input

    The first line contains an integer n which is the number of Zhenya’s letters (1 ≤ n ≤ 100 000). These letters are listed in the next n lines. Description of each letter consists of the amount of money Zhenya spent or earned (in the form -c or +c accordingly, where c is an integer, 1 ≤ c ≤ 50 000) followed by both date and time when it took place (in the form of dd.MM hh:mm). All dates belong to the same year, which is not leap (i. e. there are 365 days in it). Any two letters contain either different dates or different time. The letters are listed in the order the father received them.

    Output

    After each received letter output what Zhenya’s father thinks the amount of the debt on the credit card is.

    Sample

    inputoutput
    5
    -1000 10.09 21:00
    +500 09.09 14:00
    +1000 02.09 00:00
    -1000 17.09 21:00
    +500 18.09 13:00
    
    -1000
    -500
    0
    -500
    -500
    

    分析:当前的盈亏始终对之后的盈亏有影响,且要维护整体最小值,所以用线段树解决;

       注意要用long long;

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <map>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <list>
    #define rep(i,m,n) for(i=m;i<=n;i++)
    #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define vi vector<int>
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    #define pii pair<int,int>
    #define Lson L, mid, rt<<1
    #define Rson mid+1, R, rt<<1|1
    const int maxn=1e5+10;
    const int dis[4][2]={{0,1},{-1,0},{0,-1},{1,0}};
    using namespace std;
    ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p%mod;p=p*p%mod;q>>=1;}return f;}
    int n,m,k,t,c[maxn];
    struct Node
    {
        ll Min, lazy;
    } T[maxn<<2];
    void PushUp(int rt)
    {
        T[rt].Min = min(T[rt<<1].Min, T[rt<<1|1].Min);
    }
    void PushDown(int L, int R, int rt)
    {
        int mid = (L + R) >> 1;
        ll t = T[rt].lazy;
        T[rt<<1].Min += t;
        T[rt<<1|1].Min +=  t;
        T[rt<<1].lazy += t;
        T[rt<<1|1].lazy += t;
        T[rt].lazy = 0;
    }
    void Update(int l, int r, int v, int L, int R, int rt)
    {
        if(l==L && r==R)
        {
            T[rt].lazy += v;
            T[rt].Min += v;
            return ;
        }
        int mid = (L + R) >> 1;
        if(T[rt].lazy) PushDown(L, R, rt);
        if(r <= mid) Update(l, r, v, Lson);
        else if(l > mid) Update(l, r, v, Rson);
        else
        {
            Update(l, mid, v, Lson);
            Update(mid+1, r, v, Rson);
        }
        PushUp(rt);
    }
    ll Query(int l, int r, int L, int R, int rt)
    {
        if(l==L && r== R)
        {    return T[rt].Min;
        }
        int mid = (L + R) >> 1;
        if(T[rt].lazy) PushDown(L, R, rt);
        if(r <= mid) return Query(l, r, Lson);
        else if(l > mid) return Query(l, r, Rson);
        return min(Query(l, mid, Lson), Query(mid + 1, r, Rson));
    }
    struct node
    {
        int x,y;
    }a[maxn];
    int main()
    {
        int i,j;
        scanf("%d",&n);
        rep(i,1,n)
        {
            int b,p,d,e;
            scanf("%d",&a[i].x);
            scanf("%d.%d %d:%d",&b,&p,&d,&e);
            a[i].y=(b+p*31)*24*60+d*60+e;
            c[i]=a[i].y;
        }
        sort(c+1,c+n+1);
        rep(i,1,n)a[i].y=lower_bound(c+1,c+n+1,a[i].y)-c;
        rep(i,1,n)
        {
            Update(a[i].y,n,a[i].x,1,n,1);
            printf("%lld
    ",min(0LL,Query(1,n,1,n,1)));
        }
        //system("pause");
        return 0;
    }
  • 相关阅读:
    《Java编程思想第四版》附录 B 对比 C++和 Java
    《Java编程思想第四版》附录 C Java 编程规则
    《尚学堂_史上最易懂的设计模式视频》--章节1 责任链模式-- 过滤器模式
    《Effective Java中文版(第2版).pdf》-笔记
    javascript面试--网络收集
    眼睛问题
    网易云课堂《JS原创视频教程-知识点类》
    定时刷新页面SetInterval 和setTimeout -时间间隔可以动态设定
    MSSQL无法启动-原来电脑登录密码改了,重启后要设置
    Thymeleaf--:fragment
  • 原文地址:https://www.cnblogs.com/dyzll/p/5823605.html
Copyright © 2011-2022 走看看