제출 #1340765

#제출 시각아이디문제언어결과실행 시간메모리
1340765domiA Difficult(y) Choice (BOI21_books)C++20
컴파일 에러
0 ms0 KiB
#include <bits/stdc++.h>
#include "books.h"

// #define int long long
// #define fi first
// #define se second
//
// #define sz(a) (int)((a).size())
// #define all(a) (a).begin(), (a).end()
//
// #define lsb(x) (x & (-x))
// #define vi vector<int>
// #define YES { cout << "YES" << endl; return; }
// #define NO { cout << "NO" << endl; return; }

// using ll = long long;
// using pii = std::pair<int, int>;

const int NMAX = 1e5;

using namespace std;

int64_t mins[NMAX + 5];

void solve(int N, int K, long long A, int S) {
    int64_t mins[K];
    for(int i = 0; i < K; i++)
        mins[i] = skim(i + 1);

    int64_t sum = 0;
    for(int i = 0; i < K; i++)
        sum += mins[i];

    if(sum > 2 * A) {
        impossible();
        return;
    }

    if(sum >= A) {
        vector<int> res;
        for(int i = 0; i < K; i++)
            res.push_back(i + 1);
        answer(res);
        return;
    }

    int lo = K, hi = N;
    while(lo < hi){
        int mid = (lo + hi) / 2;
        int64_t x = skim(mid + 1);
        if(x > A)
            hi = mid;
        else
            lo = mid + 1;
    }

    if(lo < N){
        int64_t big = skim(lo + 1);
        sum = 0;
        vector<int> res;
        for(int i = 0; i < K - 1; i++) {
            sum += mins[i];
            res.push_back(i + 1);
        }

        sum += big;
        res.push_back(lo + 1);
        if(sum <= 2 * A){
            answer(res);
            return;
        }
    }

    vector<pair<int, int64_t> > arr;
    for(int i = 0; i < K; i++)
        arr.push_back(make_pair(i, mins[i]));

    for(int i = max(lo - K, K); i < lo; i++)
        arr.push_back(make_pair(i, skim(i + 1)));

    int n = arr.size();
    sum = 0;
    for(int i = n - K; i < n; i++)
        sum += arr[i].second;

    if(sum < A){
        impossible();
        return;
    }

    vector<int> res;
    sum = 0;
    for(int i = 0; i < K; i++){
        sum += arr[i].second;
        res.push_back(i);
    }

    for(int i = K - 1; i >= 0; i--){
        int nxt = (i == K - 1 ? n : res[i+1]);
        while(res[i] < nxt) {
            if(sum >= A && sum <= 2 * A){
                for(int j = 0; j < K; j++)
                    res[j] = arr[res[j]].first + 1;
                answer(res);
                return;
            }

            if(res[i] + 1 >= nxt)
                break;

            sum -= arr[res[i]].second;
            ++res[i];
            sum += arr[res[i]].second;
        }
    }

    impossible();
}

#ifndef ONLINE_JUDGE
/*
SAMPLE GRADER for task BOOKS

USAGE:
place together with your solution and books.h in the same directory, then:
g++ <flags> sample_grader.cpp <solution_file>
e.g.:
g++ -std=c++17 sample_grader.cpp books.cpp

INPUT/OUTPUT:
The sample grader expects on standard input two lines. The first line should
contain the four integers N, K, A and S. The second line should contain a list
of N integers, the sequence of difficulties x_1 x_2 ... x_N which has to be
strictly increasing. Then, the grader writes to standard output a protocol of
all grader functions called by your program.
At the end, the grader prints your verdict.
*/
#include<vector>
#include<cstdio>
#include<set>
#include<cstdlib>
#include<cstdarg>
#include<cassert>
#include"books.h"

using namespace std;

typedef long long ll;

void __attribute__((noreturn)) __attribute__((format(printf, 1, 2))) result(const char *msg, ...)
{
    va_list args;
    va_start(args, msg);
    vfprintf(stderr, msg, args);
    fprintf(stderr, "\n");
    va_end(args);
    exit(0);
}

namespace
{
    int N, K, S, sUsed;
    long long A;
    vector<long long> seq;
}

void impossible()
{
    result("Impossible (not checked): %d book(s) skimmed", sUsed);
    exit(0);
}

long long skim(int pos)
{
    printf("skim(%d): ", pos);
	if (pos<1 || pos>N) result("Invalid skim");
    printf("%lld\n", seq[pos]);
	sUsed++;
	if (sUsed>S) result("Out of books to skim");
    return seq[pos];
}

void answer(vector<int> v)
{
    printf("answer({");
    for(int i = 0; i < (int) v.size(); ++i)
    {
        printf("%d", v[i]);
        if(i + 1 != (int) v.size()) printf(", ");
    }
    printf("})\n");

	if ((int) v.size() != K) result("Invalid answer");
   	ll sum = 0;
	for(auto x: v) {
		if (x<1 || x>N) result("Invalid answer");
		sum += seq[x];
	}
	if (sum < A || 2*A<sum) result("Wrong answer");

	result("Correct: %d book(s) skimmed", sUsed);
    exit(0);
}

int main()
{
    if(scanf("%d %d %lld %d", &N, &K, &A, &S) != 4)
        result("Invalid input");

    seq.resize(N + 1);
    for(int i = 1; i <= N; ++i) {
    	if(scanf("%lld", &(seq[i])) != 1) result("Invalid input");
		if(i>1 && seq[i]<=seq[i-1]) result("Invalid input");
   	}

    solve(N, K, A, S);

    result("No answer");
}

#endif

컴파일 시 표준 에러 (stderr) 메시지

/usr/bin/ld: /tmp/cc0eSXMm.o: in function `answer(std::vector<int, std::allocator<int> >)':
grader.cpp:(.text+0xa0): multiple definition of `answer(std::vector<int, std::allocator<int> >)'; /tmp/ccE91gwK.o:books.cpp:(.text+0x2e0): first defined here
/usr/bin/ld: /tmp/cc0eSXMm.o: in function `impossible()':
grader.cpp:(.text+0x160): multiple definition of `impossible()'; /tmp/ccE91gwK.o:books.cpp:(.text+0x230): first defined here
/usr/bin/ld: /tmp/cc0eSXMm.o: in function `skim(int)':
grader.cpp:(.text+0x1a0): multiple definition of `skim(int)'; /tmp/ccE91gwK.o:books.cpp:(.text+0x250): first defined here
/usr/bin/ld: /tmp/cc0eSXMm.o: in function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccE91gwK.o:books.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status