#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
struct frac
{
long long first,second;
frac()
{
}
frac(long long f,long long s)
{
first=f;
second=s;
}
bool operator<(const frac &f) const
{
return (first*f.second<second*f.first);
}
bool operator<=(const frac &f) const
{
return (first*f.second<=second*f.first);
}
};
int gcd(int a,int b)
{
if (!a || !b)
return (a^b);
return __gcd(a,b);
}
frac norm(frac a)
{
int g=gcd(a.first,a.second);
a.first/=g;
a.second/=g;
return a;
}
frac operator+(frac a,frac b)
{
return norm(frac(a.first*b.second+b.first*a.second,a.second*b.second));
}
frac operator-(frac a,frac b)
{
b.first*=-1;
return a+b;
}
frac operator*(frac a,frac b)
{
return norm(frac(a.first*b.first,a.second*b.second));
}
frac operator/(frac a,frac b)
{
swap(b.first,b.second);
return a*b;
}
int floor(frac a)
{
return a.first/a.second;
}
int ceil(frac a)
{
return (a.first+a.second-1)/a.second;
}
frac sp[2005];
int n,l,mat[2005][2005],p[2005],L[2005],R[2005];
long long sum[2005];
bool ok[2005][2005],vis[2005];
bool match(int node)
{
if (vis[node])
return 0;
vis[node]=1;
for (int i=0;i<n;i++)
{
if (ok[node][i] && L[i]==-1)
{
L[i]=node;
R[node]=i;
return 1;
}
}
for (int i=0;i<n;i++)
{
if (ok[node][i] && match(L[i]))
{
L[i]=node;
R[node]=i;
return 1;
}
}
return 0;
}
int main()
{
scanf("%d%d",&n,&l);
for (int i=0;i<n;i++)
{
for (int j=1;j<=l;j++)
{
scanf("%d",&mat[i][j]);
sum[j]+=mat[i][j];
}
}
for (int i=1;i<=l;i++)
sum[i]+=sum[i-1];
sp[0]=frac(0,1);
for (int i=1;i<n;i++)
{
for (int j=1;j<=l;j++)
{
if (frac(i,n)<frac(sum[j],sum[l]))
{
sp[i]=(norm(frac(i*sum[l],n))-frac(sum[j-1],1))*frac(1,sum[j]-sum[j-1])+frac(j-1,1);
break;
}
}
printf("%lld %lld\n",sp[i].first,sp[i].second);
}
sp[n]=frac(l,1);
for (int i=0;i<n;i++)
{
p[i]=i;
frac tot(0,1);
for (int j=1;j<=l;j++)
tot=tot+frac(mat[i][j],1);
for (int j=0;j<n;j++)
{
int l=ceil(sp[j]),r=floor(sp[j+1]);
frac hap(0,1);
if (l<=r)
{
hap=hap+(frac(l,1)-sp[j])*frac(mat[i][l],1);
hap=hap+(sp[j+1]-frac(r,1))*frac(mat[i][r+1],1);
for (int k=l+1;k<=r;k++)
hap=hap+frac(mat[i][k],1);
}
else
hap=frac(mat[i][l],1)*(sp[j+1]-sp[j]);
if (frac(1,n)<=hap/tot)
ok[i][j]=1;
}
}
memset(L,-1,sizeof(L));
memset(R,-1,sizeof(R));
int ok=1;
while (ok--)
{
memset(vis,0,sizeof(vis));
for (int i=0;i<n;i++)
{
if (R[i]==-1 && match(i))
ok=1;
}
}
for (int i=0;i<n;i++)
printf("%d ",L[i]+1);
}
Compilation message
naan.cpp: In function 'int main()':
naan.cpp:96:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&n,&l);
~~~~~^~~~~~~~~~~~~~
naan.cpp:101:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&mat[i][j]);
~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Incorrect |
3 ms |
376 KB |
Integer parameter [name=P_i] equals to 0, violates the range [1, 2] |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
376 KB |
Output is correct |
4 |
Correct |
4 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
376 KB |
Output is correct |
6 |
Correct |
3 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
4 ms |
376 KB |
Output is correct |
10 |
Correct |
4 ms |
508 KB |
Output is correct |
11 |
Correct |
3 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
3 ms |
376 KB |
Output is correct |
14 |
Correct |
4 ms |
504 KB |
Output is correct |
15 |
Correct |
4 ms |
504 KB |
Output is correct |
16 |
Correct |
4 ms |
376 KB |
Output is correct |
17 |
Correct |
4 ms |
504 KB |
Output is correct |
18 |
Correct |
4 ms |
376 KB |
Output is correct |
19 |
Correct |
4 ms |
376 KB |
Output is correct |
20 |
Correct |
4 ms |
376 KB |
Output is correct |
21 |
Correct |
4 ms |
504 KB |
Output is correct |
22 |
Correct |
4 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
3 ms |
504 KB |
Output is correct |
25 |
Correct |
3 ms |
376 KB |
Output is correct |
26 |
Correct |
3 ms |
376 KB |
Output is correct |
27 |
Correct |
3 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Incorrect |
3 ms |
376 KB |
Integer parameter [name=P_i] equals to 0, violates the range [1, 2] |
3 |
Halted |
0 ms |
0 KB |
- |