Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5090
The main topic: give you n number, ask you to add a number of number of c*k (c>=0) can compose 1,2,3,4,5,..., n?
When I played this afternoon, I used a DFS to see which of the a[i] matches 1 to N.
Later tle to die ...
Think about it, first of all, if the number is a small number, then it can match a lot of kinds, so if you first match it out will be wasted, because it "ability" big.
So you can order, from big to small to match.
I don't know how to prove it in mathematical language.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cstdlib>5#include <algorithm>6#include <map>7#include <Set>8#include <queue>9#include <string>Ten#include <functional> One using namespacestd; A - intvis[ the]; - inta[ the]; the intn,k; - BOOLFlag; - - intMain () { + intM; -scanf"%d",&M); + A while(m--){ atFlag =true; -memset (Vis,0,sizeof(Vis)); -scanf"%d%d",&n,&K); - for(intI=0; i<n;i++){ -scanf"%d",&a[i]); - } inSort (a,a+n,greater<int>() ); - inti; to for(i=n;i>=1; i--){ + for(intj=0; j<n;j++){ - if(a[j]<=i&&!vis[j]&& (i-a[j])%k==0 ){ thevis[j]=1; * Break; $ }Panax Notoginseng } - } the for(intI=0; i<n;i++){ + if( !Vis[i]) { AFlag =0; the Break; + } - } $ if(flag) puts ("Jerry"); $ ElsePuts"Tom"); - } - return 0; the } - Wuyi /* the Ten - 3 1 Wu 1 2 3 - 3 1 About 0 0 0 $ 3 2 - 2 2 3 - 3 2 - 0 1 3 A 5 1 + 1 2 3) 4 5 the 6 2 - 1 2 3 4 5 5 $ */
[HDU 5090] Game with pearls (greedy)