• 二分 + 差分


    Alice likes snow a lot! Unfortunately, this year's winter is already over, and she can't expect to have any more of it. Bob has thus bought her a gift — a large snow maker. He plans to make some amount of snow every day. On day i he will make a pile of snow of volume Vi and put it in her garden.

    Each day, every pile will shrink a little due to melting. More precisely, when the temperature on a given day is Ti, each pile will reduce its volume by Ti. If this would reduce the volume of a pile to or below zero, it disappears forever. All snow piles are independent of each other.

    Note that the pile made on day i already loses part of its volume on the same day. In an extreme case, this may mean that there are no piles left at the end of a particular day.

    You are given the initial pile sizes and the temperature on each day. Determine the total volume of snow melted on each day.

    Input

    The first line contains a single integer N (1 ≤ N ≤ 105) — the number of days.

    The second line contains N integers V1, V2, ..., VN (0 ≤ Vi ≤ 109), where Vi is the initial size of a snow pile made on the day i.

    The third line contains N integers T1, T2, ..., TN (0 ≤ Ti ≤ 109), where Ti is the temperature on the day i.

    Output

    Output a single line with N integers, where the i-th integer represents the total volume of snow melted on day i.

    Example
    Input
    3
    10 10 5
    5 7 2
    Output
    5 12 4
    Input
    5
    30 25 20 15 10
    9 10 12 4 13
    Output
    9 20 35 11 25
    Note

    In the first sample, Bob first makes a snow pile of volume 10, which melts to the size of 5 on the same day. On the second day, he makes another pile of size 10. Since it is a bit warmer than the day before, the first pile disappears completely while the second pile shrinks to 3. At the end of the second day, he has only a single pile of size 3. On the third day he makes a smaller pile than usual, but as the temperature dropped too, both piles survive till the end of the day.

    题意 : 输入的一个 n ,表示天数,然后输入的第一行表示在这天产生的雪的量,然后再一行表示雪在这天的融化量是多少,对于当前的一天,他不仅会融化当天的,还会融化前面所有天的雪,最后输出每天融化雪的量。

    思路分析 :最开始的想法是纯用线段树去搞它,写了一会交上去WA了... 后来问了下大佬的思路,才知道原来这题是去二分雪的量,去判断雪在哪天融化 。然后差分一遍就没问题了。

    代码示例 :

    #define ll long long
    const ll maxn = 1e5+5;
    const double pi = acos(-1.0);
    const ll inf = 0x3f3f3f3f;
    
    ll v[maxn], te[maxn];
    ll sum[maxn];
    ll ans[maxn], pp[maxn], f[maxn];
    ll start;
    
    bool check(ll p, ll vi){
        ll ss = sum[p] - sum[start-1];
        if (ss >= vi) return true;
        else return false;
    }
    
    void fun(ll l, ll r, ll vi){
        ll mid, pos;
        while(l <= r){
            mid = (l+r) >> 1;
            if (check(mid, vi)) {pos = mid; r = mid-1;} 
            else l = mid+1;
        }
        //printf("mid = %lld
    ", pos);
        ans[pos]--;
        ans[start]++;
        pp[pos] += vi-(sum[pos-1]-sum[start-1]);
    }
    
    int main() {
        //freopen("in.txt", "r", stdin);
        //freopen("out.txt", "w", stdout);
        ll n;
        cin >> n;
        for(ll i = 1; i <= n; i++) scanf("%d", &v[i]);
        for(ll i = 1; i <= n; i++) {
            scanf("%lld", &te[i]);
            sum[i] = sum[i-1] + te[i];
        }
        sum[n+1] = sum[n] + 10000000000;
        for(ll i = 1; i <= n; i++){
            start = i;
            fun(i, n+1, v[i]); 
        }
        for(ll i = 1; i <= n; i++) {
            
            ans[i] += ans[i-1];
            f[i] = ans[i]*te[i] + pp[i];
        }
        for(ll i = 1; i <= n; i++) printf("%lld%c", f[i], i==n?'
    ':' ');
        return 0;
    }
    

    方法二 : 优先队列 + 前缀和

    看了大佬们的代码,发现优先队列也可以,每次将当天前面所有的温度和累加再加上当前天的雪的总量,这样打入优先队列的雪量就可以视为是从同一天打入的

    代码示例 :

    #define ll long long
    const ll maxn = 1e5+5;
    const double pi = acos(-1.0);
    const ll inf = 0x3f3f3f3f;
    
    ll v[maxn], te[maxn];
    ll sum[maxn];
    priority_queue<ll, vector<ll>, greater<ll> >que;
    
    int main() {
        //freopen("in.txt", "r", stdin);
        //freopen("out.txt", "w", stdout);
        ll n;
        cin >> n;
        for(ll i = 1; i <= n; i++) scanf("%d", &v[i]);
        for(ll i = 1; i <= n; i++) {
            scanf("%lld", &te[i]);
            sum[i] = sum[i-1] + te[i];
        }
        ll pt = 0;
        for(ll i = 1; i <= n; i++){
            que.push(sum[i-1]+v[i]);
            pt += te[i];
            ll ans = 0;
            
            while(!que.empty()){
                ll x = que.top();
                if (x > pt) break;
                que.pop();
                ans += x-(pt-te[i]);    
            }
            ans += que.size()*te[i];
            printf("%lld%c", ans, i==n?'
    ':' ');
        }    
        return 0;
    }
    
    东北日出西边雨 道是无情却有情
  • 相关阅读:
    Linux DNS配置
    SqlCommandBuilder 为SqlDataAdapter 提供 update、insert、delete 命令(自己利用select命令自动从数据库元数据推断)
    Dev中GridView——事件
    select 动态赋值 layui重新渲染
    url-loader和file-loader的区别和使用
    Fetch API
    Fetch API
    FormData上传文件
    Buffer、ArrayBuffer互转
    if exists用法(判断对象的用法)
  • 原文地址:https://www.cnblogs.com/ccut-ry/p/8547530.html
Copyright © 2020-2023  润新知