This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <stdio.h>
#include <algorithm>
#include <queue>
#include <vector>
#define inf 0x7fffffff
using namespace std;
typedef pair <int,int> ppair;
priority_queue <ppair,vector<ppair>,greater<ppair> > q;
int n, m, a[1001][1001], ans=inf;
void input(void)
{
int i, j;
scanf("%d %d",&n,&m);
for(i=1 ; i<=n ; i++)
{
for(j=1 ; j<=m ; j++)
scanf("%d",&a[i][j]);
sort(a[i]+1,a[i]+m+1);
}
}
void process(void)
{
int i, ptr[1001], mmax=-inf, mmin, minv;
for(i=1 ; i<=n ; i++)
{
ptr[i]=1;
q.push(make_pair(a[i][1],i));
if(mmax<a[i][1])
mmax=a[i][1];
}
while(1)
{
mmin=q.top().first;
minv=q.top().second;
q.pop();
if(ans>mmax-mmin)
ans=mmax-mmin;
ptr[minv]++;
if(ptr[minv]>m)
break;
q.push(make_pair(a[minv][ptr[minv]],minv));
if(mmax<a[minv][ptr[minv]])
mmax=a[minv][ptr[minv]];
}
}
void output(void)
{
printf("%d",ans);
}
int main(void)
{
input();
process();
output();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |