#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<int>s(n);for(int i=0;n>i;++i)for(int j:v[i])s[i]+=j;
vector<pair<int,int>>t(n-1);
frac cur(0);
for(int cyc=0;n>cyc+1;++cyc){
frac nxt(l);
int cind,c=cur.a/cur.b;
frac cc((c+1)*cur.b-cur.a,cur.b);
for(int i=0;n>i;++i)if(!was[i]){
if(cc.a*n*v[i][c]>=cc.b*s[i]){
frac tmp=frac(s[i],v[i][c]*n);
if(tmp<nxt){nxt=tmp;cind=i;}
}
else{
ll cb=cur.b;
ll cl=s[i]*cur.b;
ll ck=cc.a*v[i][c];
int j=c+1;
while(l>j&&(ck+v[i][j]*cb)*n<cl){ck+=v[i][j]*cb;++j;}
if(j==l)continue;
ll tt=cl-ck*n;
frac tmp=frac(tt,n*cb*v[i][j])+cc+frac(j-c-1);
if(tmp<nxt){nxt=tmp;cind=i;}
}
}
cur=cur+nxt;
was[cind]=1;
p[cyc]=1+cind;
t[cyc]={cur.a,cur.b};
}
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;
}
Compilation message
naan.cpp: In function 'std::pair<std::vector<std::pair<int, int> >, std::vector<int> > solve(int, int, std::vector<std::vector<int> >)':
naan.cpp:49:17: warning: 'cind' may be used uninitialized in this function [-Wmaybe-uninitialized]
49 | p[cyc]=1+cind;
| ~^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 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 |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
472 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 |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 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 |
344 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 |
468 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 |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 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 |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
472 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 |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 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 |
492 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
460 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 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 |
344 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 |
468 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 |
1 ms |
348 KB |
Output is correct |
42 |
Correct |
1 ms |
464 KB |
Output is correct |
43 |
Runtime error |
19 ms |
4184 KB |
Execution killed with signal 8 |
44 |
Halted |
0 ms |
0 KB |
- |