Submission #873354

# Submission time Handle Problem Language Result Execution time Memory
873354 2023-11-14T22:26:09 Z ofrankel Naan (JOI19_naan) C++17
29 / 100
24 ms 14172 KB
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int n,l;
vector<vector<int>>v;
ll gc(ll a,ll b){
    if(!b)return a;
    return gc(b,a%b);
}
class frac{
public:
    ll a,b;
    frac(ll _a,ll _b):a(_a),b(_b){reduce();}
    frac(ll x):a(x),b(1){}
    void reduce(){if(int g=gc(a,b)){a/=g;b/=g;}}
    bool operator <(const frac& other){return a*other.b<other.a*b;}
    frac operator +( const frac& other){return frac(a*other.b+b*other.a,b*other.b);}
};
pair<vector<pair<int,int>>,vector<int>> solve(int N,int L,vector<vector<int>>V){
    n=N;l=L;v=move(V);
    vector<int>was(n);
    vector<int>p(n);
    vector<pair<int,int>>t(n-1);
    vector<ll>s(n);for(int i=0;n>i;++i)for(int j:v[i])s[i]+=j;
    vector<vector<frac>>g(n,vector<frac>(n-1,l));
    for(int i=0;n>i;++i){
        frac cur(0);
        for(int j=0;n>j+1;++j){
            int c=cur.a/cur.b;
            frac cc((c+1)*cur.b-cur.a,cur.b);
            if(cc.a*n*v[i][c]>=cc.b*s[i]){
                cur=cur+frac(s[i],v[i][c]*n);
                g[i][j]=cur;
            }
            else{
                ll cb=cur.b;
                ll cl=s[i]*cur.b;
                ll ck=cc.a*v[i][c];
                int j1=c+1;
                while(l>j1&&(ck+v[i][j1]*cb)*n<cl){ck+=v[i][j1]*cb;++j1;}
                if(j1==l)continue;
                ll tt=cl-ck*n;
                frac tmp=frac(tt,n*cb*v[i][j1])+cc+frac(j1-c-1);
                cur=cur+tmp;
                g[i][j]=cur;
            }
        }
            
    }
    for(int i=0;n>i+1;++i){frac cur(l);int ind=0;for(int j=0;n>j;++j)if(!was[j]&&g[j][i]<cur){cur=g[j][i];ind=j;}t[i]={cur.a,cur.b};was[ind]=1;p[i]=ind+1;}
    for(int i=0;n>i;++i)if(!was[i])p[n-1]=i+1;
    return {t,p};
}


int main()
{
    std::ios::sync_with_stdio(0);cin.tie(0);
    int N,L;cin>>N>>L;
    vector<vector<int>>V(N,vector<int>(L));
    for(auto&i:V)for(auto&j:i)cin>>j;
    auto [t,p]=solve(N,L,move(V));
    if(t.size()==0&&p.size()==0)
        cout<<-1<<endl;
    else{
        for(auto [a,b]:t)
            cout<<a<<" "<<b<<"\n";
        for(int i:p)
            cout<<i<<" ";
        cout<<"\n";
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 504 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 456 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 520 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 504 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 0 ms 456 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 520 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 1 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 1 ms 348 KB Output is correct
40 Correct 1 ms 348 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Runtime error 24 ms 14172 KB Execution killed with signal 8
44 Halted 0 ms 0 KB -