#include <bits/stdc++.h>
#define gibon ios::sync_with_stdio(false); cin.tie(0);
using namespace std;
#define fir first
#define sec second
#define ll long long
#define pii pair<int, int>
const int mxN=2020;
typedef struct frac{
int ja, mo;
}frac;
int N, L;
int V[mxN][mxN];
bool Chk[mxN];
vector <vector<int>> v;
vector <int> tmp;
frac ans[mxN];
int sum[mxN];
frac gop(frac a, frac b)
{
frac res={a.ja*b.ja, a.mo*b.mo};
if(res.mo<0) res.mo*=-1, res.ja*=-1;
return res;
}
frac mnus(frac a, frac b)
{
frac res={a.ja*b.mo-a.mo*b.ja, a.mo*b.mo};
if(res.mo<0) res.mo*=-1, res.ja*=-1;
return res;
}
frac pls(frac a, frac b)
{
frac res={a.ja*b.mo+a.mo*b.ja, a.mo*b.mo};
if(res.mo<0) res.mo*=-1, res.ja*=-1;
return res;
}
bool cmp(frac a, frac b)
{
return mnus(a, b).ja<0;
}
bool ok(int idx, int pos, int cur)
{
if(pos==N)
{
for(int i=0;i<N-1;i++) cout << ans[i].ja << " " << ans[i].mo << '\n';
for(int i=0;i<N;i++) cout << v[idx][i] << " ";
return true;
}
frac now;
if(pos==0) now={0, 1};
else now=ans[pos-1];
int ncur=cur;
frac tot={sum[v[idx][pos]], N};
while(ncur!=L && cmp(gop(mnus({ncur, 1}, now), {V[v[idx][pos]][ncur], 1}), tot))
{
//printf("len=%d, %d\n", gop(mnus({ncur, 1}, now), {V[v[idx][pos]][ncur], 1}).ja, gop(mnus({ncur, 1}, now), {V[v[idx][pos]][ncur], 1}).mo);
tot=mnus(tot, gop(mnus({ncur, 1}, now), {V[v[idx][pos]][ncur], 1}));
now={ncur, 1};
ncur++;
}
if(cmp(gop(mnus({ncur, 1}, now), {V[v[idx][pos]][ncur], 1}), tot)) return false;
else
{
ans[pos]=pls(now, {tot.ja, tot.mo*V[v[idx][pos]][ncur]});
//printf("ans[%d]=%d, %d\n", pos, ans[pos].ja, ans[pos].mo);
if(ok(idx, pos+1, ncur)) return true;
}
return false;
}
void jag(int pos)
{
if(pos==N+1)
{
v.push_back(tmp);
return;
}
for(int i=1;i<=N;i++)
{
if(Chk[i]) continue;
Chk[i]=true;
tmp.push_back(i);
jag(pos+1);
tmp.pop_back();
Chk[i]=false;
}
}
int main()
{
gibon
cin >> N >> L;
for(int i=1;i<=N;i++) for(int j=1;j<=L;j++) cin >> V[i][j];
for(int i=1;i<=N;i++) for(int j=1;j<=L;j++) sum[i]+=V[i][j];
jag(1);
ans[0]={0, 1};
for(int i=0;i<v.size();i++)
{
//printf("i=%d\n", i);
//for(int j=0;j<N;j++) printf("%d ", v[i][j]);
//printf("\n");
if(ok(i, 0, 1)) return 0;
}
cout << -1;
}
Compilation message
naan.cpp: In function 'int main()':
naan.cpp:96:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
96 | for(int i=0;i<v.size();i++)
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Incorrect |
1 ms |
492 KB |
X_i is not increasing |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Incorrect |
1 ms |
492 KB |
X_i is not increasing |
19 |
Halted |
0 ms |
0 KB |
- |