[HDU 5090] Game with pearls (greedy)

Source: Internet
Author: User

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)

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.