[NOIP review] Chapter 2: Dynamic Planning

Source: Internet
Author: User
Tags radar

[NOIP review] Chapter 2: Dynamic Planning
I. Backpack Problems

1. Wikioi 1014 Packing Problem

Description Description

There is a box with a capacity of V (positive integer, 0 <= V <= 20000) and n items (0 <n <= 30 ), each item has a volume (positive integer ).

It is required that any number of n items be loaded into the box, so that the remaining space of the box is minimized.

Input description Input Description

An integer v indicates the box capacity.

An integer n indicates that there are n items

The next n integers indicate the respective volumes of the n items.

Output description Output Description

An integer that indicates the remaining space of the box.

Sample Input Sample Input

24

6

8

3

12

7

9

7

Sample output Sample Output

0

A classic backpack motion gauge, which can be used for motion gauge with array f []. f [v] = the maximum volume that can be used when the remaining capacity is v, you can traverse the remaining capacity status every time you input an item volume cost. When the remaining capacity in the current status is v, you can choose to load an item (the volume that can be used to load the item is f [v-cost] + cost) or not to load the item, and introduce the dynamic rule equation: f [v] = max {f [v-cost] + cost}

# Include
 
  
# Include
  
   
# Define MAXN 30000int f [MAXN]; // f [I] = the remaining volume is the maximum volume of the Imported item int max (int a, int B) {if (a> B) return a; return B;} int main () {int v, n, cost; scanf ("% d", & v, & n); for (int I = 1; I <= n; I ++) {scanf ("% d", & cost); for (int j = v; j> = cost; j --) f [j] = max (f [j], f [j-cost] + cost);} printf ("% d \ n ", v-f [v]); return 0 ;}
  
 

2. Wikioi 1068

Description Description

During James's birthday, his father gave him a pair of turtles as a gift. The turtle chess board contains N grids in a row, each of which has a score (non-negative integer ). The checkboard 1st is the only starting point, and the nth is the final point. The game requires players to control a tortoise piece from the starting point to the final point.

...... 1 2 3 4 5 ...... N. M crawling cards in Tortoise games are divided into four different types (M cards do not necessarily contain all four types of cards, as shown in the example ), each type of card is marked with one of the four numbers: 1, 2, 3, and 4, indicating that after using this card, the tortoise will crawl the corresponding number of grids forward. In the game, each time a player needs to select a previously unused crawling card from all crawling cards, controlling the number of squares that the tortoise moves forward. Each card can only be used once. In the game, the tortoise piece automatically obtains the score of the starting point lattice, and gets the corresponding score for each entry in the subsequent crawling. The final score of a player is the sum of the scores of all the squares that have elapsed from the start point to the end point. Obviously, different order of use of crawling cards will make the final game score different. James wants to find a card use order to make the final game score the most. Now, tell you the scores of each grid on the Board and all crawling cards. Can you tell James how many points he gets?

Input description Input Description

Two numbers in each input line are separated by a space. There are two positive integers N and M in row 1st, indicating the number of checkerboard grids and the number of crawling cards, respectively. N non-negative integers in the 2nd rows, a1a2 ...... AN

Where ai indicates the score on the I-th grid of the Board. The number of M integers in the second row is b1b2 ...... BM

Indicates the number on M crawling cards. Input data to ensure that only M crawling cards are used when the end point is reached, that is, N-1 = Σ (1-> M) bi

Output description Output Description

Output an integer in a row

Sample Input Sample Input

13 8

4 96 10 64 55 13 94 53 5 24 89 8 30

1 1 1 1 1 2 4 1

Sample output Sample Output

455

Data range and prompt Data Size & Hint

[Data Scope]

For 30% of data, 1 ≤ N ≤ 30, 1 ≤ M ≤ 12.

For 50% of the data, there are four types of crawling cards: 1 ≤ N ≤, 1 ≤ M ≤ 50, and the number of cards for each type is no larger

Over 20.

For 100% of the data, there are four types of crawling cards: 1 ≤ N ≤ 120, 1 ≤ M ≤, and the number of cards for each type is not

More than 40; 0 ≤ ai ≤, 1 ≤ I ≤ N; 1 ≤ bi ≤ 4, 1 ≤ I ≤ M. Input data guarantee N? 1 = Σ M

I B

1

It can be said that this is a problem of deformation of a backpack, and there is no longer a single State (remaining volume). In fact, because of card classification, the status has changed to four: for the remaining number of cards for each of the four types of crawling cards, the array f [] [] [] [] can be used for the action rule, f [I] [j] [k] [h] = 1, 2, 3, and 4 cards each use I, j, k, h, the maximum score obtained by playing a game, from small to large, goes through the state f [I] [j] [k] [h, consider using a card number 1, 2, 3, or 4, then the score obtained before this operation is f [I-1] [k] [j] [h] or f [I] [k-1] [j] [h] or f [I] [k] [J-1] [h] or f [I] [k] [j] [h-1] (Note: the score obtained by the last operation does not include the number of grid scores obtained after the last operation.) then, the number of grid scores obtained after the last operation is added. The number of grid scores obtained by this operation is calculated after the next operation. Finally, f [card [1] [card [2] [card [3] [card [4] + map [n] is output. card [x] = the number of cards x, map [n] = the end point.

Of course, you can also calculate the grid score of the starting point first. When making a decision, you do not add the grid score of the next operation, but add the grid score of this operation, which may be easier to understand.

# Include
 
  
# Include
  
   
# Define MAXM 40 # define MAXN 400int f [MAXM] [MAXM] [MAXM] [MAXM]; // f [I] [j] [k] [h] = 1 2 3 4 cards use I, j, k, h indicates the maximum score of an integer. int map [MAXN]; // The integer int max (int a, int B) {if (a> B) return; return B;} int main () {int n, m; int card [5] = {0}; scanf ("% d", & n, & m ); for (int I = 1; I <= n; I ++) scanf ("% d", & map [I]); for (int I = 0; I
   
    
= 1) f [I] [j] [k] [h] = max (f [I] [j] [k] [h], f [I-1] [j] [k] [h] + map [1 + (I-1) * 1 + j * 2 + k * 3 + h * 4]); if (j> = 1) f [I] [j] [k] [h] = max (f [I] [j] [k] [h], f [I] [J-1] [k] [h] + map [1 + I * 1 + (J-1) * 2 + k * 3 + h * 4]); if (k> = 1) f [I] [j] [k] [h] = max (f [I] [j] [k] [h], f [I] [j] [k-1] [h] + map [1 + I * 1 + j * 2 + (k-1) * 3 + h * 4]); if (h> = 1) f [I] [j] [k] [h] = max (f [I] [j] [k] [h], f [I] [j] [k] [h-1] + map [1 + I * 1 + j * 2 + k * 3 + (h-1) * 4]);} printf ("% d \ n ", f [card [1] [card [2] [card [3] [card [4] + map [n]); return 0 ;}
   
  
 

3. Wikioi 1044 intercepts missiles

Description Description

A country develops a missile interception system to defend against missile attacks by the enemy. However, this missile interception system has a defect: although its first shell can reach any height, it cannot be higher than the previous one in the future. One day, the radar captured the enemy's missiles. Because the system is still in the trial phase, there is only one system, so it may not be able to intercept all missiles.

Input description Input Description

Input the height of the missile flying in sequence (the height data given by the radar is a positive integer not greater than 30000)

Output description Output Description

Outputs the maximum number of missiles that the system can intercept, and the minimum number of such missile interception systems to intercept all missiles.

Sample Input Sample Input

389 207 155 300 299 170 65

Sample output Sample Output

6

2

Data range and prompt Data Size & Hint

Missile height <= 30000, number of missiles <= 20

The height of a missile can be converted into a digital sequence. From the question, we can see that the target of a missile interception must be an ascending subsequence of the original sequence. If we want to have as many targets as possible, it is required that the non-ascending subsequence be the longest. In other words, the first question is to find the non-ascending subsequence, and the second question is a little troublesome. It is necessary to make all targets hit with as few missiles as possible, therefore, each missile should not only set a single target, but also hit the non-ascending sub-Sequence starting with this target. The green line is the non-ascending sub-sequence, the red line is a strictly ascending subsequence. Obviously, the second question requires a maximum strict ascending subsequence. All elements in this subsequence need a missile, the non-ascending subsequences they connect can be hit by these missiles. The second question is the maximum strict ascending subsequences.

#include 
 
  #include 
  
   #include 
   
    #define MAXN 1000int up[MAXN],dn[MAXN];int high[MAXN],cnt=0;int maxDN=-1,maxUP=-1;int max(int a,int b){if(a>b) return a;return b;}int main(){while(scanf("%d",&high[++cnt])!=EOF);for(int i=1;i<=cnt;i++)for(int j=1;j
    
     high[j]){up[i]=max(up[i],up[j]+1);}if(high[i]<=high[j]){dn[i]=max(dn[i],dn[j]+1);}}for(int i=1;i<=cnt;i++){maxDN=max(maxDN,dn[i]);maxUP=max(maxUP,up[i]);}printf("%d\n%d\n",maxDN,maxUP+1);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.