제출 #1188122

#제출 시각아이디문제언어결과실행 시간메모리
1188122Mher777A Difficult(y) Choice (BOI21_books)C++20
0 / 100
0 ms412 KiB
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <array>
#include <string>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <list>
#include <iterator>
#include <numeric>
#include <complex>
#include <utility>
#include <random>
#include <cassert>
#include <fstream>
using namespace std;
#include "books.h"
mt19937 rnd(time(nullptr));
using namespace std;

/* -------------------- Typedefs -------------------- */

typedef int itn;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef float fl;
typedef long double ld;

/* -------------------- Usings -------------------- */

using vi = vector<int>;
using vll = vector<ll>;
using mii = map<int, int>;
using mll = map<ll, ll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

/* -------------------- Defines -------------------- */

#define ff first
#define ss second
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define mpr make_pair
#define yes cout<<"Yes\n"
#define no cout<<"No\n"
#define all(x) (x).begin(), (x).end()
#define USACO freopen("feast.in", "r", stdin); freopen("feast.out", "w", stdout);

/* -------------------- Constants -------------------- */

const int dx[8] = { -1, 0, 1, 0, -1, -1, 1, 1 };
const int dy[8] = { 0, -1, 0, 1, -1, 1, -1, 1 };
const int MAX = int(1e9 + 5);
const ll MAXL = ll(1e18) + 5ll;
const ll MOD = ll(1000000007);
const ll MOD2 = ll(998244353);

/* -------------------- Functions -------------------- */

vector<long long> first_books, last_books;
long long sum_first, sum_last;
int N, K;
long long A;


void solve(int N, int K, long long A, int S) {
    ::N = N; ::K = K; ::A = A;

    // Skim first K books
    first_books.clear();
    sum_first = 0;
    for (int i = 1; i <= K; ++i) {
        long long x = skim(i);
        first_books.push_back(x);
        sum_first += x;
    }
    if (sum_first > 2 * A) {
        impossible();
        return;
    }
    if (sum_first >= A && sum_first <= 2 * A) {
        vector<int> ans;
        for (int i = 1; i <= K; ++i) ans.push_back(i);
        answer(ans);
        return;
    }

    // Skim last K books
    last_books.clear();
    sum_last = 0;
    for (int i = N - K + 1; i <= N; ++i) {
        long long x = skim(i);
        last_books.push_back(x);
        sum_last += x;
    }
    if (sum_last < A) {
        impossible();
        return;
    }
    if (sum_last >= A && sum_last <= 2 * A) {
        vector<int> ans;
        for (int i = N - K + 1; i <= N; ++i) ans.push_back(i);
        answer(ans);
        return;
    }

    // Binary search for a
    int low = 0, high = K;
    int best_a = -1;
    while (low <= high) {
        int mid = (low + high) / 2;
        int a = mid;
        int b = K - a;

        if (a < 0 || b < 0) continue;

        long long current_sum = 0;
        if (a > 0) {
            current_sum += accumulate(first_books.begin(), first_books.begin() + a, 0LL);
        }
        if (b > 0) {
            current_sum += accumulate(last_books.end() - b, last_books.end(), 0LL);
        }

        if (current_sum >= A) {
            best_a = mid;
            high = mid - 1;
        }
        else {
            low = mid + 1;
        }
    }

    if (best_a == -1) {
        impossible();
        return;
    }

    int a = best_a;
    int b = K - a;

    long long sum_a = 0;
    if (a > 0) sum_a = accumulate(first_books.begin(), first_books.begin() + a, 0LL);
    long long sum_b = 0;
    if (b > 0) sum_b = accumulate(last_books.end() - b, last_books.end(), 0LL);
    long long total = sum_a + sum_b;

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

    vector<int> ans;
    for (int i = 0; i < a; ++i) ans.push_back(i + 1); // first a books are 1..a
    for (int i = 0; i < b; ++i) ans.push_back(N - b + 1 + i); // last b books are N-b+1..N

    answer(ans);
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...