답안 #321691

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
321691 2020-11-13T03:47:07 Z daniel920712 The Kingdom of JOIOI (JOI17_joioi) C++14
100 / 100
1803 ms 67568 KB
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>

using namespace std;
int all[2005][2005];
int l1[2005],r1[2005];
int l2[2005],r2[2005];
vector < pair < int ,  pair < int , int > > > where;


int main()
{
    int small=2e9,big=0,l,r,N,M,ok,x,y,a,b,c,d,i,j,k;
    scanf("%d %d",&N,&M);
    for(i=0;i<N;i++)
    {
        for(j=0;j<M;j++)
        {
            scanf("%d",&all[i][j]);
            where.push_back(make_pair(all[i][j],make_pair(i,j)));
            small=min(small,all[i][j]);
            big=max(big,all[i][j]);
        }
    }
    l=0;
    r=(big-small+1);
    while((r-l)>1)
    {
        ok=1;
        for(auto i:where)
        {
            if(i.first>small+(r+l)/2) if(big-i.first>(r+l)/2) ok=0;
            if(i.first<big-(r+l)/2) if(i.first-small>(r+l)/2) ok=0;
        }
        if(ok==0)
        {
            l=(r+l)/2;
            continue;
        }
        for(i=0;i<N;i++)
        {
            l1[i]=l2[i]=M;
            r1[i]=r2[i]=-1;
        }
        for(auto i:where)
        {
            if(i.first>small+(r+l)/2)
            {
                l1[i.second.first]=min(l1[i.second.first],i.second.second);
                r1[i.second.first]=max(r1[i.second.first],i.second.second);
            }
            if(i.first<big-(r+l)/2)
            {
                l2[i.second.first]=min(l2[i.second.first],i.second.second);
                r2[i.second.first]=max(r2[i.second.first],i.second.second);
            }


        }
        a=b=c=d=1;
        x=M;
        for(i=0;i<N;i++)
        {
            x=min(x,l1[i]);
            if(x<=r2[i]) a=0;
        }
        x=-1;
        for(i=0;i<N;i++)
        {
            x=max(x,r2[i]);
            if(x>=l1[i]) b=0;
        }
        x=M;
        for(i=0;i<N;i++)
        {
            x=min(x,l2[i]);
            if(x<=r1[i]) c=0;
        }
        x=-1;
        for(i=0;i<N;i++)
        {
            x=max(x,r1[i]);
            if(x>=l2[i]) d=0;
        }
        if(a||b||c||d) r=(l+r)/2;
        else l=(r+l)/2;
    }
    ok=1;
    for(auto i:where)
    {
        if(i.first>small+l) if(big-i.first>l) ok=0;
        if(i.first<big-l) if(i.first-small>l) ok=0;
    }
    if(ok==0)
    {
        printf("%d\n",r);
        return 0;
    }
    for(i=0;i<N;i++)
    {
        l1[i]=l2[i]=M;
        r1[i]=r2[i]=-1;
    }
    for(auto i:where)
    {
        if(i.first>small+l)
        {
            l1[i.second.first]=min(l1[i.second.first],i.second.second);
            r1[i.second.first]=max(r1[i.second.first],i.second.second);
        }
        if(i.first<big-l)
        {
            l2[i.second.first]=min(l2[i.second.first],i.second.second);
            r2[i.second.first]=max(r2[i.second.first],i.second.second);
        }


    }

    a=b=c=d=1;

    x=M;
    for(i=0;i<N;i++)
    {
        x=min(x,l1[i]);
        if(x<=r2[i]) a=0;
    }

    x=-1;
    for(i=0;i<N;i++)
    {
        x=max(x,r2[i]);
        if(x>=l1[i]) b=0;
    }

    x=M;
    for(i=0;i<N;i++)
    {
        x=min(x,l2[i]);
        if(x<=r1[i]) c=0;
    }

    x=-1;
    for(i=0;i<N;i++)
    {
        x=max(x,r1[i]);
        if(x>=l2[i]) d=0;
    }

    if(a||b||c||d) printf("%d\n",l);
    else printf("%d\n",r);



    return 0;
}

Compilation message

joioi.cpp: In function 'int main()':
joioi.cpp:18:38: warning: unused variable 'y' [-Wunused-variable]
   18 |     int small=2e9,big=0,l,r,N,M,ok,x,y,a,b,c,d,i,j,k;
      |                                      ^
joioi.cpp:18:52: warning: unused variable 'k' [-Wunused-variable]
   18 |     int small=2e9,big=0,l,r,N,M,ok,x,y,a,b,c,d,i,j,k;
      |                                                    ^
joioi.cpp:19:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   19 |     scanf("%d %d",&N,&M);
      |     ~~~~~^~~~~~~~~~~~~~~
joioi.cpp:24:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   24 |             scanf("%d",&all[i][j]);
      |             ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 384 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 364 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
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 384 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 364 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 492 KB Output is correct
16 Correct 7 ms 2024 KB Output is correct
17 Correct 11 ms 2024 KB Output is correct
18 Correct 9 ms 2024 KB Output is correct
19 Correct 14 ms 2024 KB Output is correct
20 Correct 13 ms 2024 KB Output is correct
21 Correct 13 ms 2024 KB Output is correct
22 Correct 12 ms 2024 KB Output is correct
23 Correct 20 ms 2024 KB Output is correct
24 Correct 10 ms 2024 KB Output is correct
25 Correct 23 ms 2152 KB Output is correct
26 Correct 13 ms 2024 KB Output is correct
27 Correct 13 ms 2024 KB Output is correct
28 Correct 13 ms 2024 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 Correct 1 ms 384 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 364 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 492 KB Output is correct
16 Correct 7 ms 2024 KB Output is correct
17 Correct 11 ms 2024 KB Output is correct
18 Correct 9 ms 2024 KB Output is correct
19 Correct 14 ms 2024 KB Output is correct
20 Correct 13 ms 2024 KB Output is correct
21 Correct 13 ms 2024 KB Output is correct
22 Correct 12 ms 2024 KB Output is correct
23 Correct 20 ms 2024 KB Output is correct
24 Correct 10 ms 2024 KB Output is correct
25 Correct 23 ms 2152 KB Output is correct
26 Correct 13 ms 2024 KB Output is correct
27 Correct 13 ms 2024 KB Output is correct
28 Correct 13 ms 2024 KB Output is correct
29 Correct 965 ms 60144 KB Output is correct
30 Correct 1076 ms 62064 KB Output is correct
31 Correct 1381 ms 65604 KB Output is correct
32 Correct 905 ms 65668 KB Output is correct
33 Correct 695 ms 60288 KB Output is correct
34 Correct 1320 ms 66036 KB Output is correct
35 Correct 1207 ms 67548 KB Output is correct
36 Correct 1006 ms 62756 KB Output is correct
37 Correct 1803 ms 67568 KB Output is correct