Codeforces 388A Fox and Box Accumulation (Greedy)

Source: Internet
Author: User

Link: Codeforces 388A Fox and Box Accumulation


N boxes are provided. Each box tells you that you can put at most a few boxes on the box and asks how many boxes are needed.


Solution: x is only 105 at the maximum. Use a cnt array to record the number of each box, and then add the number from the smallest until it cannot be added.


# Include

 
  
# Include

  
   
# Include using namespace std; const int N = 105; int n, cnt [N], tmp; void init () {int a; tmp = 0; scanf ("% d", & n); for (int I = 0; I <n; I ++) {scanf ("% d", & ); tmp = max (a, tmp); cnt [a] ++ ;}} void set (int x, int c) {if (x> tmp) return; if (c> x | cnt [x] = 0) {set (x + 1, c);} else {cnt [x] --; set (x, c + 1) ;}} int solve () {int ans = 0; for (int I = 0; I <= tmp; I ++) {while (cnt [I]) {set (I, 0); ans ++ ;}} return ans ;}int main () {memset (cnt, 0, sizeof (cnt); init (); printf ("% d \ n", solve (); return 0 ;}

  

 


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.