답안 #1084498

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1084498 2024-09-06T10:34:47 Z _rain_ Discharging (NOI20_discharging) C++14
100 / 100
84 ms 25796 KB
#include<bits/stdc++.h>
using namespace std;
#define ll long long

#define fixbug false

const ll INF = (ll)4e18+7;
const int maxn = 1e6;
ll a[maxn+2] ;
int n;

namespace subtask1{
    bool check(){
        return n <= 1500;
    }
    const int N = 1500;
    ll f[N+2] , sum[N+2] = {};
    void main_code(){
        for (int i = 1; i <= n; ++i) f[i] = INF , sum[i] = INF;
        for (int i = 1; i <= n; ++i){
            ll mx = a[i];
            for (int j = i; j >= 1; --j){
                mx = max(mx , a[j]);
                f[i] = min(f[i] , f[j - 1] + mx * (n - j + 1));
            }
        }
        cout << f[n];
        return;
    }
}

namespace subtask2{
    bool check(){
        return true;
    }
    const int N = 1e6;
    ll f[N+2];
    #define double long double
    struct Line{
        ll a , b;
        Line(ll a , ll b) : a(a) , b(b) {};
        ll F(ll x){
            return a * x + b;
        }
    };
    struct Hull{
        Line X;
        double slope;
    };
    double get_slope(Line a , Line b){
        return (double)(b.b - a.b) / (a.a - b.a);
    }
    vector<Hull> convex;
    void addline(Line x){
        int n = convex.size();
        while (n > 1 && get_slope(convex[n - 2].X , x) <= convex[n-1].slope){
            convex.pop_back();
            --n;
        }
        if (!convex.size()) convex.push_back({x , LLONG_MIN});
            else convex.push_back({x , get_slope(convex[n - 1].X,x)});
        return;
    }
    ll Find(ll x , int t){
        int l = 0 , r = convex.size() - 1 , p = 0;
        while (l<=r){
            int m = l + r >> 1;
            if (convex[m].slope <= x){
                l = m + 1;
                p = m;
            }
            else r = m - 1;
        }
        if (t&&fixbug){
            cout << convex[p].X.a << ' ' << convex[p].X.b << ' ' << p << '\n';
            for (auto& x : convex) cout << x.X.a << ' ' << x.X.b << ' ' << x.slope << '\n';
        }
        return convex[p].X.F(x);
    }
    void add(ll a , ll b){
        addline(Line(a,b));
        return;
    }


    void main_code(){
        for (int i = 1; i <= n; ++i){
            add(-i , f[i-1]);
            f[i] = Find(a[i],i==2) + a[i]*n + a[i];
            int j = i;
            while (j <= n && a[i] >= a[j]){
                ++j;
                f[j] = f[i];
            }
            i = j - 1;
            if (fixbug){
                cout << i << ' ' << f[i] << '\n';
            }
        }
        cout << f[n];
    }

}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    #define name "main"
    if (fopen(name".inp","r")){
        freopen(name".inp","r",stdin);
        freopen(name".out","w",stdout);
    }

    cin >> n;
    for (int i = 1; i <= n; ++i) cin >> a[i];

    if (subtask1::check()) {
        subtask1::main_code();
        exit(0);
    }
    subtask2::main_code();
    exit(0);
}

Compilation message

Discharging.cpp: In function 'long long int subtask2::Find(long long int, int)':
Discharging.cpp:67:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   67 |             int m = l + r >> 1;
      |                     ~~^~~
Discharging.cpp: In function 'int main()':
Discharging.cpp:110:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  110 |         freopen(name".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
Discharging.cpp:111:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  111 |         freopen(name".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 0 ms 2396 KB Output is correct
7 Correct 1 ms 2392 KB Output is correct
8 Correct 0 ms 2396 KB Output is correct
9 Correct 1 ms 2648 KB Output is correct
10 Correct 0 ms 2396 KB Output is correct
11 Correct 0 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 0 ms 2396 KB Output is correct
14 Correct 0 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2396 KB Output is correct
2 Correct 2 ms 2396 KB Output is correct
3 Correct 2 ms 2396 KB Output is correct
4 Correct 2 ms 2396 KB Output is correct
5 Correct 2 ms 2396 KB Output is correct
6 Correct 2 ms 2396 KB Output is correct
7 Correct 2 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 2 ms 2396 KB Output is correct
10 Correct 2 ms 2396 KB Output is correct
11 Correct 2 ms 2396 KB Output is correct
12 Correct 2 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 3 ms 2396 KB Output is correct
15 Correct 2 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2396 KB Output is correct
2 Correct 2 ms 2396 KB Output is correct
3 Correct 2 ms 2396 KB Output is correct
4 Correct 2 ms 2396 KB Output is correct
5 Correct 2 ms 2396 KB Output is correct
6 Correct 2 ms 2396 KB Output is correct
7 Correct 2 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 2 ms 2396 KB Output is correct
10 Correct 2 ms 2396 KB Output is correct
11 Correct 2 ms 2396 KB Output is correct
12 Correct 2 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 3 ms 2396 KB Output is correct
15 Correct 2 ms 2396 KB Output is correct
16 Correct 66 ms 20564 KB Output is correct
17 Correct 69 ms 21844 KB Output is correct
18 Correct 55 ms 20308 KB Output is correct
19 Correct 68 ms 21840 KB Output is correct
20 Correct 73 ms 21708 KB Output is correct
21 Correct 68 ms 21844 KB Output is correct
22 Correct 67 ms 21496 KB Output is correct
23 Correct 84 ms 22352 KB Output is correct
24 Correct 72 ms 21844 KB Output is correct
25 Correct 74 ms 22352 KB Output is correct
26 Correct 79 ms 22608 KB Output is correct
27 Correct 68 ms 21076 KB Output is correct
28 Correct 72 ms 21820 KB Output is correct
29 Correct 76 ms 22100 KB Output is correct
30 Correct 81 ms 22356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 64 ms 24404 KB Output is correct
2 Correct 75 ms 25668 KB Output is correct
3 Correct 66 ms 25680 KB Output is correct
4 Correct 68 ms 25684 KB Output is correct
5 Correct 64 ms 25792 KB Output is correct
6 Correct 65 ms 25684 KB Output is correct
7 Correct 68 ms 25680 KB Output is correct
8 Correct 69 ms 25668 KB Output is correct
9 Correct 68 ms 25680 KB Output is correct
10 Correct 68 ms 25796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 0 ms 2396 KB Output is correct
7 Correct 1 ms 2392 KB Output is correct
8 Correct 0 ms 2396 KB Output is correct
9 Correct 1 ms 2648 KB Output is correct
10 Correct 0 ms 2396 KB Output is correct
11 Correct 0 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 0 ms 2396 KB Output is correct
14 Correct 0 ms 2396 KB Output is correct
15 Correct 2 ms 2396 KB Output is correct
16 Correct 2 ms 2396 KB Output is correct
17 Correct 2 ms 2396 KB Output is correct
18 Correct 2 ms 2396 KB Output is correct
19 Correct 2 ms 2396 KB Output is correct
20 Correct 2 ms 2396 KB Output is correct
21 Correct 2 ms 2396 KB Output is correct
22 Correct 1 ms 2396 KB Output is correct
23 Correct 2 ms 2396 KB Output is correct
24 Correct 2 ms 2396 KB Output is correct
25 Correct 2 ms 2396 KB Output is correct
26 Correct 2 ms 2396 KB Output is correct
27 Correct 1 ms 2396 KB Output is correct
28 Correct 3 ms 2396 KB Output is correct
29 Correct 2 ms 2396 KB Output is correct
30 Correct 2 ms 2396 KB Output is correct
31 Correct 1 ms 2396 KB Output is correct
32 Correct 1 ms 2396 KB Output is correct
33 Correct 2 ms 2392 KB Output is correct
34 Correct 2 ms 2396 KB Output is correct
35 Correct 2 ms 2396 KB Output is correct
36 Correct 3 ms 2396 KB Output is correct
37 Correct 2 ms 2396 KB Output is correct
38 Correct 2 ms 2396 KB Output is correct
39 Correct 2 ms 2392 KB Output is correct
40 Correct 2 ms 2396 KB Output is correct
41 Correct 1 ms 2396 KB Output is correct
42 Correct 2 ms 2396 KB Output is correct
43 Correct 2 ms 2520 KB Output is correct
44 Correct 2 ms 2396 KB Output is correct
45 Correct 1 ms 2396 KB Output is correct
46 Correct 1 ms 2396 KB Output is correct
47 Correct 2 ms 2396 KB Output is correct
48 Correct 1 ms 2396 KB Output is correct
49 Correct 2 ms 2396 KB Output is correct
50 Correct 2 ms 2392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 0 ms 2396 KB Output is correct
7 Correct 1 ms 2392 KB Output is correct
8 Correct 0 ms 2396 KB Output is correct
9 Correct 1 ms 2648 KB Output is correct
10 Correct 0 ms 2396 KB Output is correct
11 Correct 0 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 0 ms 2396 KB Output is correct
14 Correct 0 ms 2396 KB Output is correct
15 Correct 2 ms 2396 KB Output is correct
16 Correct 2 ms 2396 KB Output is correct
17 Correct 2 ms 2396 KB Output is correct
18 Correct 2 ms 2396 KB Output is correct
19 Correct 2 ms 2396 KB Output is correct
20 Correct 2 ms 2396 KB Output is correct
21 Correct 2 ms 2396 KB Output is correct
22 Correct 1 ms 2396 KB Output is correct
23 Correct 2 ms 2396 KB Output is correct
24 Correct 2 ms 2396 KB Output is correct
25 Correct 2 ms 2396 KB Output is correct
26 Correct 2 ms 2396 KB Output is correct
27 Correct 1 ms 2396 KB Output is correct
28 Correct 3 ms 2396 KB Output is correct
29 Correct 2 ms 2396 KB Output is correct
30 Correct 66 ms 20564 KB Output is correct
31 Correct 69 ms 21844 KB Output is correct
32 Correct 55 ms 20308 KB Output is correct
33 Correct 68 ms 21840 KB Output is correct
34 Correct 73 ms 21708 KB Output is correct
35 Correct 68 ms 21844 KB Output is correct
36 Correct 67 ms 21496 KB Output is correct
37 Correct 84 ms 22352 KB Output is correct
38 Correct 72 ms 21844 KB Output is correct
39 Correct 74 ms 22352 KB Output is correct
40 Correct 79 ms 22608 KB Output is correct
41 Correct 68 ms 21076 KB Output is correct
42 Correct 72 ms 21820 KB Output is correct
43 Correct 76 ms 22100 KB Output is correct
44 Correct 81 ms 22356 KB Output is correct
45 Correct 64 ms 24404 KB Output is correct
46 Correct 75 ms 25668 KB Output is correct
47 Correct 66 ms 25680 KB Output is correct
48 Correct 68 ms 25684 KB Output is correct
49 Correct 64 ms 25792 KB Output is correct
50 Correct 65 ms 25684 KB Output is correct
51 Correct 68 ms 25680 KB Output is correct
52 Correct 69 ms 25668 KB Output is correct
53 Correct 68 ms 25680 KB Output is correct
54 Correct 68 ms 25796 KB Output is correct
55 Correct 2 ms 2396 KB Output is correct
56 Correct 1 ms 2396 KB Output is correct
57 Correct 1 ms 2396 KB Output is correct
58 Correct 2 ms 2392 KB Output is correct
59 Correct 2 ms 2396 KB Output is correct
60 Correct 2 ms 2396 KB Output is correct
61 Correct 3 ms 2396 KB Output is correct
62 Correct 2 ms 2396 KB Output is correct
63 Correct 2 ms 2396 KB Output is correct
64 Correct 2 ms 2392 KB Output is correct
65 Correct 2 ms 2396 KB Output is correct
66 Correct 1 ms 2396 KB Output is correct
67 Correct 2 ms 2396 KB Output is correct
68 Correct 2 ms 2520 KB Output is correct
69 Correct 2 ms 2396 KB Output is correct
70 Correct 1 ms 2396 KB Output is correct
71 Correct 1 ms 2396 KB Output is correct
72 Correct 2 ms 2396 KB Output is correct
73 Correct 1 ms 2396 KB Output is correct
74 Correct 2 ms 2396 KB Output is correct
75 Correct 2 ms 2392 KB Output is correct
76 Correct 53 ms 21952 KB Output is correct
77 Correct 56 ms 21956 KB Output is correct
78 Correct 53 ms 21452 KB Output is correct
79 Correct 55 ms 21584 KB Output is correct
80 Correct 53 ms 22356 KB Output is correct
81 Correct 49 ms 21584 KB Output is correct
82 Correct 52 ms 21584 KB Output is correct
83 Correct 52 ms 21956 KB Output is correct
84 Correct 52 ms 21844 KB Output is correct
85 Correct 53 ms 22096 KB Output is correct
86 Correct 49 ms 21584 KB Output is correct
87 Correct 49 ms 21332 KB Output is correct
88 Correct 50 ms 21840 KB Output is correct
89 Correct 52 ms 21564 KB Output is correct
90 Correct 49 ms 21584 KB Output is correct
91 Correct 50 ms 21344 KB Output is correct
92 Correct 73 ms 22608 KB Output is correct
93 Correct 66 ms 21904 KB Output is correct
94 Correct 49 ms 21056 KB Output is correct
95 Correct 54 ms 22256 KB Output is correct
96 Correct 52 ms 21332 KB Output is correct
97 Correct 50 ms 21584 KB Output is correct
98 Correct 54 ms 21844 KB Output is correct
99 Correct 50 ms 21328 KB Output is correct
100 Correct 58 ms 21572 KB Output is correct