HZOJ grouping

He played more than good code.

For the test point, 11: manual simulation.

 1 void QJ1_11()
 2 {
 3     if(n==2)
 4     {
 5         int tk;
 6         if(pd(a[1]+a[2]))tk=2;
 7         else tk=1;
 8         if(tk<=k)    
 9         {
10             puts("1");
11             puts("");
12         }
13         else
14         {
15             puts("2");
16             printf("%d\n",1);
17         }
18         exit(0);
19     }
20 }
View Code

For the test point 2 to 6:

Greedy approach may be used, from the forward sweep, each encountered a number, determining whether the current segment may be added, is not disconnected. Complexity $ n ^ 2 $.

 1 void QJ2_6()
 2 {
 3     if(k==1)
 4     {
 5         if(n==4||n==16||n==256||n==1024)
 6         {
 7             LL l=n,num=0;
 8             for(int i=n;i;i--)
 9             {
10                 for(int j=l;j>i;j--)
11                 if(pd(a[i]+a[j]))
12                 {
13                     num++,ans[++cnt]=i,l=i;break;
14                 }
15             }
16             if(ans[cnt]==0&&cnt!=0){cnt--;}
17             else num++;
18             printf("%lld\n",num);
19             for(int i=cnt;i;i--)
20                 printf("%lld ",ans[i]);
21             exit(0);
22         }
23     }
24 }
View Code

For the test point 7 to 10:

n = 131072, then changing an enumeration method, ah determines whether there is a [j] + a [i] = x ^ 2, an open bucket, only enumerate x (1 ~ 512) to. The complexity of n * √n.

 1 void QJ7_10()
 2 {
 3     if(k==1)
 4     {
 5         if(n==131072)
 6         {
 7             int num=0;
 8             for(int i=n;i;i--)
 9             {
10                 for(int j=512;j&&j*j>a[i];j--)
11                 if(t[j*j-a[i]])
12                 {
13                     num++,ans[++cnt]=i;ma(t);break;
14                 }
15                 t[a[i]]=1;
16             }
17             if(ans[cnt]==0&&cnt!=0){cnt--;}
18             else num++;
19             printf("%d\n",num);
20             for(int i=cnt;i;i--)
21                 printf("%lld ",ans[i]);
22             exit(0);
23         }
24     }
25 }
View Code

For the test points 12 to 25:

The same greedy method, two rabbits even contradictory side, if this time is a bipartite graph, then it must be divided into two groups so that no conflict arises smaller, k = 2, otherwise OFF. Theoretical complexity $ n ^ 2 $, in fact, because the point comparison of water (people may think this is a question $ n ^ 3 $, so there is no card this algorithm), can A out, run pretty fast.

 1 void QJ12_25()
 2 {
 3     if(k==2)
 4 //    if(n==4||n==8||n==16)
 5     {
 6         int num=0,l=n;
 7         for(re int i=n;i;i--)
 8         {
 9             for(re int j=l;j>i;j--)
10             if(pd(a[i]+a[j]))
11                 add(i,j),add(j,i);
12             for(re int j=l;j>=i;j--)co[j]=0;
13             if(!dfs(i,2))
14             {
15                 l=i;num++;ans[++cnt]=i;
16                 for(re int j=l;j>=i;j--)first[j]=0;
17                 num_e=0;
18             }
19         }
20         if(ans[cnt]==0&&cnt!=0){cnt--;}
21         else num++;
22         printf("%d\n",num);
23         for(re int i=cnt;i;i--)
24             printf("%lld ",ans[i]);
25         exit(0);
26     }
27 }
View Code

Solution 2 is also positive for k =:

I have not played this, the first pit.

 

Guess you like

Origin www.cnblogs.com/Al-Ca/p/11296096.html
Recommended