HDU 5355 Cake (construction + backtracking)

Source: Internet
Author: User

Test instructions

Give a cake, cut into 1~n size n blocks, and ask if you can piece together the M equal without continuing the cut.

Analytical:

First you can find out the sum of these cakes n ( n + 1 ) / 2 , if the sum sum%m! = 0 Then would not be able to be flattened into M-parts, then output "NO".
Next calculate the average avg =sum/m , if the average AVG < N, the cake is impossible to run out of, also output "NO".
The rest of the situation cake is bound to be able to spell "YES", then these cakes can be 2*m as a unit of a group of distribution, everyone take the current group of the largest and smallest, the second and small ....
Until we get the rest. < Span class= "Mrow" id= "mathjax-span-245" > [ 0 4 m Between the cakes is a stop.
At this time of the brute force solution, the problem becomes 1~n number, can spell out, and the same m number, here Direct violence Search DFS.

Summarize:

This problem special judge in the game, so a lot of WA's code is also AC. Later the data is strengthened, and the following code is code that can be enhanced with data.

y code

#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#define PB Push_backusing namespace STD;typedef Long LongllConst intN =1e5+Ten; vector<int>ans[ the];intsumv[ the], cake[n];BOOLVis[n];ll N, M;ll sum, AVG, res, dis;voidInit () {memset(SUMV,0,sizeof(SUMV));memset(Vis,false,sizeof(VIS));memset(Cake,0,sizeof(cake)); for(inti =0; I <= m; i++) Ans[i].clear ();}BOOLDfsintCurintSumintPOS) {if(cur = = m +1)return true; for(inti = res; I >= pos; i--) {if(Vis[i])Continue;if(sum + i = = dis)            {Cake[i] = cur; Vis[i] =true;if(Dfs (cur+1,0,1))return true; Vis[i] =false;return false; }Else if(Sum + i < dis)            {Cake[i] = cur; Vis[i] =true;if(Dfs (cur, sum+i, i+1))return true; Vis[i] =false; }     }return false;}intMain () {intTscanf("%d", &t); while(t--) {scanf("%lld%lld", &n, &m); Init ();if(N &1) sum = (n +1) /2NElsesum = N/2* (n +1);if(sum% m! =0) {puts("NO");Continue; } avg = sum/m;if(Avg < N) {puts("NO");Continue; }puts("YES"); res = n% (2* m);if(Res! =0) {res + =2* m;        res = MIN (n, res); }//Divided into 2 * M parts        intA, B; for(inti = n; i > Res; I-= (2* m)) { for(intK =1; K <= m; k++) {a = I-k +1, B = i-(2* m) + K;                ANS[K].PB (a), ANS[K].PB (b);            Sumv[k] + = A, sumv[k] + = b; }} dis = avg-sumv[1]; Dfs1,0,1); for(inti =1; I <= Res;        i++) {ANS[CAKE[I]].PB (i); } for(inti =1; I <= m; i++) {intSize = Ans[i].size ();printf("%d", size); for(intK =0; K < size; k++) {printf("%d", Ans[i][k]); }puts(""); }    }return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 5355 Cake (construction + backtracking)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.