답안 #1037641

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1037641 2024-07-29T06:22:42 Z 김은성(#10980) Cookies (JOI23_cookies) C++17
7 / 100
8 ms 16988 KB
#include <bits/stdc++.h>
using namespace std;
const int INF = 1234123412;
int a[15009], dp[2][15009];
int cnt[15009];
short opt[15009][15009];
bool pos[15009];
int main(){
    int n, m, i, j, v, N = 0;
    scanf("%d", &n);
    for(i=1; i<=n; i++){
        scanf("%d", &a[i]);
        cnt[i] = a[i];
        N += a[i];
    }
    sort(cnt+1, cnt+n+1, [](int &u, int &v){return u > v;});
    for(i=n; i>=1; i--)
        cnt[i] += cnt[i+1];
    scanf("%d", &m);
    for(i=1; i<=m; i++){
        scanf("%d", &v);
        pos[v] = 1;
    }
    for(j=0; j<=N; j++)
        dp[n%2][j] = -INF;
    dp[n%2][0] = 0;
    opt[n][0] = 0;
    for(i=n; i>=1; i--){
        if(pos[i]){
            for(j=0; j<cnt[i]; j++){
                if(dp[i%2][j+1] < dp[i%2][j] + 1){
                    dp[i%2][j+1] = dp[i%2][j] + 1;
                    opt[i][j+1] = -1;
                }
            }
        }
        for(j=0; j<=N; j++)
            dp[(i-1)%2][j] = -INF;
        for(j=0; j<=cnt[i]; j++){
            if(j + dp[i%2][j] >= 0 && j+dp[i%2][j] <= cnt[i-1] && dp[(i-1)%2][j+dp[i%2][j]] < dp[i%2][j]){
                dp[(i-1)%2][j+dp[i%2][j]] = dp[i%2][j];
                opt[i-1][j+dp[i%2][j]] = j;
            }
           // printf("dp[%d][%d]=%d\n", i, j, dp[i%2][j]);
        }
    }
    int row = 1, col = N;
    if(dp[1][N] <= -INF){
        printf("-1\n");
        return 0;
    }
    vector<int> vec;
    while(row <= n && col > 0){
        if(opt[row][col] == -1){
            vec.push_back(row);
            col--;
        }
        else{
            col = opt[row][col];
            row++;
        }
    }
    for(i=0; i<vec.size()/2; i++)
        swap(vec[i], vec[(int)vec.size()-1-i]);
    priority_queue<pair<int, int> > qa;
    for(i=1; i<=n; i++)
        qa.push(make_pair(a[i], i));
    printf("%d\n", vec.size());
    for(i=0; i<vec.size(); i++){
        printf("%d ", vec[i]);
        vector<pair<int, int> > temp;
        for(int j=0; j<vec[i]; j++){
            temp.push_back(qa.top());
            temp.back().first--;
            printf("%d ", temp.back().second);
            qa.pop();
        }
        printf("\n");
        while(!temp.empty()){
            qa.push(temp.back());
            temp.pop_back();
        }
    }
    return 0;
}

Compilation message

cookies.cpp: In function 'int main()':
cookies.cpp:63:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   63 |     for(i=0; i<vec.size()/2; i++)
      |              ~^~~~~~~~~~~~~
cookies.cpp:68:14: warning: format '%d' expects argument of type 'int', but argument 2 has type 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wformat=]
   68 |     printf("%d\n", vec.size());
      |             ~^     ~~~~~~~~~~
      |              |             |
      |              int           std::vector<int>::size_type {aka long unsigned int}
      |             %ld
cookies.cpp:69:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   69 |     for(i=0; i<vec.size(); i++){
      |              ~^~~~~~~~~~~
cookies.cpp:10:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   10 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
cookies.cpp:12:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   12 |         scanf("%d", &a[i]);
      |         ~~~~~^~~~~~~~~~~~~
cookies.cpp:19:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   19 |     scanf("%d", &m);
      |     ~~~~~^~~~~~~~~~
cookies.cpp:21:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   21 |         scanf("%d", &v);
      |         ~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 2396 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 396 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 2 ms 16732 KB Output is correct
6 Correct 2 ms 16732 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 2 ms 604 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 0 ms 2396 KB Output is correct
18 Correct 2 ms 2652 KB Output is correct
19 Correct 8 ms 16988 KB Output is correct
20 Correct 8 ms 16984 KB Output is correct
21 Correct 7 ms 16988 KB Output is correct
22 Correct 3 ms 4700 KB Output is correct
23 Correct 2 ms 16728 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 2396 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 2396 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 2396 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 2396 KB Output isn't correct
2 Halted 0 ms 0 KB -