"Codeforces" 755F Polandball and Gifts

Source: Internet
Author: User

Portal

Codeforces & Luogu

Title Description

It ' s Christmas time! Polandball and his friends'll be giving themselves gifts. There is n n Balls overall. Each ball had someone for whom he should bring a present according to some permutation p, p{i}≠i for all I.

Unfortunately, Balls is quite clumsy. We know earlier that exactly k of them would forget to bring their gift. A Ball Number I'll get his present if the following II constraints'll hold:

Ball number I'll bring the present he should give. Ball x such that P{x}=i would bring his present. What are minimum and maximum possible number of kids who would not get their present if exactly K Balls would forget theirs?

Ideas

First of all see the topic, people must be ignorant force, completely do not know how to do.

After observing a period of time, it can be found that if from the first person to the person to send a gift to the a{i} individual with a directed line segment, then the entire graph can be turned into a bunch of closed loop structure, such as the following situation:

By observing the picture, you can get the conclusion that:

And then get into the loop until you see the puzzle 2333333

First of all, the topic asks for maximum and minimum values, then consider separately

Maximum value:

Because the most people can not get gifts, so it should be thought that this should be a greedy, greedy strategy is this: first consider taking a person and make two people do not have a gift, that is, if a to give a B gift, then choose a after will definitely not choose B, at this time we start from the ring, if it is even ring, then very good Only need N/2 individual can make n person have no gift qwq, but if is odd ring, according to our greedy strategy, two two greedy after will have 1 person surplus, then we don't go to this person, continue to look at other ring, if can according to the original greedy strategy to take enough k person, then we will not go to the rest of the people. But if all the rings are taken, and not enough to take the K-person, then you have to go back to think about the rest of the people, so greedy can be the biggest greedy.

    int left=k;The rest of the peopleint weipipei=0; //odd ring did not match the person for (register int i=1;i<=cnt;i++) {if (huan[i]/< Span class= "Hljs-number" >2<=left) {//if you can also match the entire ring left-=huan[i]/ 2; Maxans+=huan[i]/2*2;} else{//if the remainder is too small to match the entire ring Maxans+=left*0;} if (Huan[i]&1) weipipei++; //odd ring, plus the remaining person} maxans+=min (Left,weipipei); //last add             

When the minimum value is taken:

Obviously each ring is taken out is the smallest, of course, not necessarily can ensure that just take a certain number of rings can make k individuals just be taken out, so if you can not just take a certain number of rings so that the sum of the number of K, the remainder will be in the rest of the ring to continue to take, such a minimum value of k+1, and if The sum is K, so the minimum value is K.

Obviously, after this analysis, it will be found that this is a multi-pack, because the number of each ring is limited, to select a certain number of rings in all kinds of rings, so that the sum of K. Then define the bool array Dp[i] for the number of people elected I is able to be expressed in full, so if the value of Dp[k] is 1, then the explanation can be just out, the answer is k, otherwise the answer is k+1.

But note that the data range of the problem is very large, so you can not split a kind of ring into a separate one, and then do 01 backpack, must be optimized with binary, split into 1,2,4,8,16 ... And then do 01 backpacks, which can save a lot of time.

int temp=0;    for(register int i=1;i<=cnt;i++){ if(huan[i]==huan[i-1])shu[temp]++; else { shu[++temp]++; re[temp]=huan[i]; // tong ji shu ju } }

This code is to say that at the time when the statistics are used CNT to form the first ring, the value of the array is the size of the ring, so it is not good for binary optimization, because we do not know how many rings of each size, so the Shu array represents the number of rings, the re array represents the size of the ring.

cnt=0;    for(register int i=1;i<=temp;i++){ for(register int j=1;shu[i]>0;j<<=1){ int leftt=min(shu[i],j); opt[++cnt]=leftt*re[i]; shu[i]-=leftt; } }

For binary optimization, the OPT array records binary optimization after each type of ring is disassembled.

for(register int i=1;i<=cnt;i++){        for(register int j=k;j>=opt[i];j--){ if(dp[j-opt[i]])dp[j]=1; } }

A simple backpack 23333

A small detail

Use scanf when reading, or it will be tle QWQ

Attach the complete code (almost as much as the internet qwq)
#Include<iostream>#Include<cstring>#Include<algorithm>#Include<cstdio>UsingNamespaceStdint a[1000005],huan[1000005],shu[1000005],re[1000005],opt[1000005];BOOL vis[1000005];BOOL dp[1000005];InlineIntCmp(int A,int b) {return a<b;}IntDfs(int now,int from,int dep) {if (Now==from)return DEP;else {vis[now]=1; DFS (a[now],from,dep+1); }}int maxans=0;IntMain() {dp[0]=1;int n,k;cin>>n>>k;ForRegisterint i=1;i<=n;i++) {scanf"%d", &a[i]); }int cnt=0;ForRegisterint i=1;i<=n;i++) {if (vis[i]==0) {Huan[++cnt]=dfs (a[i],i,1); }} sort (huan+1,huan+cnt+1,CMP);Qiu Zui da zhi------tan xinint left=k;int weipipei=0;ForRegisterint i=1;i<=cnt;i++) {if (huan[i]/2<=left) {left-=huan[i]/2; maxans+=huan[i]/*2; }else{maxans+=left*2; left=0; }if (huan[i]&1) weipipei++; } maxans+=min (Left,weipipei);Qiu Zui Xiao zhi------duo Chong Bei Baoint temp=0;ForRegisterint i=1;i<=cnt;i++) {if (huan[i]==huan[i-1]) shu[temp]++;else {shu[++temp]++; re[temp]=huan[i];Tong Ji Shu Ju}} cnt=0;ForRegisterint i=1;i<=temp;i++) {Forregister int j=1;shu[i]>0;j<<=1) {int leftt=min (SHU[I],J) ; Opt[++cnt]=leftt*re[i]; SHU[I]-=LEFTT; }} for (register int I=1;i<=cnt;i++) {for (register int j=k;j>=opt[i];j--) {if (Dp[j-opt[i]]) dp[j]= 1;} } int minans=k; if (!dp[k]) minans++; cout<<minans<< "<<maxans<<ENDL;}              

"Codeforces" 755F Polandball and Gifts

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.