답안 #1075264

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1075264 2024-08-25T22:16:42 Z oscar1f Sprinklers (CEOI24_sprinklers) C++17
100 / 100
1749 ms 55972 KB
#include<bits/stdc++.h>
using namespace std;

#define int long long

const int TAILLE_MAX=100*1000+5,INFINI=1000*1000*1000+5;
int nbArro,nbFleur,tailleInter;
vector<int> listeArro,listeFleur;
set<pair<int,int>> fleurTri,arroTri;
unordered_map<int,int> dejaArro;
unordered_map<int,int> memo,dv,proFleur;
unordered_map<int,char> direc;
int suivGau[TAILLE_MAX],suivDro[TAILLE_MAX],premAp[TAILLE_MAX],premAv[TAILLE_MAX];

void afficher(vector<int> v) {
    for (int i:v) {
        cout<<i<<" ";
    }
    cout<<endl;
}

int dyna(int pos,int fleurPb) {
    if (fleurPb==nbFleur) {
        return 1;
    }
    if (pos>nbArro or pos>premAp[fleurPb]+2) {
        return 0;
    }
    int caseTab=pos*INFINI+fleurPb;
    if (dv[caseTab]!=0) {
        return memo[caseTab];
    }
    dv[caseTab]=1;
    if (listeArro[pos-1]<listeFleur[fleurPb]) {
        if (premAv[fleurPb]>pos) {
            memo[caseTab]=dyna(premAv[fleurPb],fleurPb);
            return memo[caseTab];
        }
        if (listeArro[pos]<listeFleur[fleurPb]) {
            memo[caseTab]=dyna(pos+1,suivDro[pos]);
            proFleur[caseTab]=suivDro[pos];
            direc[caseTab]='R';
        }
        else if (listeArro[pos]-tailleInter>listeFleur[fleurPb]) {
            memo[caseTab]=0;
        }
        else {
            if (dyna(pos+1,fleurPb)==1) {
                memo[caseTab]=1;
                proFleur[caseTab]=fleurPb;
                direc[caseTab]='R';
            }
            else if (dyna(pos+1,suivGau[pos])==1) {
                memo[caseTab]=1;
                proFleur[caseTab]=suivGau[pos];
                direc[caseTab]='L';
            }
        }
    }
    else {
        if (listeArro[pos]-tailleInter>listeFleur[fleurPb]) {
            memo[caseTab]=0;
        }
        else {
            if (dyna(pos+1,fleurPb)==1) {
                memo[caseTab]=1;
                proFleur[caseTab]=fleurPb;
                direc[caseTab]='R';
            }
            else if (dyna(pos+1,max(suivGau[pos],suivDro[pos-1]))==1) {
                memo[caseTab]=1;
                proFleur[caseTab]=max(suivGau[pos],suivDro[pos-1]);
                direc[caseTab]='L';
            }
        }
    }
    return memo[caseTab];
}

pair<bool,string> verif(int valQuest) {
    tailleInter=valQuest;
    memo.clear();
    dv.clear();
    proFleur.clear();
    direc.clear();
    set<pair<int,int>>::iterator it;
    for (int i=0;i<=nbArro;i++) {
        it=fleurTri.lower_bound({listeArro[i],2*INFINI});
        suivGau[i]=(*it).second;
        it=fleurTri.lower_bound({listeArro[i]+tailleInter,2*INFINI});
        suivDro[i]=(*it).second;
    }
    for (int i=0;i<nbFleur;i++) {
        it=arroTri.lower_bound({listeFleur[i],0});
        it--;
        premAv[i]=(*it).second;
    }
    if (dyna(1,0)==0) {
        return {false,""};
    }
    string ans;
    //cout<<tailleInter<<" : ";
    int fleurPb=0,caseTab;
    for (int i=1;i<=nbArro;i++) {
        caseTab=i*INFINI+fleurPb;
        //cout<<i<<" "<<fleurPb<<" "<<direc[caseTab]<<endl;
        if (direc[caseTab]!=(char)0) {
            ans+=direc[caseTab];
        }
        else {
            ans+='L';
        }
        fleurPb=max(proFleur[caseTab],fleurPb);
    }
    //cout<<ans<<endl;
    return {true,ans};
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin>>nbArro>>nbFleur;
    int valNouv;
    listeArro.push_back(-2*INFINI);
    for (int i=0;i<nbArro;i++) {
        cin>>valNouv;
        dejaArro[valNouv]=1;
        listeArro.push_back(valNouv);
        arroTri.insert({valNouv,i});
    }
    listeArro.push_back(2*INFINI);
    arroTri.insert({2*INFINI,nbArro+1});
    arroTri.insert({-2*INFINI,-1});
    int numFleur=0;
    for (int i=0;i<nbFleur;i++) {
        cin>>valNouv;
        if (dejaArro[valNouv]==0) {
            listeFleur.push_back(valNouv);
            fleurTri.insert({valNouv,numFleur});
            dejaArro[valNouv]=1;
            numFleur++;
        }
    }
    //afficher(listeFleur);
    if (listeFleur.empty()) {
        cout<<0<<endl;
        for (int i=0;i<nbArro;i++) {
            cout<<'L';
        }
        cout<<endl;
        return 0;
    }
    nbFleur=listeFleur.size();
    fleurTri.insert({5*INFINI,nbFleur});
    fleurTri.insert({-5*INFINI,-1});
    set<pair<int,int>>::iterator it;
    for (int i=0;i<nbFleur;i++) {
        it=arroTri.lower_bound({listeFleur[i],INFINI});
        premAp[i]=(*it).second;
    }

    int debDicho=0,finDicho=INFINI,midDicho;
    while (debDicho!=finDicho) {
        midDicho=(debDicho+finDicho)/2;
        if (verif(midDicho).first) {
            finDicho=midDicho;
        }
        else {
            debDicho=midDicho+1;
        }
    }
    if (debDicho==INFINI) {
        cout<<-1<<endl;
    }
    else {
        cout<<debDicho<<endl;
        cout<<verif(debDicho).second<<endl;
    }
    return 0;
}
 
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Correct
2 Correct 1 ms 2396 KB Correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Correct
2 Correct 28 ms 10468 KB Correct
3 Correct 0 ms 2392 KB Correct
4 Correct 48 ms 15172 KB Correct
5 Correct 53 ms 15160 KB Correct
6 Correct 0 ms 2392 KB Correct
7 Correct 1 ms 2396 KB Correct
8 Correct 8 ms 4824 KB Correct
9 Correct 0 ms 2396 KB Correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Correct
2 Correct 56 ms 10980 KB Correct
3 Correct 53 ms 5200 KB Correct
4 Correct 978 ms 37184 KB Correct
5 Correct 835 ms 36772 KB Correct
6 Correct 1 ms 2392 KB Correct
7 Correct 0 ms 2396 KB Correct
8 Correct 411 ms 24668 KB Correct
9 Correct 458 ms 24980 KB Correct
10 Correct 654 ms 30720 KB Correct
11 Correct 308 ms 22028 KB Correct
12 Correct 346 ms 21792 KB Correct
13 Correct 872 ms 36856 KB Correct
14 Correct 1042 ms 40448 KB Correct
15 Correct 935 ms 40676 KB Correct
16 Correct 620 ms 31508 KB Correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Correct
2 Correct 1 ms 2396 KB Correct
3 Correct 1 ms 2396 KB Correct
4 Correct 1 ms 2396 KB Correct
5 Correct 1 ms 2652 KB Correct
6 Correct 1 ms 2652 KB Correct
7 Correct 1 ms 2396 KB Correct
8 Correct 1 ms 2396 KB Correct
9 Correct 1 ms 2652 KB Correct
10 Correct 1 ms 2652 KB Correct
11 Correct 1 ms 2396 KB Correct
12 Correct 1 ms 2520 KB Correct
13 Correct 1 ms 2652 KB Correct
14 Correct 1 ms 2396 KB Correct
15 Correct 1 ms 2392 KB Correct
16 Correct 1 ms 2396 KB Correct
17 Correct 1 ms 2396 KB Correct
18 Correct 1 ms 2396 KB Correct
19 Correct 1 ms 2652 KB Correct
20 Correct 1 ms 2652 KB Correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Correct
2 Correct 200 ms 13692 KB Correct
3 Correct 1565 ms 55356 KB Correct
4 Correct 1749 ms 55860 KB Correct
5 Correct 1608 ms 54492 KB Correct
6 Correct 1623 ms 55056 KB Correct
7 Correct 1739 ms 55972 KB Correct
8 Correct 1633 ms 54932 KB Correct
9 Correct 1623 ms 54984 KB Correct
10 Correct 1598 ms 54504 KB Correct
11 Correct 1687 ms 55656 KB Correct
12 Correct 0 ms 2396 KB Correct
13 Correct 1 ms 2396 KB Correct
14 Correct 32 ms 11776 KB Correct
15 Correct 396 ms 25652 KB Correct
16 Correct 425 ms 25908 KB Correct
17 Correct 318 ms 20112 KB Correct
18 Correct 306 ms 20448 KB Correct
19 Correct 328 ms 20904 KB Correct
20 Correct 1481 ms 54512 KB Correct
21 Correct 1398 ms 54252 KB Correct
22 Correct 1150 ms 49720 KB Correct
23 Correct 1184 ms 49716 KB Correct
24 Correct 713 ms 30368 KB Correct
25 Correct 668 ms 30340 KB Correct
26 Correct 543 ms 24068 KB Correct
27 Correct 457 ms 21716 KB Correct
28 Correct 995 ms 40016 KB Correct
29 Correct 639 ms 31136 KB Correct
30 Correct 845 ms 36784 KB Correct
31 Correct 1226 ms 49132 KB Correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Correct
2 Correct 1 ms 2396 KB Correct
3 Correct 28 ms 10468 KB Correct
4 Correct 0 ms 2392 KB Correct
5 Correct 48 ms 15172 KB Correct
6 Correct 53 ms 15160 KB Correct
7 Correct 0 ms 2392 KB Correct
8 Correct 1 ms 2396 KB Correct
9 Correct 8 ms 4824 KB Correct
10 Correct 0 ms 2396 KB Correct
11 Correct 56 ms 10980 KB Correct
12 Correct 53 ms 5200 KB Correct
13 Correct 978 ms 37184 KB Correct
14 Correct 835 ms 36772 KB Correct
15 Correct 1 ms 2392 KB Correct
16 Correct 0 ms 2396 KB Correct
17 Correct 411 ms 24668 KB Correct
18 Correct 458 ms 24980 KB Correct
19 Correct 654 ms 30720 KB Correct
20 Correct 308 ms 22028 KB Correct
21 Correct 346 ms 21792 KB Correct
22 Correct 872 ms 36856 KB Correct
23 Correct 1042 ms 40448 KB Correct
24 Correct 935 ms 40676 KB Correct
25 Correct 620 ms 31508 KB Correct
26 Correct 1 ms 2396 KB Correct
27 Correct 1 ms 2396 KB Correct
28 Correct 1 ms 2652 KB Correct
29 Correct 1 ms 2652 KB Correct
30 Correct 1 ms 2396 KB Correct
31 Correct 1 ms 2396 KB Correct
32 Correct 1 ms 2652 KB Correct
33 Correct 1 ms 2652 KB Correct
34 Correct 1 ms 2396 KB Correct
35 Correct 1 ms 2520 KB Correct
36 Correct 1 ms 2652 KB Correct
37 Correct 1 ms 2396 KB Correct
38 Correct 1 ms 2392 KB Correct
39 Correct 1 ms 2396 KB Correct
40 Correct 1 ms 2396 KB Correct
41 Correct 1 ms 2396 KB Correct
42 Correct 1 ms 2652 KB Correct
43 Correct 1 ms 2652 KB Correct
44 Correct 200 ms 13692 KB Correct
45 Correct 1565 ms 55356 KB Correct
46 Correct 1749 ms 55860 KB Correct
47 Correct 1608 ms 54492 KB Correct
48 Correct 1623 ms 55056 KB Correct
49 Correct 1739 ms 55972 KB Correct
50 Correct 1633 ms 54932 KB Correct
51 Correct 1623 ms 54984 KB Correct
52 Correct 1598 ms 54504 KB Correct
53 Correct 1687 ms 55656 KB Correct
54 Correct 0 ms 2396 KB Correct
55 Correct 1 ms 2396 KB Correct
56 Correct 32 ms 11776 KB Correct
57 Correct 396 ms 25652 KB Correct
58 Correct 425 ms 25908 KB Correct
59 Correct 318 ms 20112 KB Correct
60 Correct 306 ms 20448 KB Correct
61 Correct 328 ms 20904 KB Correct
62 Correct 1481 ms 54512 KB Correct
63 Correct 1398 ms 54252 KB Correct
64 Correct 1150 ms 49720 KB Correct
65 Correct 1184 ms 49716 KB Correct
66 Correct 713 ms 30368 KB Correct
67 Correct 668 ms 30340 KB Correct
68 Correct 543 ms 24068 KB Correct
69 Correct 457 ms 21716 KB Correct
70 Correct 995 ms 40016 KB Correct
71 Correct 639 ms 31136 KB Correct
72 Correct 845 ms 36784 KB Correct
73 Correct 1226 ms 49132 KB Correct
74 Correct 87 ms 13016 KB Correct
75 Correct 1008 ms 44524 KB Correct
76 Correct 1274 ms 46196 KB Correct
77 Correct 1 ms 2392 KB Correct
78 Correct 346 ms 24704 KB Correct
79 Correct 461 ms 24972 KB Correct
80 Correct 572 ms 29928 KB Correct
81 Correct 300 ms 25404 KB Correct
82 Correct 326 ms 25788 KB Correct
83 Correct 369 ms 25656 KB Correct
84 Correct 117 ms 10708 KB Correct
85 Correct 1096 ms 44480 KB Correct
86 Correct 1103 ms 44608 KB Correct
87 Correct 1194 ms 49176 KB Correct
88 Correct 52 ms 15172 KB Correct
89 Correct 52 ms 15156 KB Correct