답안 #145725

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
145725 2019-08-20T22:42:56 Z osaaateiasavtnl Candies (JOI18_candies) C++14
8 / 100
5000 ms 4392 KB
#include<bits/stdc++.h>
using namespace std;
#define int long long 
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcount
const int N = 2e5 + 7, INF = 1e18 + 7;
int a[N];
bool used[N];
signed main() {
    #ifdef HOME
    freopen("input.txt", "r", stdin);
    #else
    ios_base::sync_with_stdio(0); cin.tie(0);
    #endif
    int n; cin >> n; for (int i = 1; i <= n; ++i) cin >> a[i];
    int ans = 0;
    for (int _ = 1; _ <= (n + 1) / 2; ++_) {
        int mx = -INF;
        for (int i = 1; i <= n; ++i) {
            if (!used[i] && !used[i - 1] && !used[i + 1]) mx = max(mx, a[i]);
        }
        vector <int> p;
        for (int i = 1; i <= n; ++i) {
            if (used[i]) p.app(i);
        }
        int l = 0;
        while (l < p.size()) {
            int r = l;
            while (r + 1 < p.size() && p[r + 1] == p[r] + 2) ++r;
            if (1 < p[l] && p[r] < n) {
                int sum = a[p[l] - 1] + a[p[r] + 1];
                for (int i = p[l] + 1; i < p[r]; i += 2) sum += a[i];
                for (int i = p[l]; i <= p[r]; i += 2) sum -= a[i];
                mx = max(mx, sum);
            }
            l = r + 1;
        }

        ans += mx;

        bool mem = 0;
        for (int i = 1; i <= n; ++i) {
            if (!used[i] && !used[i - 1] && !used[i + 1] && a[i] == mx) {
                used[i] = 1; mem = 1; break;
            }
        }
        if (!mem) {
            l = 0;
            while (l < p.size()) {
                int r = l;
                while (r + 1 < p.size() && p[r + 1] == p[r] + 2) ++r;
                if (1 < p[l] && p[r] < n) {
                    int sum = a[p[l] - 1] + a[p[r] + 1];
                    for (int i = p[l] + 1; i < p[r]; i += 2) sum += a[i];
                    for (int i = p[l]; i <= p[r]; i += 2) sum -= a[i];
                    if (sum == mx) {
                        used[p[l] - 1] = used[p[r] + 1] = 1;
                        for (int i = p[l] + 1; i < p[r]; i += 2) used[i] = 1;
                        for (int i = p[l]; i <= p[r]; i += 2) used[i] = 0;
                        break;
                    }
                }
                l = r + 1;
            }       
        }

        cout << ans << '\n';
    }
}

Compilation message

candies.cpp: In function 'int main()':
candies.cpp:29:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         while (l < p.size()) {
                ~~^~~~~~~~~~
candies.cpp:31:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             while (r + 1 < p.size() && p[r + 1] == p[r] + 2) ++r;
                    ~~~~~~^~~~~~~~~~
candies.cpp:51:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             while (l < p.size()) {
                    ~~^~~~~~~~~~
candies.cpp:53:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                 while (r + 1 < p.size() && p[r + 1] == p[r] + 2) ++r;
                        ~~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 376 KB Output is correct
2 Correct 23 ms 376 KB Output is correct
3 Correct 22 ms 504 KB Output is correct
4 Correct 21 ms 376 KB Output is correct
5 Correct 21 ms 376 KB Output is correct
6 Correct 21 ms 376 KB Output is correct
7 Correct 22 ms 376 KB Output is correct
8 Correct 21 ms 376 KB Output is correct
9 Correct 17 ms 376 KB Output is correct
10 Correct 19 ms 376 KB Output is correct
11 Correct 23 ms 504 KB Output is correct
12 Correct 23 ms 464 KB Output is correct
13 Correct 21 ms 376 KB Output is correct
14 Correct 21 ms 504 KB Output is correct
15 Correct 21 ms 504 KB Output is correct
16 Correct 22 ms 376 KB Output is correct
17 Correct 22 ms 504 KB Output is correct
18 Correct 22 ms 376 KB Output is correct
19 Correct 17 ms 372 KB Output is correct
20 Correct 18 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 376 KB Output is correct
2 Correct 23 ms 376 KB Output is correct
3 Correct 22 ms 504 KB Output is correct
4 Correct 21 ms 376 KB Output is correct
5 Correct 21 ms 376 KB Output is correct
6 Correct 21 ms 376 KB Output is correct
7 Correct 22 ms 376 KB Output is correct
8 Correct 21 ms 376 KB Output is correct
9 Correct 17 ms 376 KB Output is correct
10 Correct 19 ms 376 KB Output is correct
11 Correct 23 ms 504 KB Output is correct
12 Correct 23 ms 464 KB Output is correct
13 Correct 21 ms 376 KB Output is correct
14 Correct 21 ms 504 KB Output is correct
15 Correct 21 ms 504 KB Output is correct
16 Correct 22 ms 376 KB Output is correct
17 Correct 22 ms 504 KB Output is correct
18 Correct 22 ms 376 KB Output is correct
19 Correct 17 ms 372 KB Output is correct
20 Correct 18 ms 376 KB Output is correct
21 Execution timed out 5100 ms 4392 KB Time limit exceeded
22 Halted 0 ms 0 KB -