# 2016-level algorithm the fourth time on the machine-d.alvinzh 1021 experiment Plus

Source: Internet
Author: User

1021 Experimental plus ideas of 978 Alvinzh

Greedy, medium problem.

Use the Miss variable to represent the smallest number that is not overwritten, with an initial value of 1.
The initial coverage interval is [1,miss], and the goal is to overwrite [1,m], i.e. the miss needs to be greater than M.
You need to compare the smallest number x in numbers that are not used in miss and arrays (so you need to sort the array first.)

• Miss is smaller: there is no combination that satisfies the desired value, the value needs to be inserted, that is, insert the miss, and the coverage interval becomes \ ([1,miss<<1) \) .
• Miss larger: The current can overwrite [1,miss], plus this value, the coverage interval becomes \ ([1,miss+x) \) .

Cyclic termination conditions: Miss > M.

The following are the contents:

Many students asked me why WA, the reason is not to see weights can only be placed on the same side, by the impact of the previous question (Ta chuckle, the lad is still not fine.)

There is the same learning OJ is not a problem, obviously should be WA but hint tle, because the array number in the int range, Miss may be more than int, in the group has said Int32_max+1 what will become, the answer is int32_min, so your while equivalent to Infinite loop, No, it's not tle.

Analysis

The greedy of the subject is to maximize the coverage every time, in order to get close to the target number M.

Again greedy, greedy algorithms always make the best choice at the moment. That is to say, the greedy algorithm does not take the overall optimal consideration, it makes the choice is only in some sense the local optimal choice. Of course, it is hoped that the final result of greedy algorithm is also the overall optimal. Although the greedy algorithm cannot get the whole optimal solution for all problems, it can produce the whole optimal solution for many problems. such as single source shortest path through problem, minimum spanning tree problem, etc. In some cases, even if the greedy algorithm can not get the overall optimal solution, the final result is a good approximation of the optimal solution.

Reference Code One
``////Created by Alvinzh on 2017/11/19.//Copyright (c) alvinzh. All rights reserved.//#include <cstdio>#include <algorithm>using namespaceStdintN, M;intfm[1005];intMain () { while(~SCANF ("%d %d", &n, &m)) { for(inti =0; I < n; ++i) scanf ("%d", &fm[i]); Sort (FM, FM + N);intAns =0;intpos =0;Long LongMiss =1; while(Miss <= m) {if(POS < n && Fm[pos] <= Miss)                {Miss + = Fm[pos];            pos++; }Else{ans++; Miss <<=1; }} printf ("%d\n", ans);/ * Simple Replacementint ans = 0, pos = 0;For (Long miss = 1; Miss <= m; ans++)Miss + = (pos < n && Fm[pos] <= miss)? Fm[pos++]: Miss;printf ("%d\n", Ans-pos);        */}}``

2016-level algorithm the fourth time on the machine-d.alvinzh 1021 experiment Plus

Related Keywords:

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.