답안 #772264

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
772264 2023-07-03T20:43:15 Z danikoynov Potatoes and fertilizers (LMIO19_bulves) C++14
100 / 100
264 ms 39568 KB
#include <iostream>
#include <set> 
#include <cassert>
#include <numeric>
#include <bits/stdc++.h>
#define endl '\n'
#define int long long
typedef long long ll;
using namespace std;
struct slope_trick
{
    const ll inf = 9223372036854775807ll;
 
    std::multiset < ll > left_set, right_set;
    ll height;
 
    slope_trick()
    {
        height = 0;
    }
 
 
    ll get_right()
    {
        if (right_set.empty())
            return inf;
        return *right_set.begin();
    }
 
    ll get_left()
    {
        if (left_set.empty())
            return -inf;
        return *left_set.rbegin();
    }
 
    void add_basic(ll v) /// adds |x - v|
    {
        ll left = get_left(), right = get_right();
 
        if (v >= left && v <= right)
        {
            left_set.insert(v);
            right_set.insert(v);
        }
        else
        if (v < left)
        {
            left_set.insert(v);
            left_set.insert(v);
 
            right_set.insert(left);
            left_set.erase(left_set.find(left));
 
            height = height + abs(v - left);
        }
    }
 
 
    void print_slope_trick()
    {
        std::cout << "left_slope" << endl;
        for (int cor : left_set)
            std::cout << cor << " ";
            std::cout << endl;
            std::cout << "right_slope" << endl;
        for (int cor : right_set)
            std::cout << cor << " ";
        std::cout << endl;
        std::cout << "height " << height << endl;
    }
 
 
    void prefix_minimum()
    {
        
 
        right_set.clear();
        ///right_set.clear();
    }
 
    ll query_vlaue(ll debit)
    {   
        ll last = get_left(), slope = 0, base = height;
        while(left_set.size() > 0 && get_left() >= debit)
        {
            ll distance = last - get_left();
            base = base + distance * slope;
            slope ++;
            last = last - distance;
            assert(left_set.size() > 0);
            left_set.erase(prev(left_set.end()));
        }
 
        if (slope != 0)
        {
            assert(last >= debit);
            base = base + (last - debit) * slope;
        }
 
        return base;
    }
};
 
 
const int maxn = 500010;
 
int n;
ll a[maxn], b[maxn];
ll x[maxn], pref[maxn];
 
void solve()
{
    std::cin >> n;
    for (int i = 1; i <= n; i ++)
    {
        std::cin >> a[i] >> b[i];
        x[i] = a[i] - b[i];
        pref[i] = pref[i - 1] + x[i];
    }
 
    slope_trick st;
    
    for (int i = 1; i <= n; i ++)
    {
        if (pref[i] < 0)
        {
            st.height += -pref[i];
            st.add_basic(0);
        }
        else
            st.add_basic(pref[i]);
        st.prefix_minimum();
        //std::cout << "--------------------" << endl;
        // st.print_slope_trick();
    }
    ///std::cout << "dasdas" << endl;
    ll ans = st.query_vlaue(pref[n]);
    ///std::cout << "-----------------" << endl;
    std::cout << ans << endl;
}
 
void speed()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    std::cout.tie(NULL);
}
 
signed main()
{
    speed();
    solve();
    return 0;
}

Compilation message

bulves.cpp: In member function 'void slope_trick::print_slope_trick()':
bulves.cpp:63:9: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   63 |         for (int cor : left_set)
      |         ^~~
bulves.cpp:65:13: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   65 |             std::cout << endl;
      |             ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 18 ms 4252 KB Output is correct
5 Correct 41 ms 8168 KB Output is correct
6 Correct 107 ms 19792 KB Output is correct
7 Correct 189 ms 39376 KB Output is correct
8 Correct 191 ms 39364 KB Output is correct
9 Correct 198 ms 39444 KB Output is correct
10 Correct 223 ms 39568 KB Output is correct
11 Correct 192 ms 39424 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 18 ms 4252 KB Output is correct
5 Correct 41 ms 8168 KB Output is correct
6 Correct 107 ms 19792 KB Output is correct
7 Correct 189 ms 39376 KB Output is correct
8 Correct 191 ms 39364 KB Output is correct
9 Correct 198 ms 39444 KB Output is correct
10 Correct 223 ms 39568 KB Output is correct
11 Correct 192 ms 39424 KB Output is correct
12 Correct 54 ms 10096 KB Output is correct
13 Correct 136 ms 23820 KB Output is correct
14 Correct 186 ms 39440 KB Output is correct
15 Correct 191 ms 39372 KB Output is correct
16 Correct 249 ms 39376 KB Output is correct
17 Correct 158 ms 39416 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 18 ms 4252 KB Output is correct
12 Correct 41 ms 8168 KB Output is correct
13 Correct 107 ms 19792 KB Output is correct
14 Correct 189 ms 39376 KB Output is correct
15 Correct 191 ms 39364 KB Output is correct
16 Correct 198 ms 39444 KB Output is correct
17 Correct 223 ms 39568 KB Output is correct
18 Correct 192 ms 39424 KB Output is correct
19 Correct 54 ms 10096 KB Output is correct
20 Correct 136 ms 23820 KB Output is correct
21 Correct 186 ms 39440 KB Output is correct
22 Correct 191 ms 39372 KB Output is correct
23 Correct 249 ms 39376 KB Output is correct
24 Correct 158 ms 39416 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 1 ms 468 KB Output is correct
32 Correct 1 ms 468 KB Output is correct
33 Correct 55 ms 10200 KB Output is correct
34 Correct 127 ms 23820 KB Output is correct
35 Correct 226 ms 39448 KB Output is correct
36 Correct 264 ms 39440 KB Output is correct
37 Correct 177 ms 39404 KB Output is correct
38 Correct 221 ms 39384 KB Output is correct
39 Correct 194 ms 39412 KB Output is correct
40 Correct 154 ms 35744 KB Output is correct
41 Correct 194 ms 39328 KB Output is correct
42 Correct 199 ms 39380 KB Output is correct
43 Correct 193 ms 39428 KB Output is correct
44 Correct 182 ms 39408 KB Output is correct
45 Correct 244 ms 39356 KB Output is correct
46 Correct 144 ms 39332 KB Output is correct
47 Correct 146 ms 30876 KB Output is correct