Dp struggling before noip

Source: Internet
Author: User

I wrote a few relatively watery dp statements, but there are also many problems, such as the order of initialization and loop, and the biggest problem: Dynamic equations...

 

CODEVS1253 supermarket

Description Description

A person prefers to go to the market to buy food according to his own rules. Every day, he makes a list of food buyers. There is also an order of food in the free market. This person has a characteristic that he buys food in order, she never goes back. Of course, she wants to spend the best money to buy all the dishes. Can you help him?

 

The input and output data is as follows:

Idea: relatively simple dp. f [I] [j] indicates the minimum cost for the menu to be purchased from the market to j. If I = j, update this point, otherwise f [I] [j] = f [i-1] [j]. When initialization is performed, f [I] [0] = 0. If the other values are used, the maximum values are calculated. Then, f [n] [m] and 0x7fffff are compared. If the value is greater than that, impossible is used. Otherwise, the output is used.
# Include <iostream> # include <cstdio> # include <cstring> using namespace std; struct use {int kind; double mon ;} B [100001]; double f [100001] [101] = {0}; int a [101] = {0}; int main () {int I, j, n, m, k; scanf ("% d", & m, & n); memset (f, 127, sizeof (f); for (I = 1; I <= m; ++ I) scanf ("% d", & a [I]); for (I = 1; I <= n; ++ I) scanf ("% d % lf", & B [I]. kind, & B [I]. mon); for (I = 1; I <= n; ++ I) f [I] [0] = 0; for (I = 1; I <= n; + + I) for (j = 1; j <= m; ++ j) {f [I] [j] = f [i-1] [j]; if (B [I]. kind = a [j]) f [I] [j] = min (f [I] [j], f [i-1] [j-1] + B [I]. mon);} if (f [n] [m]> 0x7fffff) printf ("Impossible \ n"); else printf ("%. 2f \ n ", f [n] [m]);}
RZUC Code

 

 

CODEVS1959 tug-of-war games

Description Description

When a tug-of-war competition is held in a school, all people are divided into two groups, each of which must (and can only) be in one group. The difference between the two groups cannot exceed 1, and the weights of all people in the two groups are as close as possible.

 

Idea: A two-dimensional charge (bool) backpack problem, one is the number of elements (equivalent to the volume), and the other is the weight. First, find half of the total weight and half of the number of elements, and carry on the backpack.

 

# Include <iostream> # include <cstdio> using namespace std; bool f [101] [45001] = {false}; int a [101] = {0 }; int main () {int I, j, sum = 0, n, k, q, cha, summ; cin> n; if (n % 2 = 0) k = n/2; else k = n/2 + 1; for (I = 1; I <= n; ++ I) {cin> a [I]; sum + = a [I];} if (summ % 2 = 0) summ = sum/2; else summ = sum/2 + 1; for (I = 0; I <= 1; ++ I) f [I] [0] = true; for (I = 1; I <= n; ++ I) for (j = summ; j> = a [I]; -- j) for (q = k; q> = 1; -- q) f [q] [j] = f [q] [j] | f [q-1] [j-a [I]; for (j = summ; j> = 0; -- j) if (f [k] [j]) {cout <min (j, sum-j) <"<max (j, sum-j) <endl; break ;}}
RZUC Code

 

 

CODEVS1060 Funny World Cup

Description Description

With the completion of the World Cup group competition, France, Argentina and other world strong teams have been eliminated, which makes a heartache.

People organized a funny world cup and reorganized these eliminated strong teams to compete with the World Cup. You and your friends

You are happy to purchase tickets. However, for the funny World Cup, tickets are sold in A very special way. They only have two tickets. Class

Ticket ------ free world Pass B-class pass ------- double-price world pass. The ticket is determined by the staff by throwing a coin at the purchase and sent to the front.

Because it is A market economy, it is impossible for the organizers to post money, so they always prepare

With the same number of Class A and Class B tickets, you and your friends are lucky to be in the last two places of A wonderful competition.

At this time, the staff began to sell tickets via coins. Fortunately, when the staff came to you, they found they were no longer needed.

I threw another coin, because the remaining two tickets are free tickets.

 

When you and your friends are excited, they want to calculate the probability that two people at the end of the team will get a ticket at the same time.

(Including taking both Class A and Class B tickets) assume that the staff has prepared 2n tickets, n of which are class A and n are Class B, in addition, each person in the team must buy only one ticket (whether to buy A or B ).

 

Train of thought: let's talk about a tough task first. It reads 2n, not n. F [I] [j] indicates the probability that the I-th person will buy j A tickets. There are three scenarios for j:

1) j = 0: f [I] [j] = f [i-1] [j] * 0.5; 2) 0 <j <n: f [I] [j] = (f [i-1] [j] + f [i-1] [j-1]) * 0.5; 3) j = n: f [I] [j] = f [i-1] [j] + f [i-1] [j-1] * 0.5.

Initialize f [0] [0] = 1; output f [n * 2-2] [n] * 2 (because the last two can be A or B );

 

# Include <iostream> # include <cstdio> using namespace std; double f [3000] [3000] = {0}; int main () {int n, I, j; cin> n; n/= 2; f [0] [0] = 1; for (I = 1; I <= 2 * n; ++ I) for (j = 0; j <= n; ++ j) {if (j> I) continue; if (j = 0) f [I] [j] = f [i-1] [j] * 0.5; else {if (j <n) f [I] [j] = (f [i-1] [j] + f [i-1] [j-1]) * 0.5; else f [I] [j] = f [i-1] [j] + f [i-1] [j-1] * 0.5;} printf ("%. 4f \ n ", f [n * 2-2] [n] * 2 );}
RZUC Code

 

 

CODEVS3369 worship

Description Description

There are a lot of magic cows... Of course... Every student has his own kind of awesome God.
A school has two cows, a. X and B. N new students have long heard of their myth. Therefore, I sincerely worship one of them.
Now, the teacher wants to allocate data centers to them.
However, either make sure that the entire machine room is the same as the worship of the cool, or that the number of worships of the two cool cannot exceed M.
In addition, now N students are arranged in a row, and the teacher will only split a row of students into a data center. The teacher wants to know how many data centers are required.

Idea: pre-process how many worships (prefix and array) there are at I, and then the dp section. f [I] indicates the minimum number of classrooms allocated to I, loop I and k (I-k indicates the number of people in the last classroom), and update the value of f [I. Preprocessing: f [0] = 0;
# Include <iostream> # include <cstdio> # include <cstring> # include <cmath> using namespace std; int f [3000] = {0 }, sum1 [3000] = {0}, sum2 [3000] = {0}; int main () {int n, m, I, j, x; memset (f, 127/3, sizeof (f); scanf ("% d", & n, & m); for (I = 1; I <= n; ++ I) {sum1 [I] = sum1 [i-1]; sum2 [I] = sum2 [i-1]; scanf ("% d", & x); if (x = 1) ++ sum1 [I]; else ++ sum2 [I];} f [0] = 0; for (I = 1; I <= n; ++ I) for (j = 0; j <I; ++ j) if (abs (sum1 [I]-sum1 [j]) -(sum2 [I]-sum2 [j]) <= m | sum1 [I]-sum1 [j] = 0 | sum2 [I]-sum2 [j] = 0) f [I] = min (f [I], f [j] + 1); cout <f [n] <endl ;}
RZUC Code

 

 

CODEVS

Description Description

Now there are n items (which may be the same). Please calculate the number of different methods to obtain k items.

 

Thought: for multiple knapsack problems, the number is used as an element and the number of occurrences is the number. Note that when selecting the number, the cycle will be 1, and the first cycle will be 0, and the result will be wa... Die...

 

# Include <iostream> # include <cstdio> # include <algorithm> using namespace std; int a [31], f [31] = {0 }, w [31] = {0}; int main () {int n, k, I, j, p; cin> n> k; for (I = 1; I <= n; ++ I) cin> a [I]; sort (a + 1, a + n + 1); ++ w [0]; + + w [w [0]; for (I = 2; I <= n; + + I) {if (a [I] = a [i-1]) ++ w [w [0]; else {++ w [0]; ++ w [w [0];} if (w [w [0]> k) w [w [0] = k;} f [0] = 1; for (I = 1; I <= w [0]; ++ I) for (j = k; j> = 0; -- j) for (p = w [I]; p> = 1; -- p) if (j> = p) f [j] = f [j] + f [j-p]; cout <f [k] <endl ;}
RZUC Code

 

Dp struggling before noip

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.