Question link:
Https://icpcarchive.ecs.baylor.edu/index.php? Option = com_onlinejudge & Itemid = 8 & page = show_problem & problem = 1668
Root |
|
Regionals 2006> Asia-Beijing 3667-rulertime limit: 3.000 seconds |
Question meaning:
There are n length requirements. Ask how to design a ruler scale so that each length is between two scales. The shorter the ruler length, the better. The first scale is 0.
Solution:
DFS
Because the maximum scale is 7 and the minimum scale number can be calculated, the maximum length that can be measured after the number of scales m is fixed C (m, 2 ).
To satisfy the minimum scale and the shortest length, assume that the last scale is at the longest position.
Then, increase the brute-force search speed.
Code:
// # Include <cspreadsheet. h> # include <iostream> # include <cmath> # include <cstdio> # include <sstream> # include <cstdlib> # include <string. h> # include <cstring> # include <algorithm> # include <vector> # include <map> # include <set> # include <stack> # include <list> # include <queue> # include <ctime> # include <bitset> # include <cmath> # define EPS 1e-6 # define INF 0x3f3f3f3f # define PI ACOs (-1.0) # define ll _ int64 # define ll Lo Ng long # define lson L, M, (RT <1) # define rson m + 1, R, (RT <1) | 1 # define M 1000000007 // # pragma comment (linker, "/Stack: 1024000000,1024000000") using namespace STD; # define maxn 55 # define maxm 1100000int hav [maxm], sa [maxn], n, ans; bool vis [maxn]; int dis [maxn]; bool DFS (INT cur) {If (cur = ans) {for (INT I = 1; I <n; I ++) // The first n-1 length can be measured if (! Vis [I]) return false; return true;} For (INT I = 1; I <cur; I ++) {for (Int J = 1; j <N; j ++) {If (! Vis [J]) {int dd = dis [I] + SA [J]; // current scale if (DD <= dis [cur-1]) // greater than the previous continue; If (DD> = sa [N]) // smaller than the maximum continue; DIS [cur] = dd; queue <int> myq; // The length of the record tag. the return value is for (int K = 1; k <cur; k ++). // after the current scale is added, the length {int temp = dis [cur]-Dis [k]; If (HAV [temp] &! Vis [hav [temp]) {vis [hav [temp] = true; myq. push (HAV [temp]) ;}} int LA = sa [N]-Dis [cur]; // The last section if (HAV [la] &! Vis [hav [la]) {vis [hav [la] = true; myq. push (HAV [la]);} If (DFS (cur + 1) return true; while (! Myq. empty () {LA = myq. front (); myq. pop (); vis [la] = false ;}}} return false;} int main () {// freopen ("in.txt", "r", stdin ); // freopen ("out.txt", "W", stdout); int CAS = 0; while (~ Scanf ("% d", & N) {for (INT I = 1; I <= N; I ++) scanf ("% d ", & SA [I]); sort (SA + 1, Sa + n + 1); n = unique (SA + 1, Sa + n + 1)-sa-1; memset (HAV, 0, sizeof (HAV); For (INT I = 1; I <= N; I ++) hav [SA [I] = I; dis [1] = 0; ans = 2; while (ANS * (ans-1)/2 <n) ans ++; memset (VIS, 0, sizeof (VIS )); while (! DFS (2) ans ++; printf ("case % d: \ n % d \ n", ++ cas, ANS); printf ("% d ", dis [1]); DIS [ANS] = sa [N]; for (INT I = 2; I <= ans; I ++) printf ("% d ", dis [I]); putchar ('\ n');} return 0 ;}
[DFS] uvalive 3667 ruler