Submission #845269

#TimeUsernameProblemLanguageResultExecution timeMemory
845269simona1230T-Covering (eJOI19_covering)C++17
70 / 100
1029 ms79052 KiB
#include <bits/stdc++.h>

using namespace std;
vector<short int> a[1000000];
vector<int> used[1000000];
int n,m,k;
struct edge
{
    short int x,y;
    edge(){}
    edge(short int _x,short int _y)
    {
        x=_x;
        y=_y;
    }
    edge operator+(edge e)
    {
        return {e.x+x,e.y+y};
    }
};
edge pos[4]={{-1,0},{1,0},{0,-1},{0,1}};
edge p[1000000];
vector<int> v[1000000];
bool in_range(edge p)
{
    return p.x>=0&&p.x<n&&p.y>=0&&p.y<m;
}
void read()
{
    cin>>n>>m;
    for(int i=0;i<n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            short int x;
            cin>>x;
            a[i].push_back(x);
            used[i].push_back(0);
        }
    }
    cin>>k;
    for(short int i=1;i<=k;i++)
    {
        cin>>p[i].x>>p[i].y;
        used[p[i].x][p[i].y]=i*10;
        for(int x=-2;x<=2;x++)
        {
            for(int y=-2;y<=2;y++)
            {
                if(x==0&&y==0||x==2&&y!=0||x==-2&&y!=0||y==2&&x!=0||y==-2&&x!=0)continue;
                edge nb={p[i].x+x,p[i].y+y};
                if(in_range(nb)&&used[nb.x][nb.y])
                {
                    v[i].push_back(used[nb.x][nb.y]/10);
                    v[used[nb.x][nb.y]/10].push_back(i);
                }
            }
        }
    }
}
short int minn;
int sum,curr,cnt;
void use(edge e)
{
    for(int i=0;i<4;i++)
    {
        edge nb=pos[i]+e;
        if(in_range(nb)&&!used[nb.x][nb.y])
        {
            used[nb.x][nb.y]=1;
            if(minn>a[nb.x][nb.y])minn=a[nb.x][nb.y];
            sum+=a[nb.x][nb.y];
            cnt++;
        }
    }
}
void dfs(int num)
{
    edge e=p[num];
    //cout<<e.x<<" "<<e.y<<endl;
    cnt++;
    curr++;
    sum+=a[e.x][e.y];
    used[e.x][e.y]+=1;
    use(e);
    for(short int i=0;i<v[num].size();i++)
    {
        int nb=v[num][i];
        if(in_range(p[nb])&&!(used[p[nb].x][p[nb].y]%10))
        {
            dfs(nb);
        }
    }
}
void solve()
{
    int ans=0;
    for(int i=1;i<=k;i++)
    {
        if(!(used[p[i].x][p[i].y]%10))
        {
            //cout<<p[i].x<<" "<<p[i].y<<endl;
            cnt=0;
            minn=1e3;
            curr=0;
            sum=0;
            dfs(i);
            //cout<<endl;
            //cout<<cnt<<" "<<curr<<" "<<sum<<" "<<minn<<endl;
            if(cnt<curr*4)
            {
                cout<<"No"<<'\n';
                exit(0);
            }
            if(cnt==curr*4)
            {
                ans+=sum;
            }
            if(cnt==curr*4+1)
            {
                ans+=sum;
                ans-=minn;
            }
        }
    }
    cout<<ans<<'\n';
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	read();
	solve();
	return 0;
}

Compilation message (stderr)

covering.cpp: In member function 'edge edge::operator+(edge)':
covering.cpp:18:20: warning: narrowing conversion of '(((int)e.edge::x) + ((int)((edge*)this)->edge::x))' from 'int' to 'short int' [-Wnarrowing]
   18 |         return {e.x+x,e.y+y};
      |                 ~~~^~
covering.cpp:18:26: warning: narrowing conversion of '(((int)e.edge::y) + ((int)((edge*)this)->edge::y))' from 'int' to 'short int' [-Wnarrowing]
   18 |         return {e.x+x,e.y+y};
      |                       ~~~^~
covering.cpp: In function 'void read()':
covering.cpp:50:24: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   50 |                 if(x==0&&y==0||x==2&&y!=0||x==-2&&y!=0||y==2&&x!=0||y==-2&&x!=0)continue;
      |                    ~~~~^~~~~~
covering.cpp:50:49: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   50 |                 if(x==0&&y==0||x==2&&y!=0||x==-2&&y!=0||y==2&&x!=0||y==-2&&x!=0)continue;
      |                                            ~~~~~^~~~~~
covering.cpp:50:61: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   50 |                 if(x==0&&y==0||x==2&&y!=0||x==-2&&y!=0||y==2&&x!=0||y==-2&&x!=0)continue;
      |                                                         ~~~~^~~~~~
covering.cpp:50:74: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   50 |                 if(x==0&&y==0||x==2&&y!=0||x==-2&&y!=0||y==2&&x!=0||y==-2&&x!=0)continue;
      |                                                                     ~~~~~^~~~~~
covering.cpp:51:32: warning: narrowing conversion of '(((int)p[((int)i)].edge::x) + x)' from 'int' to 'short int' [-Wnarrowing]
   51 |                 edge nb={p[i].x+x,p[i].y+y};
      |                          ~~~~~~^~
covering.cpp:51:41: warning: narrowing conversion of '(((int)p[((int)i)].edge::y) + y)' from 'int' to 'short int' [-Wnarrowing]
   51 |                 edge nb={p[i].x+x,p[i].y+y};
      |                                   ~~~~~~^~
covering.cpp: In function 'void dfs(int)':
covering.cpp:86:24: warning: comparison of integer expressions of different signedness: 'short int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   86 |     for(short int i=0;i<v[num].size();i++)
      |                       ~^~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...