172582025-06-08 12:07:11algoproGamecpp17Időlimit túllépés 30/1002.599s5432 KiB
// UUID: dda06e14-2abe-4ed6-a9e8-450076f1f4ca
#include <iostream>
#include <vector>
#include <set> // Include the header for multiset

const int MAX_N = 1e5;
int a[MAX_N];

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);

    int n, k;
    std::cin >> n >> k;
    for (int i = 0; i < n; ++i) {
        std::cin >> a[i];
    }
        
    for (; k > 0; --k) {
        int p;
        std::cin >> p;

        // Use std::multiset to correctly handle duplicate cards while keeping them sorted.
        std::multiset<int> available_cards;
        for (int i = 0; i < p; ++i) {
            available_cards.insert(a[i]);
        }
            
        int pocket = 0;
        long long score = 0;
        int player = 1;
        
        // This variable correctly mimics the stateful 'mx' from the original logic.
        // It's the boundary used to decide where the opponent's next card goes.
        int mx_boundary = n; 

        // --- Main game simulation loop 1 ---
        for (int i = p; i < n; ++i, player *= -1) {
            int card_to_play;
            if (pocket) {
                card_to_play = pocket;
                score += (long long)card_to_play * player;
                pocket = 0;
                // Note: mx_boundary is NOT updated when playing from the pocket.
            } else {
                // Get the largest element from the hand.
                auto it = prev(available_cards.end());
                card_to_play = *it;
                available_cards.erase(it);
                
                score += (long long)card_to_play * player;
                
                // CRUCIAL: Update the boundary ONLY when a card is played from the hand.
                mx_boundary = card_to_play;
            }

            // The opponent draws card a[i]. The comparison uses the persistent boundary.
            if (a[i] <= mx_boundary) {
                available_cards.insert(a[i]);
            } else {
                pocket = a[i];
            }
        }
        
        // --- Final turns simulation loop 2 ---
        // This loop now correctly simulates the end-game using the remaining cards.
        for (int i = 0; i < p; ++i, player *= -1) {
            if (pocket) {
                score += (long long)pocket * player;
                pocket = 0;
            } else {
                if (!available_cards.empty()) {
                    auto it = prev(available_cards.end());
                    int mx = *it;
                    available_cards.erase(it);
                    score += (long long)mx * player;
                }
            }
        }
        std::cout << score << '\n';
    }
    return 0;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
subtask110/10
1Elfogadva1ms316 KiB
2Elfogadva1ms316 KiB
subtask220/20
1Elfogadva3ms316 KiB
2Elfogadva2ms316 KiB
3Elfogadva14ms316 KiB
4Elfogadva35ms316 KiB
subtask30/70
1Elfogadva340ms564 KiB
2Elfogadva365ms576 KiB
3Elfogadva1.245s824 KiB
4Elfogadva1.526s940 KiB
5Időlimit túllépés2.585s3124 KiB
6Időlimit túllépés2.599s2868 KiB
7Időlimit túllépés2.592s2868 KiB
8Időlimit túllépés2.584s4404 KiB
9Időlimit túllépés2.582s4916 KiB
10Időlimit túllépés2.584s5168 KiB
11Időlimit túllépés2.579s5432 KiB
12Időlimit túllépés2.581s4660 KiB
13Időlimit túllépés2.582s5220 KiB
14Időlimit túllépés2.599s5428 KiB