Ch round #53-Data Backup

Source: Internet
Author: User
Description

N office buildings are known to be located on the same street. You decided to pair the office buildings (two groups ). Each office building can be prepared by laying Network cables between the two buildings. However, the cost of Network cables is high. Local telecommunications companies can only provide you with K network cables, which means you can only arrange backups for K office buildings (or a total of 2 k office buildings. Any office building belongs to the only matching group (in other words, the two K office buildings must be different ). In addition, telecommunications companies are charged based on the length of Network cables (kilometers. Therefore, you need to select this k pair of office buildings to make the total length of the cable as short as possible. In other words, you need to select this k pair of office buildings, so that the distance between each pair of office buildings is as small as possible (total distance.

Input Format

The first line contains integers N and K. N represents the number of office buildings and K represents the number of available network cables.
Each line in the next n rows contains an integer s, indicating the distance from each office building to the street start point. These integers appear in ascending order.

Output Format

A positive integer that connects two K office buildings into a k pair of the minimum total length of the required network cable.

Sample Input
5 2134612
Sample output
4
Data scope and conventions
  • For 30% of data, n <= 20.
  • For 60% of data, n <= 10000.
  • For 100% of data, n <= 100000,1 <= k <= N/<= S <= 10 ^ 9.

Question:

I was shocked to understand the greedy practice of this question... Orz...

while(q.top().first!=len[q.top().second])q.pop();    int c=q.top().second,l=pre[c],r=nxt[c];    ans+=len[c];q.pop();    pre[nxt[c]=nxt[r]]=c;nxt[pre[c]=pre[l]]=c;    len[c]= l&&r?min(inf,len[l]+len[r]-len[c]):inf;    len[l]=len[r]=inf;    q.push(make_pair(len[c],c));

Here are the key statements.

Every greedy selection of the smallest distance may affect the upper and lower points. Then I will add a new node Len [l] + Len [R]-len [c] to prevent future regret.

What does it mean? I found that I got the point C and another point is better than the point l and the point R. At this time, I regret it and chose the point C that I joined later, the distance is exactly Len [l] + Len [R]

If the problem is not correct in the future, modify it again. The correctness is clear.

Orzzzzzzzzzzzzzzzzzzzzzz

Code:

 1 #include<cstdio> 2  3 #include<cstdlib> 4  5 #include<cmath> 6  7 #include<cstring> 8  9 #include<algorithm>10 11 #include<iostream>12 13 #include<vector>14 15 #include<map>16 17 #include<set>18 19 #include<queue>20 21 #include<string>22 23 #define inf 100000000024 25 #define maxn 100000+100026 27 #define maxm 500+10028 29 #define eps 1e-1030 31 #define ll long long32 33 #define pa pair<int,int>34 35 #define for0(i,n) for(int i=0;i<=(n);i++)36 37 #define for1(i,n) for(int i=1;i<=(n);i++)38 39 #define for2(i,x,y) for(int i=(x);i<=(y);i++)40 41 #define for3(i,x,y) for(int i=(x);i>=(y);i--)42 43 #define mod 100000000744 45 using namespace std;46 47 inline int read()48 49 {50 51     int x=0,f=1;char ch=getchar();52 53     while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}54 55     while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();}56 57     return x*f;58 59 }60 priority_queue<pa,vector<pa>,greater<pa> > q;61 int n,m,len[maxn],pre[maxn],nxt[maxn];62 63 int main()64 65 {66 67     freopen("input.txt","r",stdin);68 69     freopen("output.txt","w",stdout);70 71     n=read();m=read();int x=0,y;72     for1(i,n)73     {74         y=read();75         len[i]=y-x;pre[i]=i-1;nxt[i]=i+1;76         x=y;77     }78     pre[2]=0;79     nxt[n]=0;80     int ans=0;81     for2(i,2,n)q.push(make_pair(len[i],i));82     for1(i,m)83     {84         while(q.top().first!=len[q.top().second])q.pop();85         int c=q.top().second,l=pre[c],r=nxt[c];86         ans+=len[c];q.pop();87         pre[nxt[c]=nxt[r]]=c;nxt[pre[c]=pre[l]]=c;88         len[c]= l&&r?min(inf,len[l]+len[r]-len[c]):inf;89         len[l]=len[r]=inf;90         q.push(make_pair(len[c],c));91     }92     printf("%d\n",ans);93 94     return 0;95 96 }
View code

 

 

Ch round #53-Data Backup

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.