#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair<ll,ll> ii;
typedef vector<int> vi;
typedef unsigned long long ull;
typedef long double ld;
typedef tree<ii, null_type, less<ii>, rb_tree_tag, tree_order_statistics_node_update> pbds;
ii reduce(ii a)
{
ll g=__gcd(a.fi,a.se);
a.fi/=g; a.se/=g;
return a;
}
bool cmp(ii a, ii b)
{
ll s1 = a.fi/a.se; ll s2 = b.fi/b.se;
if(s1!=s2) return s1<s2;
a.fi%=a.se; b.fi%=b.se;
return (a.fi*b.se<=a.se*b.fi);
}
ii add(ii a, ii b)
{
ll s1 = a.fi/a.se; ll s2 = b.fi/b.se;
s1+=s2;
a.fi%=a.se; b.fi%=b.se;
ii c;
if(a.se==b.se) c=mp(a.fi+b.fi,a.se);
else c = mp(a.fi*b.se+a.se*b.fi,a.se*b.se);
c.fi+=s1*c.se;
return c;
}
ll a[2222][2222];
ll S[2222];
ii cutpoint[2222][2222];
bool used[2222];
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
int n,m; cin>>n>>m;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin>>a[i][j];
S[i]+=a[i][j];
}
}
for(int i=0;i<n;i++)
{
int cur=0; ll cur_left =a[i][0]*n;
ii len=mp(0,1);
//search for the first place sum=j
for(int j=0;j<n;j++)
{
ll required = S[i];
while(required>0)
{
if(cur_left<=required)
{
required-=cur_left;
cur_left=0;
len=mp(cur+1,1);
cur++;
if(cur<m) cur_left+=a[i][cur]*n;
}
else
{
cur_left-=required;
len=add(len,mp(required,a[i][cur]*n));
required=0;
}
}
cutpoint[i][j]=len;
assert(len.se<=ll(1e9));
//cerr<<i<<' '<<j<<' '<<cutpoint[i][j].fi<<' '<<cutpoint[i][j].se<<'\n';
}
}
vector<ii> ans;
vi vec;
for(int j=0;j<n;j++)
{
ii minpt = mp(-1,-1); int id=-1;
for(int i=0;i<n;i++)
{
if(used[i]) continue;
if(minpt.fi<0)
{
minpt=cutpoint[i][j]; id=i;
}
else if(cmp(cutpoint[i][j],minpt))
{
minpt=cutpoint[i][j]; id=i;
}
}
vec.pb(id);
ans.pb(minpt);
used[id]=1;
}
for(int i=0;i<n-1;i++)
{
cout<<ans[i].fi<<' '<<ans[i].se<<'\n';
}
for(int i=0;i<n;i++)
{
cout<<vec[i]+1;
if(i+1<n) cout<<' ';
}
cout<<'\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
512 KB |
Output is correct |
4 |
Correct |
2 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
512 KB |
Output is correct |
10 |
Correct |
3 ms |
512 KB |
Output is correct |
11 |
Correct |
2 ms |
512 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
3 ms |
512 KB |
Output is correct |
15 |
Correct |
3 ms |
512 KB |
Output is correct |
16 |
Correct |
3 ms |
512 KB |
Output is correct |
17 |
Correct |
3 ms |
512 KB |
Output is correct |
18 |
Correct |
3 ms |
512 KB |
Output is correct |
19 |
Correct |
3 ms |
512 KB |
Output is correct |
20 |
Correct |
3 ms |
512 KB |
Output is correct |
21 |
Correct |
3 ms |
512 KB |
Output is correct |
22 |
Correct |
3 ms |
512 KB |
Output is correct |
23 |
Correct |
2 ms |
384 KB |
Output is correct |
24 |
Correct |
3 ms |
512 KB |
Output is correct |
25 |
Correct |
3 ms |
384 KB |
Output is correct |
26 |
Correct |
3 ms |
384 KB |
Output is correct |
27 |
Correct |
2 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
512 KB |
Output is correct |
19 |
Correct |
2 ms |
512 KB |
Output is correct |
20 |
Correct |
2 ms |
512 KB |
Output is correct |
21 |
Correct |
2 ms |
384 KB |
Output is correct |
22 |
Correct |
2 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
3 ms |
512 KB |
Output is correct |
25 |
Correct |
3 ms |
512 KB |
Output is correct |
26 |
Correct |
2 ms |
512 KB |
Output is correct |
27 |
Correct |
2 ms |
384 KB |
Output is correct |
28 |
Correct |
3 ms |
384 KB |
Output is correct |
29 |
Correct |
3 ms |
512 KB |
Output is correct |
30 |
Correct |
3 ms |
512 KB |
Output is correct |
31 |
Correct |
3 ms |
512 KB |
Output is correct |
32 |
Correct |
3 ms |
512 KB |
Output is correct |
33 |
Correct |
3 ms |
512 KB |
Output is correct |
34 |
Correct |
3 ms |
512 KB |
Output is correct |
35 |
Correct |
3 ms |
512 KB |
Output is correct |
36 |
Correct |
3 ms |
512 KB |
Output is correct |
37 |
Correct |
3 ms |
512 KB |
Output is correct |
38 |
Correct |
2 ms |
384 KB |
Output is correct |
39 |
Correct |
3 ms |
512 KB |
Output is correct |
40 |
Correct |
3 ms |
384 KB |
Output is correct |
41 |
Correct |
3 ms |
384 KB |
Output is correct |
42 |
Correct |
2 ms |
512 KB |
Output is correct |
43 |
Correct |
49 ms |
13432 KB |
Output is correct |
44 |
Correct |
271 ms |
57336 KB |
Output is correct |
45 |
Correct |
146 ms |
25976 KB |
Output is correct |
46 |
Correct |
28 ms |
4216 KB |
Output is correct |
47 |
Correct |
188 ms |
37772 KB |
Output is correct |
48 |
Correct |
336 ms |
79096 KB |
Output is correct |
49 |
Correct |
86 ms |
24184 KB |
Output is correct |
50 |
Correct |
433 ms |
100216 KB |
Output is correct |
51 |
Correct |
179 ms |
42744 KB |
Output is correct |
52 |
Correct |
411 ms |
86392 KB |
Output is correct |
53 |
Correct |
351 ms |
79480 KB |
Output is correct |
54 |
Correct |
2 ms |
384 KB |
Output is correct |
55 |
Correct |
164 ms |
44024 KB |
Output is correct |
56 |
Correct |
264 ms |
57392 KB |
Output is correct |
57 |
Correct |
210 ms |
49272 KB |
Output is correct |
58 |
Correct |
339 ms |
78212 KB |
Output is correct |
59 |
Correct |
219 ms |
47224 KB |
Output is correct |
60 |
Correct |
616 ms |
104952 KB |
Output is correct |
61 |
Correct |
599 ms |
104824 KB |
Output is correct |
62 |
Correct |
592 ms |
104620 KB |
Output is correct |
63 |
Correct |
602 ms |
104952 KB |
Output is correct |
64 |
Correct |
620 ms |
104952 KB |
Output is correct |
65 |
Correct |
552 ms |
108152 KB |
Output is correct |
66 |
Correct |
543 ms |
108128 KB |
Output is correct |
67 |
Correct |
536 ms |
108280 KB |
Output is correct |
68 |
Correct |
251 ms |
57688 KB |
Output is correct |
69 |
Correct |
266 ms |
54436 KB |
Output is correct |
70 |
Correct |
300 ms |
69072 KB |
Output is correct |
71 |
Correct |
397 ms |
77324 KB |
Output is correct |