[Luogu P2170] Select Genius (and check the set +DP)

Source: Internet
Author: User

Topic Portal: https://www.luogu.org/problem/show?pid=2170

Title Description

The teacher wants to choose m people from N students as genius, but there are k to the strength of the person, if the strength of the people, part of the election, the other part is not, the students will protest. So the teacher wants you to help him to find out how many genius he should choose, in order not to let the students protest, and the original m as close as possible

Input/output format

Input format:

The first line, three positive integer n,m,k.

2nd ... K Line, 2 numbers per line, indicates the number of people of equal strength (numbered 1 ... N

Output format:

One line, saying that neither the students to protest, and the original m as close as possible to elect the number of genius. (if there are two schemes equal to the absolute value of M's difference, choose the smaller one:)

Input and Output Sample input example # #:
4 3 21) 23 4
Sample # # of output:
2
Description

100% of Data n,p<=20000

Use and check set will all the strength of the person and into a collection, each set as an item, the value of the goods for each set of people, and then converted to 01 knapsack problem, because it is the sum of values and m of the absolute value of the smallest, so the use of 2*m capacity of the backpack, DP can.

State transition equation: F[j]=max (f[j],f[j-cnt[i]]+cnt[i]).

The answer can be solved by scanning the two sides from M.

1#include <cstdio>2#include <iostream>3#include <algorithm>4 using namespacestd;5 6 intn,m,k,tot,que[20050],cnt[20050],father[20050],f[50050];7 8 intGfintx) {9     if(father[x]==x)returnx;Tenfather[x]=GF (father[x]); One     returnFather[x]; A } -  - intMain () { thescanf"%d%d%d",&n,&m,&k); -      for(intI=1; i<=n; i++) father[i]=i; -      for(intI=1; i<=k; i++) { -         intx, y; +scanf"%d%d",&x,&y); -         intfx=GF (x); +         intfy=GF (y); A         if(fx!=fy) father[fx]=fy; at     } -      for(intI=1; i<=n; i++) CNT[GF (i)]++; -      for(intI=1; i<=n; i++)if(Cnt[i]) que[++tot]=i; -      for(intI=1; i<=tot; i++) { -         intCur=Que[i]; -          for(intj=2*m; j>=cnt[cur]; j--) inF[j]=max (f[j],f[j-cnt[cur]]+cnt[cur]); -     } to      for(intI=0; i<=m; i++) { +         if(f[m-i]==m-i) { -printf"%d", M-i); the              Break; *         }  $         if(f[m+i]==m+i) {Panax Notoginsengprintf"%d", m+i); -              Break; the         } +     } A}

[Luogu P2170] Select Genius (and check the set +DP)

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.