Submission #674534

# Submission time Handle Problem Language Result Execution time Memory
674534 2022-12-25T00:30:24 Z Victor Akcija (COCI21_akcija) C++17
90 / 110
4291 ms 524288 KB
// #pragma GCC target ("avx,avx2,fma")
// #pragma GCC optimize ("Ofast,inline") // O1 - O2 - O3 - Os - Ofast
// #pragma GCC optimize ("unroll-loops")
#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define per(i, a, b) for (int i = (b) - 1; i >= (a); --i)
#define trav(a, x) for (auto &a : x)

#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define pb push_back
#define debug(x) cout<<#x<<" = "<<x<<endl

#define umap unordered_map
#define uset unordered_set

typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;

typedef long long ll;
typedef pair<ll,ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;

const int INF = 1'000'000'007;

vector<vi> node_products;
vector<vi> sorted_node_products;

vii replacements[2005];

int find_replacement_index(int deadline,int min_id,int start_pos) {
    rep(i,start_pos,sz(replacements[deadline])) if(replacements[deadline][i].second>=min_id) return i;
    return -1;
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    cin.exceptions(cin.failbit);

    int n,k;
    cin>>n>>k;
    vector<iii> products(n);

    rep(i,0,n) {
        cin>>products[i].first>>products[i].second.first;
        products[i].second.second=i;
    }

    sort(all(products));
    int cnt=0;
    ll sum=0;
    vector<bool> taken(n,0);

   vector<iii> optimal;
    {
        vi deadlines;
        int id=0;
        trav(product,products) {
            int cost=product.first,deadline,bla;
            tie(deadline,bla)=product.second;
            per(i,0,deadline) if(taken[i]==0) { 
                taken[i]=1;
                ++cnt;
                sum+=cost;
                optimal.pb({deadline,{cost,id}});
                deadlines.pb(deadline);
                break;
            } 
            ++id;
        }
        sort(all(deadlines));
        node_products.pb(deadlines);
        sorted_node_products.pb(deadlines);
    }

    per(i,0,sz(optimal)) {
        int id=optimal[i].second.second;
        products.erase(products.begin()+id);
    }

    //sort(all(optimal));

    rep(i,0,n) swap(products[i].first,products[i].second.first);
    sort(all(optimal));
    rep(i,0,n) swap(products[i].first,products[i].second.first);
    rep(i,0,n) products[i].second.second=i;

    trav(product,products) {
        int cost,deadline,id;
        cost=product.first;
        tie(deadline,id)=product.second;
        rep(i,0,deadline) replacements[i].pb({cost,id});
    }

    rep(i,0,2005) sort(all(replacements[i]));

    cnt=0;
    int unique_id=1;

    //cout<<sz(products)<<endl;
    //trav(product,products) debug(product.first);

    map<pair<int,pair<ll,int>>,pair<ii,ll>> nodes;
    map<pair<int,pair<ll,int>>,pair<ii,ll>> nodes2;
    nodes[{-sz(optimal),{sum,0}}]={{0,0},-1};

    while(cnt<k) {
        if(nodes.empty()) break;
        auto it=nodes.begin();

        int siz=-it->first.first,my_id=it->first.second.second;
        ll sum_cost=it->first.second.first;

        int product_index,pos_edit;
        tie(product_index,pos_edit)=it->second.first;
        ll move=it->second.second;
        nodes.erase(it);


        if(pos_edit%2==0) {
            pos_edit/=2;
            cout<<siz<<' '<<sum_cost<<endl;
            ++cnt;
            if(move!=-1) {
                int parent_id=move/(sz(products)+1);
                int new_product=move%(sz(products)+1);

                //debug(parent_id);
                //debug(new_product);
                //debug(my_id);
                //debug(sz(node_products));

                node_products[my_id]=node_products[parent_id];
                int add_deadline=n+1;
                if(new_product!=sz(products)) add_deadline=products[new_product].second.first;
                //cout<<"my_id = "<<my_id<<" pos_edit = "<<pos_edit<<" node_products = "<<sz(node_products[my_id])<<endl;
                node_products[my_id][pos_edit-1]=add_deadline;

                // sort
                // rep(i,pos_edit-1,n-1) if(node_products[my_id][i]>node_products[my_id][i+1]) swap(node_products[my_id][i],node_products[my_id][i+1]);
                // per(i,0,pos_edit-1) if(node_products[my_id][i]>node_products[my_id][i+1]) swap(node_products[my_id][i],node_products[my_id][i+1]);

                sorted_node_products[my_id]=node_products[my_id];
                sort(all(sorted_node_products[my_id]));
            }
            //cout<<endl;
            if(cnt==k) break;
        }

        else pos_edit/=2;

        int pos_cost=0;
        if(pos_edit!=sz(optimal)) pos_cost=optimal[pos_edit].second.first;
        int product_cost=0;
        if(product_index!=sz(products)) product_cost=products[product_index].first;

        //cout<<"siz = "<<siz<<" my_id = "<<my_id<<" sum_cost = "<<sum_cost<<endl;
        //cout<<"pos_edit = "<<pos_edit<<" product_index = "<<product_index<<" move = "<<move<<endl;
        //cout<<sz(sorted_node_products)<<endl;
        //cout<<endl;

        if(pos_edit!=sz(optimal)) {
            //nodes[{-siz,{sum_cost,my_id}}]={{product_index,(pos_edit+1)*2+1},true};
            rep(i,pos_edit,sz(optimal)) {nodes[{-(siz-1),{sum_cost-optimal[i].second.first,unique_id++}}]={{sz(products),(i+1)*2},sz(products)+ll(sz(products)+1)*my_id};

            node_products.pb(vi());
            sorted_node_products.pb(vi());
            }

            if(product_index!=sz(products)) {
                int min_deadline=0,pos_deadline=-1;
                int time_slot=0;

                vi min_deadlines(sz(optimal),2004);
                int j=pos_edit;
                rep(i,0,sorted_node_products[my_id].size()) {
                    if(j!=sz(min_deadlines)&&optimal[j].first==sorted_node_products[my_id][i]) min_deadlines[j]=min_deadline,++j;
                    if(sorted_node_products[my_id][i]==pos_deadline) {
                        pos_deadline=-1;
                        continue;
                    }
                    if(time_slot+1==sorted_node_products[my_id][i]) min_deadline=sorted_node_products[my_id][i];
                    time_slot++;
                }

                priority_queue<pair<ll,ii>> pq;
                rep(i,pos_edit,sz(optimal)) {
                    int replacement_index=find_replacement_index(min_deadlines[i],product_index,0);
                    //cout<<"i = "<<i<<" min_deadline = "<<min_deadlines[i]<<" product_index = "<<product_index<<endl;
                    //debug(replacement_index);
                    if(replacement_index!=-1) pq.push({-(sum_cost-optimal[i].second.first+replacements[min_deadlines[i]][replacement_index].first),{i,replacement_index}});
                }
                //cout<<endl;

                rep(_,0,k) {
                    if(pq.empty()) break;
                    ll cost=-pq.top().first;
                    int replacement_index;
                    tie(pos_edit,replacement_index)=pq.top().second;
                    pq.pop();

                    int i=replacements[min_deadlines[pos_edit]][replacement_index].second;
                    /*product_cost=products[i].first;
                    if(pos_cost>product_cost) {
                        cout<<"pos_cost = "<<pos_cost<<" product_cost = "<<product_cost<<endl;
                        cout<<"product_index = "<<product_index<<" min_deadline = "<<min_deadline<<" deadline = "<<products[i].second.first<<endl;
                        assert(0);
                    }*/

                    assert(i<sz(products));
                    nodes[{-siz,{cost,unique_id++}}]={{i+1,(pos_edit+1)*2},i+ll(sz(products)+1)*my_id};
                    node_products.pb(vi());
                    sorted_node_products.pb(vi());
                    

                    replacement_index=find_replacement_index(min_deadlines[pos_edit],product_index,replacement_index+1);
                    if(replacement_index!=-1) pq.push({-(sum_cost-optimal[pos_edit].second.first+replacements[min_deadlines[pos_edit]][replacement_index].first),{pos_edit,replacement_index}});
                }
            }
        }
        //if(product_index!=sz(products)) nodes[{-siz,{sum_cost,unique_id++}}]={{product_index+1,(pos_edit)*2+1},false}; // move product index

    }
    return 0;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:8:42: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    8 | #define rep(i, a, b) for (int i = (a); i < (b); ++i)
      |                                          ^
Main.cpp:183:17: note: in expansion of macro 'rep'
  183 |                 rep(i,0,sorted_node_products[my_id].size()) {
      |                 ^~~
Main.cpp:159:13: warning: variable 'pos_cost' set but not used [-Wunused-but-set-variable]
  159 |         int pos_cost=0;
      |             ^~~~~~~~
Main.cpp:161:13: warning: variable 'product_cost' set but not used [-Wunused-but-set-variable]
  161 |         int product_cost=0;
      |             ^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 724 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 3 ms 852 KB Output is correct
4 Correct 2 ms 724 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 2 ms 508 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 2 ms 724 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 724 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 3 ms 852 KB Output is correct
4 Correct 2 ms 724 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 2 ms 508 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 2 ms 724 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 2 ms 596 KB Output is correct
15 Correct 1 ms 416 KB Output is correct
16 Correct 2 ms 724 KB Output is correct
17 Correct 2 ms 512 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 2 ms 468 KB Output is correct
22 Correct 3 ms 852 KB Output is correct
23 Correct 3 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 896 KB Output is correct
2 Correct 2 ms 764 KB Output is correct
3 Correct 3 ms 980 KB Output is correct
4 Correct 2 ms 852 KB Output is correct
5 Correct 2 ms 760 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 372 KB Output is correct
9 Correct 2 ms 760 KB Output is correct
10 Correct 2 ms 508 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 1364 KB Output is correct
2 Correct 7 ms 2024 KB Output is correct
3 Correct 7 ms 1572 KB Output is correct
4 Correct 5 ms 1364 KB Output is correct
5 Correct 6 ms 1836 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 6 ms 1580 KB Output is correct
8 Correct 4 ms 724 KB Output is correct
9 Correct 4 ms 724 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 6 ms 1364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1108 KB Output is correct
2 Correct 4 ms 1364 KB Output is correct
3 Correct 6 ms 1652 KB Output is correct
4 Correct 4 ms 1620 KB Output is correct
5 Correct 3 ms 1364 KB Output is correct
6 Correct 1 ms 368 KB Output is correct
7 Correct 2 ms 1108 KB Output is correct
8 Correct 2 ms 752 KB Output is correct
9 Correct 2 ms 1108 KB Output is correct
10 Correct 1 ms 724 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 724 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 3 ms 852 KB Output is correct
4 Correct 2 ms 724 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 2 ms 508 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 2 ms 724 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 2 ms 596 KB Output is correct
15 Correct 1 ms 416 KB Output is correct
16 Correct 2 ms 724 KB Output is correct
17 Correct 2 ms 512 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 2 ms 468 KB Output is correct
22 Correct 3 ms 852 KB Output is correct
23 Correct 3 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 2 ms 896 KB Output is correct
28 Correct 2 ms 764 KB Output is correct
29 Correct 3 ms 980 KB Output is correct
30 Correct 2 ms 852 KB Output is correct
31 Correct 2 ms 760 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 468 KB Output is correct
34 Correct 1 ms 372 KB Output is correct
35 Correct 2 ms 760 KB Output is correct
36 Correct 2 ms 508 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 1 ms 368 KB Output is correct
40 Correct 6 ms 1364 KB Output is correct
41 Correct 7 ms 2024 KB Output is correct
42 Correct 7 ms 1572 KB Output is correct
43 Correct 5 ms 1364 KB Output is correct
44 Correct 6 ms 1836 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 6 ms 1580 KB Output is correct
47 Correct 4 ms 724 KB Output is correct
48 Correct 4 ms 724 KB Output is correct
49 Correct 1 ms 340 KB Output is correct
50 Correct 1 ms 468 KB Output is correct
51 Correct 1 ms 340 KB Output is correct
52 Correct 6 ms 1364 KB Output is correct
53 Correct 3 ms 1108 KB Output is correct
54 Correct 4 ms 1364 KB Output is correct
55 Correct 6 ms 1652 KB Output is correct
56 Correct 4 ms 1620 KB Output is correct
57 Correct 3 ms 1364 KB Output is correct
58 Correct 1 ms 368 KB Output is correct
59 Correct 2 ms 1108 KB Output is correct
60 Correct 2 ms 752 KB Output is correct
61 Correct 2 ms 1108 KB Output is correct
62 Correct 1 ms 724 KB Output is correct
63 Correct 1 ms 340 KB Output is correct
64 Correct 0 ms 340 KB Output is correct
65 Correct 1 ms 724 KB Output is correct
66 Runtime error 4291 ms 524288 KB Execution killed with signal 9
67 Halted 0 ms 0 KB -