"Problem Solving Report" Math

Source: Internet
Author: User

= = It was supposed to have been sent yesterday, just off the net ....

MATH

"Title description"
Little X is doing his maths homework, but the homework is too difficult. The topic is this:
1. Given an n number of series V.
2. You can remove the number of k from the sequence and define the post-removal column as V '.
3. Define m as the maximum of the difference between any two numbers in V ', and M is the minimum value of the difference of any two number in V '.
4. Please choose to delete the number of K, making m+m the smallest.
Little X's maths is so bad that he can only turn to you for help.

Input
The first line is two integers n and K.
The second row n integer vi.

Output
An integer of one line, and for the smallest m+m.

"Sample Input"
5 2
-3-2 3 8 6

"Sample Output"
7

"Sample Interpretation"
Delete-3 and-2 to get V ' ={3,6,8},m=5,m=2,m+m=7.

"Data Range"
For 60% data: 3≤n≤2 000
For 100% of data:
3≤n≤200 000
1≤k≤n-2
-5 000 000≤vi≤5 000 000

For this problem, at first glance very round ... It's actually pretty much around, but it's simple.

First, we sort the original sequence, after the order, the enumeration length is (n-k) the interval to find the difference between the maximum and the minimum difference (= =), and then da lei to find the smallest.

Since we can prove that the optimal sequence after the deletion of the element is sequential, it must be sequential, so only the enumeration is required. (Proof slightly)

But at the end of the interval the minimum difference is to use the multiplier St algorithm, otherwise it will time out ...

The code is as follows (style ugly don't mind)

#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
using namespace Std;
Ifstream fin ("math.in");
Ofstream fout ("Math.out");
Long Long int beiz[200005][25][2]={0};//????
Long long int xl[200005]={0};//??
Long long int ca[200005]={0};//i?? I+1????
int c2[20]={1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288};
Long long int num=0,cut=0;
bool px (Long long int a,long long int b);
void Beiz (int cs);
Long Long int Findi (long long int begin,long long int end);
int main (void)
{
fin>>num>>cut;
for (int i=1;i<=num;i++) fin>>xl[i];
Sort (xl+1,xl+num+1,px);
for (int i=1;i<=num;i++)
{
Ca[i]=abs (Xl[i+1]-xl[i]);
Beiz[i][0][0]=ca[i];
beiz[i][0][1]=i+1;
}
Beiz (1);
Long long int rest=num-cut-1;
Long long int ans=0x7fffffffffffffffll,tot=0;
for (int i=1;i<=cut+1;i++)
{
if (i+rest>num) break;
Tot=abs (Xl[i+rest]-xl[i]) +findi (i,i+rest-1);
Ans=min (Tot,ans);
}
fout<<ans;
return 0;
}

bool px (Long long int a,long long int b)
{
if (a<b) return 1;
return 0;
}

void Beiz (int cs)
{
if (cs==20) return;
for (int i=1;i<=num;i++)
{
BEIZ[I][CS][1]=BEIZ[BEIZ[I][CS-1][1]][CS-1][1];
Beiz[i][cs][0]=min (Beiz[beiz[i][cs-1][1]][cs-1][0],beiz[i][cs-1][0]);
}
Beiz (cs+1);
}

Long Long int Findi (long long int begin,long long int end)
{
Long long int l1=0,l2=0,mid=0;
Double K=log ((double) (End-begin))/log ((double) 2);
mid= (int) K;
L1=BEIZ[BEGIN][MID][0];
L2=BEIZ[END-C2[MID]+1][MID][0];
return min (L1,L2);
}

"Problem Solving Report" Math

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.