[] A rectangular footprint NOIp2002

link

Data range is very small, considering the search.

My first thought is to enumerate rectangular, obviously each rectangle designated point input must be given.

However, the complexity of not only wrong, but also difficult to write a thief.

Calm analysis, which can not be enumerated rectangle including points, which can be enumerated point rectangle belongs.

There are four cases such that each point, the complexity O (n- . 4 ), there is a constant check, it can be said probe at the edge of a TLE crazy.

In fact, so much dissatisfaction with the situation is to run, add some pruning, can be passed.

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<string>
 4 #include<cstring>
 5 #include<cstdlib>
 6 using namespace std;
 7 
 8 inline int read(){
 9     int x=0;char ch=getchar();
10     while(ch<'0'||ch>'9')ch=getchar();
11     while(ch>='0'&&ch<='9'){
12         x=x*10+ch-'0';ch=getchar();
13     }
14     return x;
15 }
16 
17 const int N=55;
18 
19 struct poi{
20     int x,y;
21 }p[N];
22 
23 struct mtrx{
24     int l,r,u,d;
25     bool fl;
26 }m[5];
27 
28 
29 int n,k;
30 
31 int ans=1e9+7;
32 
33 inline bool ins(int a,int x,int y){
34     if(x<=m[a].r&&x>=m[a].l&&y<=m[a].u&&y>=m[a].d)return true;
35     return false;
36 }//判断(x,y)是否在a矩形中 
37 
38 inline bool check(int a,int b){
39     if(ins(a,m[b].l,m[b].u))return true;
40     if(ins(a,m[b].l,m[b].d))return true;
41     if(ins(a,m[b].r,m[b].u))return true;
42     if(ins(a,m[b].r,m[b].d))return true;
43     return false;
44 }//判断a,b矩形是否相交 
45 
46 inline void dfs(int now){
47     int val=0;
48     for(int i=1;i<=k;++I) {
 49          IF (m [I] .fl) {
 50              for ( int J = I + . 1 ; J <= K; ++ J) {
 51 is                  IF (m [J] .fl && Check (I, J)) return ; // If another rectangle and intersection 
52 is              }
 53 is          }
 54 is          Val + = (m [I] .rm [I] .L) * (m [I] .u- m [I] 2.d);
 55      } // first seeking the answer to the current wave 
56 is      IF (Val> ANS) return ;
 57 is      IF (n-+ == now . 1 ) {
 58          ANS = min (ANS, Val);
 59          return;
 60      }
 61 is      for ( int I = . 1 ; I <= K; ++ I) {
 62 is          MTRX Shep = m [I]; // for restoring the state 
63 is          IF (! M [I] .fl) {  
 64              m [I] .L = m [I] .r = P [now] .x;
 65              m [I] .u = m [I] 2.d = P [now] .y;
 66              m [I] = .fl . 1 ;
 67              DFS (now + . 1 );
 68              m [I] = Shep;
 69              break ;
 70          } // here break because, certainly not bad into empty rectangles other than rectangular.
71         else{
72             m[i].l=min(m[i].l,p[now].x);
73             m[i].r=max(m[i].r,p[now].x);
74             m[i].d=min(m[i].d,p[now].y);
75             m[i].u=max(m[i].u,p[now].y);
76             dfs(now+1);
77             m[i]=shep;
78         }
79     }
80 }
81 
82 int main(){
83     n=read();k=read();
84     for(int i=1;i<=n;++i){
85         p[i].x=read();p[i].y=read();
86     }
87     dfs(1);
88     cout<<ans;
89 }

 

Guess you like

Origin www.cnblogs.com/chiyo/p/11230978.html