QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#118923#1914. Falling Portalsbashkort#7.142857 59ms12480kbC++204.1kb2023-07-04 15:35:572024-05-31 18:59:27

Judging History

你现在查看的是最新测评结果

  • [2024-05-31 18:59:27]
  • 评测
  • 测评结果:7.142857
  • 用时:59ms
  • 内存:12480kb
  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-04 15:35:57]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using db = long double;

struct Frac {
    ll a = 1e9, b = 1;
    Frac() = default;
    Frac(ll a, ll b) : a(a), b(b) {}
    bool operator<(const Frac &oth) const { return a * oth.b < b * oth.a; }
    bool operator>(const Frac &oth) const { return a * oth.b > b * oth.a; }
    bool operator==(const Frac &oth) const { return a * oth.b == b * oth.a; }
};

struct Line {
    ll k = 0, b = -1e18;
    Line() = default;
    Line(ll k, ll b) : k(k), b(b) {}
    db inter(const Line &oth) const { return (b - oth.b) / db(oth.k - k); }
    Frac f(ll h, int i) const {
        if (-b < h || k < i) {
            return {};
        }
        if (k == i) {
            assert(h == -b);
            return {0, 1};
        }
        assert(k > i);
        assert(-b >= h);
        return Frac((-b - h), k - i);
    }
};

struct CHT {
    vector<Line> t;

    void insert(const Line &v) {
        assert(t.empty() || t.back().k > v.k);
        while (size(t) > 1 && v.inter(t.end()[-2]) < v.inter(t.back())) {
            t.pop_back();
        }
        t.push_back(v);
    }

    Frac query(ll h, int i) {
        if (t.empty()) {
            return {};
        }
        int lo = 0;
        while (lo < size(t) && !(t[lo].f(h, i) < Frac())) {
            lo += 1;
        }
        if (lo == size(t)) {
            return Frac();
        }
        int hi = size(t) - 1;
        while (lo < hi) {
            int mid = lo + hi >> 1;
            if (t[mid].f(h, i) < t[mid + 1].f(h, i)) {
                hi = mid;
            } else {
                lo = mid + 1;
            }
        }
        return t[lo].f(h, i);
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;

    vector<int> h(n), Q(n);

    for (int i = 0; i < n; ++i) {
        cin >> h[i];
    }
    for (int i = 0; i < n; ++i) {
        cin >> Q[i];
        --Q[i];
    }

    vector<Frac> answers(n);

    auto solve = [&]() -> vector<Frac> {
        const int sz = 1 << __lg(n) + !!(n & (n - 1));
        CHT tree;

        vector<int> ord(n), inv(n), head(n);
        iota(ord.begin(), ord.end(), 0);
        sort(ord.begin(), ord.end(), [&](int i, int j) {
            return h[i] < h[j];
        });

        for (int i = 0, j = 0; i < n; i = j) {
            while (j < n && h[ord[i]] == h[ord[j]]) {
                j += 1;
            }
            int f = i;
            while (i < j) {
                inv[ord[i]] = i;
                head[ord[i]] = f;
                i += 1;
            }
        }

        vector<vector<int>> queries(n + 1);

        for (int s = 0; s < n; ++s) {
            int t = Q[s];
            if (h[t] <= h[s]) {
                queries[max(s, t) + 1].push_back(s);
            }
        }

        vector<Frac> ans(n);

        for (int i = n - 1; i >= 0; --i) {
            tree.insert({i, -h[i]});
            for (int s : queries[i]) {
                int t = Q[s];
                ans[s] = tree.query(h[t], t);
            }
        }

        return ans;
    };

    answers = solve();

    for (int s = 0; s < n; ++s) {
        int t = Q[s];
        if (t < s && h[t] <= h[s]) {
            answers[s] = min(answers[s], Frac(h[s] - h[t], s - t));
        } else if (t > s && h[t] >= h[s]) {
            answers[s] = min(answers[s], Frac(h[t] - h[s], t - s));
        }
    }

    reverse(h.begin(), h.end());
    reverse(Q.begin(), Q.end());
    for (int i = 0; i < n; ++i) {
        h[i] = 1e9 - h[i] + 1;
        Q[i] = n - Q[i] - 1;
    }

    auto sec = solve();
    reverse(sec.begin(), sec.end());

    for (int i = 0; i < n; ++i) {
        answers[i] = min(answers[i], sec[i]);
    }

    for (int s = 0; s < n; ++s) {
        Frac ans = answers[s];
        ll d = gcd(ans.a, ans.b);
        ans.a /= d, ans.b /= d;
        if (ans < Frac(1e9, 1)) {
            cout << ans.a << "/" << ans.b << '\n';
        } else {
            cout << "-1\n";
        }
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 7.14286
Accepted
time: 0ms
memory: 3800kb

input:

4
3 5 10 2
3 3 2 1

output:

7/2
7/2
5/1
-1

result:

ok 4 lines

Test #2:

score: 0
Wrong Answer
time: 47ms
memory: 11836kb

input:

100000
472909419 819415545 354941190 517113826 307239833 831177800 578834772 704837117 326642977 846249751 997452224 322958397 143787473 278464288 864820832 175161120 545824609 549197171 224617065 387719870 938593011 671365566 982006335 210657906 552581470 851423470 973444746 978545568 66820178 6528...

output:

152285674/1
188638241/1
167285155/42617
82564959/16297
148851281/50439
121823733/1
143228531/51587
50966299/35524
-1
-1
-1
128612227/16712
13767472/3
20119164/2731
-1
270852785/68242
-1
104859421/1
259960599/11905
108904549/1226
391595483/1
30735301/18371
727062875/1
41420828/2493
290914798/1
-1
663...

result:

wrong answer 1st lines differ - expected: '118375763/30533', found: '152285674/1'

Test #3:

score: 0
Wrong Answer
time: 50ms
memory: 11896kb

input:

100000
315623747 979206700 183094144 181505867 2572616 772718102 942741911 19472958 458398376 484546774 348949045 306690702 414701395 131437507 43708479 224826052 238532133 327518511 70279102 323433367 124141008 73101443 605805888 102782315 282343812 345273900 201043659 430908518 243739589 724275027...

output:

130531/66524
-1
341016653/1
158869574/47677
88395529/872
51894881/1
365939578/1
772204432/46505
107346530/803
274258869/1
93431123/3
158001/82
129734587/30488
192751841/19744
65706211/4
8888406/2993
93984043/9778
32820565/86973
467190/79
565967676/96875
148151899/48956
171964522/88855
15779020/7
850...

result:

wrong answer 2nd lines differ - expected: '292813/7595', found: '-1'

Test #4:

score: 0
Wrong Answer
time: 53ms
memory: 12480kb

input:

100000
1 4273 15397 29998 27927 48335 59992 44642 37593 89985 41136 43307 119975 48665 62351 149962 8744 116987 179946 98859 144057 209927 19516 41099 239906 183405 142896 269883 7437 83101 299858 174434 127933 329832 91448 39029 359803 174356 124036 389771 136441 166104 419738 196420 51865 449702 2...

output:

214487230/35559
88821503/17702
32033856/10529
33468604/86295
109325537/77025
75917/29913
140632343/20624
3222270/4547
136640093/16824
49022721/6184
17211212/15527
12654855/15206
45485815/97949
2089114/31923
45980153/13844
267807/89728
47193961/5878
132155797/65564
98104714/59561
939951/3643
43130746...

result:

wrong answer 5th lines differ - expected: '13665951/9628', found: '109325537/77025'

Test #5:

score: 0
Wrong Answer
time: 47ms
memory: 11944kb

input:

100000
1 933326351 11431 29998 989129972 37227 59992 989874362 62527 89984 979798680 75881 119974 982112916 19608 149961 995611915 151806 179946 913593466 132088 209929 930585647 149041 239909 930246483 218498 269887 972955258 54548 299863 927767028 295901 329836 953302565 223592 359806 995522236 31...

output:

221281385/50982
869331964/1
52985461/6135
591339/1969
3068967/1
12883181/29808
147900748/33431
134430317/1
912853771/6695
5773989/53987
925430279/1
1088062/161
177462577/24324
933346527/1
458487833/4186
216526559/36312
131293085/1
1073699/14360
56115974/10695
336651052/1
191001215/35949
9677459/9995...

result:

wrong answer 2nd lines differ - expected: '401837273/42017', found: '869331964/1'

Test #6:

score: 0
Wrong Answer
time: 49ms
memory: 11748kb

input:

100000
1 944663363 19033 29999 978821096 9085 59995 944910920 6257 89989 935899956 82545 119982 940520499 117973 149972 912135517 105359 179960 992861910 149037 209947 995445085 200979 239932 965159106 3211 269915 909296230 233961 299895 975484316 298245 329873 977094068 214362 359849 995636217 2225...

output:

2982983/44613
724008408/1
162411978/68491
7391521/7630
863403371/1
198467397/24358
13153700/88479
845880695/1
26745514/6821
164044531/68157
750958283/1
223786792/42439
62467229/83037
745895306/1
8936448/901
202494709/30804
850373562/1
201626116/59143
8441109/1285
811355267/1
390918461/99829
15846755...

result:

wrong answer 2nd lines differ - expected: '105551441/9703', found: '724008408/1'

Test #7:

score: 0
Wrong Answer
time: 0ms
memory: 3520kb

input:

100
534473706 617535198 746655229 582822181 350704338 460330801 735571468 88013273 362424504 855094440 710187915 697682713 997401051 460972806 717634399 444280514 755460273 944591484 265200138 839544060 662809699 661469793 497227720 278087261 937000185 722532327 468846527 92827045 15385128 139854649...

output:

460073995/39
12887123/5
-1
89666677/87
56359354/13
95172478/11
105100478/5
64096843/70
69791749/2
380182295/1
139453261/30
-1
47435537/1
242217149/1
85267635/4
19704644/1
8612481/7
12887123/5
320687502/17
14372905/1
282646370/1
-1
129514865/2
24414189/53
34722875/24
106422023/18
-1
-1
248966439/38
1...

result:

wrong answer 2nd lines differ - expected: '499461693/79', found: '12887123/5'

Test #8:

score: 0
Wrong Answer
time: 0ms
memory: 3792kb

input:

100
832359132 241086117 70268513 75641100 493265869 8969911 580115154 253097306 322151607 572536179 103433364 769475382 58915476 7943159 674934181 286336577 158800591 167266000 146088101 655190514 41590604 326590745 562776717 281254161 127537826 133491925 80685740 116325615 999795567 247078385 96567...

output:

-1
252179752/3
238403329/67
89450727/91
47249794/7
141636926/51
270049998/1
27792264/7
532938928/1
-1
11144545/2
-1
62774260/33
10045243/9
140034057/1
145892371/43
66348905/24
127748535/34
127748535/34
9175579/4
-1
4460961/13
250384572/1
3559760/3
5002900/27
-1
152030481/61
91512785/6
-1
56941787/73...

result:

wrong answer 3rd lines differ - expected: '37201903/6', found: '238403329/67'

Test #9:

score: 0
Wrong Answer
time: 1ms
memory: 3724kb

input:

2000
21505957 142018840 50835846 230286771 619214141 711966088 451418032 83312406 997278214 235672441 776998092 992475976 912444334 252212792 547270894 875994775 420249958 660039013 284835603 996966995 545404037 762620339 218177198 216536966 712712166 284586306 598975962 889965588 251816698 28540064...

output:

621739397/749
21379537/2
482154380/809
71254561/58
106942807/248
368582114/1
141060098/1665
119436143/17
-1
16853958/5
137708607/1814
-1
82978779/1
234038969/1165
194193695/1
-1
-1
72179485/12
166259/59
6444859/50
-1
-1
97064431/522
121722629/53
700110256/1
375503455/254
-1
21948438/1
64789171/438
1...

result:

wrong answer 2nd lines differ - expected: '68924125/851', found: '21379537/2'

Test #10:

score: 0
Wrong Answer
time: 1ms
memory: 3724kb

input:

2000
797776435 328363264 380792931 830287509 570335429 882870824 429432401 710728673 945865610 940849228 403089500 796543599 901854446 712568094 68385691 346788092 179631402 72575296 680321593 139773675 517849401 190654156 839257552 981435241 992409916 887842045 919377993 677204575 940817503 5589288...

output:

-1
38621117/206
214354369/1137
-1
-1
-1
41576602/1721
-1
-1
9827803/573
128263937/513
97044383/7
559203/1
-1
8072465/67
774333643/1
554039885/1037
136547497/295
-1
-1
110142397/776
48666785/1741
7410561/2
-1
-1
10863667/3
-1
37738995/463
392359137/1
233929855/1
-1
34584745/1747
436195823/157
1582359...

result:

wrong answer 1st lines differ - expected: '103496945/1514', found: '-1'

Test #11:

score: 0
Wrong Answer
time: 42ms
memory: 11820kb

input:

100000
40496370 324535254 49378557 776806467 392153838 609799230 999070315 9268041 50237542 87633903 878656049 864388090 896320175 129811364 229732659 323726790 766611509 83562791 105404287 304944654 412773020 198917959 443657299 415124223 512809649 256079123 986186956 2116823 811457832 528369347 63...

output:

909844125/74797
44752761/2
138443771/3
-1
581796039/1
57773435/4
1880299/1
389816351/60850
356986812/4679
480772724/30785
359023647/1
-1
496966923/1
373080966/1
2524209/4670
-1
-1
220155017/56091
26635342/10865
1002356/23
8880563/8457
8248996/1
402427421/1
83364995/81099
27162307/4020
50779853/89491...

result:

wrong answer 2nd lines differ - expected: '224068850/13921', found: '44752761/2'

Test #12:

score: 0
Wrong Answer
time: 59ms
memory: 11948kb

input:

100000
747693760 346580867 964846225 969980340 332880584 194631534 209614296 522099709 464443293 921495511 83514635 841823909 900939960 262364081 256636328 411285480 427902137 406071146 24620069 112056879 943629859 862303870 308874724 251885142 471236107 744614688 681975403 212603157 214491071 32304...

output:

-1
118249853/13726
188747735/2
-1
8084821/2769
176853560/12729
84266082/12443
589021845/1
257972110/42497
266273578/3
794770271/18621
21885340/69607
-1
-1
214367135/82708
279048062/1
348241472/1
12309953/490
289791746/75455
233233519/23345
11316602/99271
256611870/1
344991640/72689
57955389/28477
50...

result:

wrong answer 1st lines differ - expected: '78195569/6188', found: '-1'

Test #13:

score: 0
Wrong Answer
time: 46ms
memory: 11900kb

input:

100000
97813451 955958588 478382047 254807140 430405236 587356632 581166185 416641725 298677059 255167178 707387178 995828332 1210912 419588635 497113506 472224137 683979366 992616764 280702107 282745359 832716627 830311108 1931725 972918653 684698510 654423087 966059769 427612303 261873029 35728741...

output:

127919159/46770
-1
64147060/1
212509/21
26291384/7555
16984427/2366
465661/4681
90622325/10837
98729312/73825
2687546/9
67612945/21223
58188198/1
308404938/34405
3893485/11669
407950073/41963
420191104/8629
154809641/33303
-1
-1
595287471/26000
8436608/31
-1
12989093/13010
-1
18877056/1
97911874/156...

result:

wrong answer 2nd lines differ - expected: '266183846/35305', found: '-1'

Test #14:

score: 0
Wrong Answer
time: 57ms
memory: 11836kb

input:

100000
822999751 128955399 431407947 917749297 190297412 252492617 150325783 759881331 829939635 283074122 899601042 159665057 607610844 489768568 320041755 787661887 373639719 710748050 188262807 366602165 732969261 932719233 208477147 358075812 125541209 536067868 278333698 201086482 537247556 180...

output:

-1
51144568/51191
349046335/85638
-1
72903083/19044
329825178/60235
23066327/40173
331847666/1
20187979/4
4759354/8409
10644649/85083
78919891/68507
21842081/2
7392021/25
247872793/2
-1
263576792/1
25821919/28596
130655890/25201
3691553/1
-1
569586507/1
555721319/11599
143351670/1
66061428/1
-1
3286...

result:

wrong answer 1st lines differ - expected: '80903637/10393', found: '-1'