Submission #944690

# Submission time Handle Problem Language Result Execution time Memory
944690 2024-03-13T03:42:08 Z dugersuren Fence (CEOI08_fence) C++14
100 / 100
1 ms 596 KB
#include <bits/stdc++.h>
#define ll long long
#define MAXN 110
using namespace std;
struct TT{
    ll x,y;
};
ll n,m,dist[MAXN];
TT h[MAXN],t[MAXN],p0;
bool in[MAXN];
ll Cross(TT c,TT a,TT b){
    return (a.x-c.x)*(b.y-c.y)-(a.y-c.y)*(b.x-c.x);
}
bool Cmp(TT a,TT b){
    return Cross(p0,a,b)>0;
}
vector<TT> ch;
void Convex_Hull(){
    ll ind=1;
    for(ll i=2;i<=n;i++){ //ind = Rigth Bottom index
        if(h[i].y<h[ind].y || (h[i].y==h[ind].y && h[i].x<h[ind].x))
            ind=i;
    }
    swap(h[1],h[ind]); //h[1] change Right Bottom tree to index=1
    p0=h[1];
    sort(h+2,h+1+n,Cmp);//Order by Angle Ox axe  
    stack<TT> s;
    s.push(h[1]);
    s.push(h[2]);
    s.push(h[3]);
    for(ll i=4;i<=n;i++){
        TT a=s.top(),b;
        s.pop();
        b=s.top();
        s.push(a);
        while(Cross(b,a,h[i])<0){
            s.pop();
            a=s.top();
            s.pop();
            b=s.top();
            s.push(a);
        }
        s.push(h[i]);
    }
    
    while (!s.empty()){
        ch.push_back(s.top());
        s.pop();
    }
    reverse(ch.begin(),ch.end());
}
vector<ll> G[MAXN];
int main(){
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin>>n>>m;
    for(ll i=1;i<=n;i++) cin>>h[i].x>>h[i].y; //Read holes h array
    for(ll i=1;i<=m;i++) cin>>t[i].x>>t[i].y; //Read trees t array
    Convex_Hull(); //create convex holl by h hole's array
    
    
    for(ll i=0;i<ch.size() && ch.size()>2;){//Remove free hole from "Convex holl"
    	TT a=ch[i],b=ch[(i+1)%ch.size()],c=(!i)?ch[ch.size()-1]:ch[i-1];
		bool ok=true; 
    	for(ll j=1;j<=m;j++)
    		if(Cross(a,b,t[j])>0 && Cross(b,c,t[j])>0 && Cross(c,a,t[j])>0){
    			ok=false;
    			break;
			}
		if(ok)ch.erase(ch.begin()+i);
		else i++;
	}
	
	ll ans=111*m,cnt=0;
    if (ch.size()<3){
        cout<<ans;
        return 0;
    }
    
    for(ll i=1;i<=m;i++){ //cnt=count tree in Convex holl
        in[i]=true;
        for(ll j=0;j<ch.size();j++){
            TT a=ch[j],b=t[i],c=(!j)?ch[ch.size()-1]:ch[j-1];
            in[i]=(in[i] && Cross(c,a,b)>0);
        }
        cnt+=in[i];
    }
    

    for(ll i=1;i<=n;i++){
        for(ll j=1;j<=n;j++){
            if (i==j) continue;
            bool yes=true;
            for(ll l=1;l<=m;l++){
                if (in[l] && Cross(h[i],h[j],t[l])<0){
                    yes=false;
                    break;
                }
            }
            if(yes) G[i].push_back(j);
        }
    }
    for(ll i=1;i<=n;i++){
        for(ll j=1;j<=n;j++) dist[j]=LLONG_MAX;
        dist[i]=1;
        ll minn=LLONG_MAX;
        queue<ll> q;
        q.push(i);
        while (!q.empty()){
            ll t=q.front();
            q.pop();
            for (ll next : G[t]){
                if (next==i) minn=min(minn,dist[t]);
                if (dist[t]+1<dist[next]){
                    dist[next]=dist[t]+1;
                    q.push(next);
                }
            }
        }
        if(minn!=LLONG_MAX) ans=min(ans,(m-cnt)*111+minn*20);
    }
    cout << ans;
    return 0;
}

Compilation message

fence.cpp: In function 'int main()':
fence.cpp:61:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<TT>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |     for(ll i=0;i<ch.size() && ch.size()>2;){//Remove free hole from "Convex holl"
      |                ~^~~~~~~~~~
fence.cpp:64:6: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   64 |      for(ll j=1;j<=m;j++)
      |      ^~~
fence.cpp:69:3: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   69 |   if(ok)ch.erase(ch.begin()+i);
      |   ^~
fence.cpp:81:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<TT>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   81 |         for(ll j=0;j<ch.size();j++){
      |                    ~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct