답안 #486514

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
486514 2021-11-11T21:39:57 Z nicolaalexandra 코끼리 (Dancing Elephants) (IOI11_elephants) C++14
100 / 100
7059 ms 15656 KB
#include <bits/stdc++.h>
#define DIM 150010
#define DIMBUCKET 400
#include "elephants.h"
using namespace std;

int n,lg,nr_buckets,l,q,cnt,poz,val;
int what_bucket[DIM],x[DIM],w[DIM];
pair <int,int> v[DIM],aux[DIM],dp[DIMBUCKET][DIMBUCKET*2];
vector <pair<int,int> > buckets[DIMBUCKET];

void update_bucket (int bucket){

    int el = 0;
    for (auto it : buckets[bucket])
        aux[++el] = it;

    int j = el+1;
    for (int i=el;i;i--){

        while (aux[j-1].first > aux[i].first + l)
            j--;

        if (j <= el){
            dp[bucket][i].first = dp[bucket][j].first + 1;
            dp[bucket][i].second = dp[bucket][j].second;
        } else {
            dp[bucket][i].first = 1;
            dp[bucket][i].second = aux[i].first + l;
        }
    }

}


void build (){

    for (int i=1;i<=nr_buckets;i++)
        buckets[i].clear();
    sort (v+1,v+n+1);
    memset (what_bucket,0,sizeof what_bucket);
    memset (dp,0,sizeof dp);

    for (int i=1;i<=n;i++){
        what_bucket[v[i].second] = i / lg;
        if (i % lg)
            what_bucket[v[i].second]++;

        buckets[what_bucket[v[i].second]].push_back(v[i]);
    }

    for (int i=1;i<=nr_buckets;i++)
        sort (buckets[i].begin(),buckets[i].end());

    /// dp[bucket][i] - nr min de intervale cu care pot sa acopar toate punctele
    /// din bucketul asta, incepand de la al i - lea, si care e cea mai din dreapta poz
    /// pt care pot sa fac asta

    for (int i=1;i<=nr_buckets;i++){

        update_bucket(i);

    }


}

int query (){


    int i = 1, val = -1, poz, sol = 0;
    while (i <= nr_buckets){

        if (buckets[i].empty()){
            i++;
            continue;
        }

        if (val == -1){
            sol += dp[i][1].first;
            val = dp[i][1].second;

            i++;
        } else {

            if (buckets[i][ buckets[i].size()-1 ].first <= val)
                i++;

            else {

                /// caut binar pe primul > val

                int st = 1, dr = buckets[i].size(), sol_poz = 1;
                while (st <= dr){
                    int mid = (st+dr)>>1;
                    if (buckets[i][mid-1].first > val){
                        sol_poz = mid;
                        dr = mid-1;
                    } else st = mid+1;
                }

                sol += dp[i][sol_poz].first;
                val = dp[i][sol_poz].second;

                i++;

            }

        }

    }




    return sol;
}

void init (int _n, int _l, int x[]){

    n = _n, l = _l;

    lg = n / sqrt (n); /// lungime bucket

    nr_buckets = n / lg;
    if (n % lg)
        nr_buckets++;

    for (int i=1;i<=n;i++){
        v[i].first = x[i-1];
        v[i].second = i;
        w[i] = x[i-1];
    }

    build ();

}

void sterge (int idx, int val, int poz){
    int i;
    for (i=0;i<buckets[idx].size();i++)
        if (buckets[idx][i].first == val && buckets[idx][i].second == poz)
            break;

    for (int j=i+1;j<buckets[idx].size();j++)
        buckets[idx][j-1] = buckets[idx][j];

    buckets[idx].pop_back();
}

void adauga (int idx, int val, int poz){
    int i;
    for (i=0;i<buckets[idx].size();i++)
        if (val <= buckets[idx][i].first)
            break;
    buckets[idx].push_back(make_pair(0,0));
    for (int j=buckets[idx].size()-1;j>i;j--)
        buckets[idx][j] = buckets[idx][j-1];
    buckets[idx][i] = make_pair(val,poz);
}

int update (int poz, int val){
    cnt++;
    if (cnt == lg+1){ /// refac bucketurile
        for (int i=1;i<=n;i++)
            v[i] = make_pair(w[i],i);
        build();
        cnt = 0;
    }

    poz++;

    int idx = what_bucket[poz];

    sterge (idx,w[poz],poz);


    update_bucket (idx);

    /// trb sa vad in ce bucket l as pune
    w[poz] = val;
    //v[poz].first = val;

    int st = 1, dr = nr_buckets, sol = 1;
    while (st <= dr){
        int mid = (st+dr)>>1;

        if (buckets[mid][0].first <= val){
            sol = mid;
            st = mid+1;
        } else dr = mid-1;

    }

    what_bucket[poz] = sol;


    adauga(sol,val,poz);

    update_bucket(sol);


    return query();
}
/*
int main (){

    ifstream cin ("date.in");
    ofstream cout ("date.out");

    cin>>n>>l>>q;
    for (int i=0;i<n;i++)
        cin>>x[i];

    init(n,l,x);

    //cin>>q;
    for (;q--;){
        int useless;
        cin>>poz>>val>>useless;
        cout<<update (poz,val)<<"\n";
    }


    return 0;
}
*/

Compilation message

elephants.cpp: In function 'int query()':
elephants.cpp:71:26: warning: unused variable 'poz' [-Wunused-variable]
   71 |     int i = 1, val = -1, poz, sol = 0;
      |                          ^~~
elephants.cpp: In function 'void sterge(int, int, int)':
elephants.cpp:141:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  141 |     for (i=0;i<buckets[idx].size();i++)
      |              ~^~~~~~~~~~~~~~~~~~~~
elephants.cpp:145:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  145 |     for (int j=i+1;j<buckets[idx].size();j++)
      |                    ~^~~~~~~~~~~~~~~~~~~~
elephants.cpp: In function 'void adauga(int, int, int)':
elephants.cpp:153:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  153 |     for (i=0;i<buckets[idx].size();i++)
      |              ~^~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 3404 KB Output is correct
2 Correct 3 ms 3404 KB Output is correct
3 Correct 4 ms 3404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 3404 KB Output is correct
2 Correct 3 ms 3404 KB Output is correct
3 Correct 4 ms 3404 KB Output is correct
4 Correct 2 ms 3404 KB Output is correct
5 Correct 2 ms 3404 KB Output is correct
6 Correct 3 ms 3404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 3404 KB Output is correct
2 Correct 3 ms 3404 KB Output is correct
3 Correct 4 ms 3404 KB Output is correct
4 Correct 2 ms 3404 KB Output is correct
5 Correct 2 ms 3404 KB Output is correct
6 Correct 3 ms 3404 KB Output is correct
7 Correct 369 ms 5256 KB Output is correct
8 Correct 481 ms 5580 KB Output is correct
9 Correct 650 ms 6404 KB Output is correct
10 Correct 826 ms 6416 KB Output is correct
11 Correct 768 ms 6468 KB Output is correct
12 Correct 1273 ms 6468 KB Output is correct
13 Correct 877 ms 6388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 3404 KB Output is correct
2 Correct 3 ms 3404 KB Output is correct
3 Correct 4 ms 3404 KB Output is correct
4 Correct 2 ms 3404 KB Output is correct
5 Correct 2 ms 3404 KB Output is correct
6 Correct 3 ms 3404 KB Output is correct
7 Correct 369 ms 5256 KB Output is correct
8 Correct 481 ms 5580 KB Output is correct
9 Correct 650 ms 6404 KB Output is correct
10 Correct 826 ms 6416 KB Output is correct
11 Correct 768 ms 6468 KB Output is correct
12 Correct 1273 ms 6468 KB Output is correct
13 Correct 877 ms 6388 KB Output is correct
14 Correct 811 ms 6100 KB Output is correct
15 Correct 758 ms 6348 KB Output is correct
16 Correct 1846 ms 7304 KB Output is correct
17 Correct 2080 ms 8516 KB Output is correct
18 Correct 2234 ms 8388 KB Output is correct
19 Correct 1319 ms 8596 KB Output is correct
20 Correct 2082 ms 8452 KB Output is correct
21 Correct 2106 ms 8468 KB Output is correct
22 Correct 1449 ms 8132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 3404 KB Output is correct
2 Correct 3 ms 3404 KB Output is correct
3 Correct 4 ms 3404 KB Output is correct
4 Correct 2 ms 3404 KB Output is correct
5 Correct 2 ms 3404 KB Output is correct
6 Correct 3 ms 3404 KB Output is correct
7 Correct 369 ms 5256 KB Output is correct
8 Correct 481 ms 5580 KB Output is correct
9 Correct 650 ms 6404 KB Output is correct
10 Correct 826 ms 6416 KB Output is correct
11 Correct 768 ms 6468 KB Output is correct
12 Correct 1273 ms 6468 KB Output is correct
13 Correct 877 ms 6388 KB Output is correct
14 Correct 811 ms 6100 KB Output is correct
15 Correct 758 ms 6348 KB Output is correct
16 Correct 1846 ms 7304 KB Output is correct
17 Correct 2080 ms 8516 KB Output is correct
18 Correct 2234 ms 8388 KB Output is correct
19 Correct 1319 ms 8596 KB Output is correct
20 Correct 2082 ms 8452 KB Output is correct
21 Correct 2106 ms 8468 KB Output is correct
22 Correct 1449 ms 8132 KB Output is correct
23 Correct 4122 ms 14136 KB Output is correct
24 Correct 4615 ms 14124 KB Output is correct
25 Correct 3399 ms 14124 KB Output is correct
26 Correct 4688 ms 14144 KB Output is correct
27 Correct 5090 ms 13992 KB Output is correct
28 Correct 1262 ms 8260 KB Output is correct
29 Correct 1209 ms 8372 KB Output is correct
30 Correct 1240 ms 8272 KB Output is correct
31 Correct 1212 ms 8260 KB Output is correct
32 Correct 4350 ms 13636 KB Output is correct
33 Correct 4355 ms 12944 KB Output is correct
34 Correct 4535 ms 13780 KB Output is correct
35 Correct 4553 ms 14076 KB Output is correct
36 Correct 2929 ms 13556 KB Output is correct
37 Correct 7059 ms 15656 KB Output is correct
38 Correct 4875 ms 12780 KB Output is correct
39 Correct 4209 ms 13820 KB Output is correct
40 Correct 4862 ms 12880 KB Output is correct
41 Correct 6844 ms 13636 KB Output is correct
42 Correct 6933 ms 13892 KB Output is correct