Submission #1072989

# Submission time Handle Problem Language Result Execution time Memory
1072989 2024-08-24T08:02:35 Z phong Naan (JOI19_naan) C++17
100 / 100
2871 ms 148152 KB
#include<bits/stdc++.h>

#define ll long long
#define int long long
const int nmax = 2e3 + 5, N = 1e5;
const ll oo = 1e12 + 1, base = 311;
const int lg = 19, M = 10;
const ll mod = 998244353, mod2 = 1e9 + 5277;
#define pii pair<int, int>
#define fi first
#define se second
#define endl "\n"
#define debug(a, n) for(int i = 1; i <= n; ++i) cout << a[i] << ' '; cout << "\n";
using namespace std;

int n, L;
int a[nmax][nmax];
ll s[nmax][nmax];
struct node{
    int x, y, id;
};
ll sum[nmax];
bool vis[nmax];
pii apply(pii tx){
    if(tx.se == 0) return tx;
    int lc = __gcd(tx.fi, tx.se);
    return {tx.fi / lc, tx.se / lc};
}

bool check(pii t1, pii t2){
    t1 = apply(t1);
    t2 = apply(t2);
    if(t1.fi == 0) return 1;
    if(t2.fi == 0) return 0;

   if(t1.fi / t1.se != t2.fi / t2.se) return t1.fi / t1.se < t2.fi / t2.se;
    return (t1.fi % t1.se) * t2.se <= (t2.fi%t2.se) * t1.se;
}
ll calc(int l, int r, int i){
    if(l > r) return 0;
    return s[i][r]  - s[i][l - 1];
}
pii get(pii t1, pii t2, int idx){
    if(t1.fi == 0) return t2;
    if(t2.fi == 0) return t1;
    pii tx ={t1.fi * t2.se + (t1.se * t2.fi * idx), t1.se * t2.se};
    tx = apply(tx);
    return tx;
}
pii c[nmax][nmax];
bool okla(pii t1, pii t2){
    t1 = apply(t1);
    t2 = apply(t2);
    return t1 == t2;
}
main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
//    freopen("code.inp", "r", stdin);
//    freopen("code.out", "w", stdout);
    cin >> n >> L;
    for(int i = 1; i <= n; ++i){
        for(int j = 1; j <= L; ++j){
            cin >> a[i][j];
            sum[i] += a[i][j];
            s[i][j] = s[i][j - 1] + a[i][j];
            c[i][j] = {-1, -1};
        }
    }
    pii pos = {0, 0};
    vector<int> tmp;
    int last = 0;
    for(int i = 1; i <= n; ++i){
        last = 0;
        for(int j = 1; j <= n; ++j){
            int l =0, r = L, t1 = -1,t2 = -1;
            l = 0, r = L + 1;
            pii dx;
            while(l <= r){
                int mid = r + l >> 1;
                pii tx = {0, 0};
                tx = get(tx, {calc(1, mid, i), 1}, 1);
                if(check(tx, {sum[i] * j, n})){
                    l = mid + 1;
                    t2 = mid;
                    dx = tx;
                }
                else r = mid - 1;
            }
            if(t2 == -1) break;
            pii one = get({sum[i] * j, n}, dx, -1);
            if(a[i][t2 + 1]) one.se *= a[i][t2 + 1];
            pii tx = one;
            tx = get(tx, {t2, 1}, 1);
            c[i][j] = pos = tx;
        }
    }
     pos = {0, 0};
    for(int j = 1; j <= n; ++j){
        int idx = -1;
        pii ma;
        for(int i = 1; i <= n; ++i){
            if(vis[i] || c[i][j].fi ==-1) continue;
            if(idx == -1 || check(c[i][j], ma) && check(pos, c[i][j]) && !okla(pos, c[i][j])){
                ma = c[i][j];
                idx = i;
            }
        }
        if(idx == -1) cout << -1, exit(0);
        vis[idx] = 1;
        if(j < n) cout << ma.fi << ' ' << ma.se << endl;
        pos = ma;
        tmp.push_back(idx);
    }

    for(auto p : tmp) cout << p << ' ';



}
/*




*/

Compilation message

naan.cpp:56:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   56 | main(){
      | ^~~~
naan.cpp: In function 'int main()':
naan.cpp:80:29: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   80 |                 int mid = r + l >> 1;
      |                           ~~^~~
naan.cpp:76:30: warning: unused variable 't1' [-Wunused-variable]
   76 |             int l =0, r = L, t1 = -1,t2 = -1;
      |                              ^~
naan.cpp:104:71: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
  104 |             if(idx == -1 || check(c[i][j], ma) && check(pos, c[i][j]) && !okla(pos, c[i][j])){
      |                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
naan.cpp:72:9: warning: variable 'last' set but not used [-Wunused-but-set-variable]
   72 |     int last = 0;
      |         ^~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 476 KB Output is correct
8 Correct 0 ms 604 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 604 KB Output is correct
11 Correct 1 ms 860 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 0 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 0 ms 604 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 604 KB Output is correct
9 Correct 1 ms 860 KB Output is correct
10 Correct 1 ms 860 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 604 KB Output is correct
14 Correct 1 ms 860 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 1 ms 860 KB Output is correct
17 Correct 1 ms 824 KB Output is correct
18 Correct 1 ms 860 KB Output is correct
19 Correct 1 ms 860 KB Output is correct
20 Correct 1 ms 760 KB Output is correct
21 Correct 1 ms 860 KB Output is correct
22 Correct 1 ms 860 KB Output is correct
23 Correct 0 ms 344 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 1 ms 604 KB Output is correct
27 Correct 1 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 476 KB Output is correct
8 Correct 0 ms 604 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 604 KB Output is correct
11 Correct 1 ms 860 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 0 ms 604 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 604 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 1 ms 860 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 0 ms 604 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 604 KB Output is correct
24 Correct 1 ms 860 KB Output is correct
25 Correct 1 ms 860 KB Output is correct
26 Correct 1 ms 604 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 604 KB Output is correct
29 Correct 1 ms 860 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 1 ms 860 KB Output is correct
32 Correct 1 ms 824 KB Output is correct
33 Correct 1 ms 860 KB Output is correct
34 Correct 1 ms 860 KB Output is correct
35 Correct 1 ms 760 KB Output is correct
36 Correct 1 ms 860 KB Output is correct
37 Correct 1 ms 860 KB Output is correct
38 Correct 0 ms 344 KB Output is correct
39 Correct 1 ms 604 KB Output is correct
40 Correct 1 ms 604 KB Output is correct
41 Correct 1 ms 604 KB Output is correct
42 Correct 1 ms 600 KB Output is correct
43 Correct 191 ms 17688 KB Output is correct
44 Correct 1222 ms 80724 KB Output is correct
45 Correct 347 ms 51236 KB Output is correct
46 Correct 19 ms 8792 KB Output is correct
47 Correct 694 ms 64480 KB Output is correct
48 Correct 1806 ms 89680 KB Output is correct
49 Correct 367 ms 31572 KB Output is correct
50 Correct 2272 ms 123476 KB Output is correct
51 Correct 719 ms 67928 KB Output is correct
52 Correct 1843 ms 115020 KB Output is correct
53 Correct 1747 ms 105300 KB Output is correct
54 Correct 1 ms 600 KB Output is correct
55 Correct 749 ms 50768 KB Output is correct
56 Correct 975 ms 84860 KB Output is correct
57 Correct 816 ms 70992 KB Output is correct
58 Correct 1498 ms 100948 KB Output is correct
59 Correct 777 ms 74824 KB Output is correct
60 Correct 2496 ms 147928 KB Output is correct
61 Correct 2667 ms 147864 KB Output is correct
62 Correct 2871 ms 147536 KB Output is correct
63 Correct 2849 ms 148152 KB Output is correct
64 Correct 2428 ms 147932 KB Output is correct
65 Correct 2716 ms 134624 KB Output is correct
66 Correct 2562 ms 134688 KB Output is correct
67 Correct 2339 ms 134648 KB Output is correct
68 Correct 1231 ms 78720 KB Output is correct
69 Correct 1022 ms 85784 KB Output is correct
70 Correct 1573 ms 92480 KB Output is correct
71 Correct 1724 ms 111280 KB Output is correct