BZOJ 4619 Swap Space solution report, bzoj4619

Source: Internet
Author: User

BZOJ 4619 Swap Space solution report, bzoj4619

Today, I did it just because David Lee spoke about a similar question.

This is a piece of water from world final 2016 ......

The question address is as follows:

Http://www.lydsy.com/JudgeOnline/problem.php? The id = 4619.

This topic allows us to find the minimum extra space required to format all hard disks.

This question can easily be seen as a greedy question (according to David Lee, there is another way of doing this: Binary + greedy, however, qdez's teacher Tian Shuo stonepage and I Icontofig both think that the dual + greedy is a little troublesome. However, stonepage is not intended to be written, so I will write this topic solution Report ).

So how can we get greedy?

First, let's take a picture of the difference between the value after formatting and before formatting, and then add up (the remaining space) see how much extra space we need (the remaining space is a negative number ). This idea is easy to get rid of by Hack. You can write a group by yourself that is greater than 0, because the disk space at the beginning is 0.

The second idea is to add the difference between the value after formatting and the value before formatting greater than 0, and then perform operations based on the value of each disk in ascending order. This is also very easy to hack, for example, suddenly a space before formatting is very large, this approach is awesome.

The third idea: I will reverse the second one. Can I do it? Obviously not. Some data can be easily hack you.

What should I do?

Positive Solution:

We pre-process the input data, put the difference between the formatted data and the pre-formatted data greater than 0 in a struct, and put the less than 0 in a struct.

First, process the values greater than 0, sort them in ascending order according to the values of B, then simulate the process (set the remaining space at the beginning to 0), and use sum to record the minimum negative value during the process, record the remaining space with ans (that is, co in the program later ).

If the number is smaller than 0, perform the same operation as above (but the remaining space is not 0 at this time, and the above operation has been changed );

Then the absolute value of sum is the answer we want! I will ask about stonepage tomorrow;

The Code is as follows:

 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <cstdlib> 7 using namespace std; 8 typedef long long LL; 9 const int maxn = 1000005;10 struct SD{11     LL a,d;12 }p[maxn];13 struct counter{14     LL a,b,co;15 }pluss[maxn],dece[maxn];16 bool cmp1(counter a,counter b){17     return a.b < b.b;18 }19 bool cmp2(counter a,counter b){20     return a.a > b.a;21 }22 LL n,ans,sum,now1,now2;23 LL get_num(){24     LL num = 0;25     char c;26     bool flag = false;27     while((c = getchar()) == ' ' || c == '\n' || c == '\r');28     if(c == '-')flag = true;29     else num = c - '0';30     while(isdigit(c = getchar()))31         num = num * 10 + c - '0';32     return (flag ? -1 : 1) * num;33 }34 void init(){35     memset(p,0,sizeof(p));36     memset(pluss,0,sizeof(pluss));37     memset(dece,0,sizeof(dece));38     now1 = now2 = 0;39 }40 int main(){41     init();42     n = get_num();43     for(int i = 1;i <= n;++i){44         p[i].d = get_num();45         p[i].a = get_num();46         if(p[i].a >= p[i].d){47             pluss[++now1].a = p[i].a;48             pluss[now1].b = p[i].d;49             pluss[now1].co = p[i].a - p[i].d;50         }51         else{52             dece[++now2].a = p[i].a;53             dece[now2].b = p[i].d;54             dece[now2].co = p[i].a - p[i].d;55         }56     }57     sort(pluss+1,pluss+now1+1,cmp1);58     sort(dece+1,dece+now2+1,cmp2);59     for(int i = 1;i <= now1;++i){60         sum = min(sum,ans - pluss[i].b);61         ans += pluss[i].co;62     }63     for(int i = 1;i <= now2;++i){64         sum = min(sum,ans - dece[i].b);65         ans += dece[i].co;66     }67     printf("%lld\n",abs(sum));68     return 0;69 }

If you have any comments on my blog, please contact me via my email. Thank you.

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.