【解题报告】Math

= =本来昨天就该发的,只是断网……。

MATH
 
【题目描述】
小 x正在做他的数学作业,可是作业实在太难了。题目是这样的:
1.给定一个含有N个数的数列 V。
2.你可以从数列中恰好移除 K个数,定义移除后的数列为 V’。
3.定义M为V’中任意两个数的差的最大值,m为 V’中任意两个数的差的最小值。
4.请你选择删去的K 个数,使得M+m最小。
小 x的数学十分之差,于是他只能向你求助了。
 
【输入】
第一行两个整数 N和K。
第二行N个整数Vi。
 
【输出】
一行一个整数,为最小的M+m的和。
 
【样例输入】
5 2
-3 -2 3 8 6
 
【样例输出】
7
 
【样例解释】
删去-3 和-2,得到 V’={3,6,8},M=5,m=2,M+m=7。
 
【数据范围】
对于60%的数据:3 ≤ N ≤ 2 000
对于100%的数据:
3 ≤ N ≤ 200 000
1 ≤ K ≤ N - 2
-5 000 000 ≤Vi ≤ 5 000 000

对于这道题,乍一看很绕……实际也很绕,但是做法很简单。

首先,我们对原序列排序,排了序之后,枚举长度为(n-k)的区间求最大差和最小差的差(= =),然后打擂求出最小即可。

因为我们可以证明,删去元素后得到的最优序列排了序之后一定是连续的,所以只需要枚举就可以了。(证明略)

但是最后求区间最小差的时候要用倍增ST算法,不然会超时……

代码如下(风格丑别在意)

#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);
}

时间: 2024-08-11 14:27:04

【解题报告】Math的相关文章

ACdream 1203 - KIDx&#39;s Triangle(解题报告)

KIDx's Triangle Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Submit Statistic Next Problem Problem Description One day, KIDx solved a math problem for middle students in seconds! And than he created this problem. N

pat解题报告【1078】

1078. Hashing (25) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task of this problem is simple: insert a sequence of distinct positive integers into a hash table, and output the positions of the input numbers.  The hash fun

[LeetCode]Longest Valid Parentheses, 解题报告

题目 Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example i

【LeetCode】3Sum Closest 解题报告

[题目] Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {

codeforces#254DIV2解题报告

今天简直大爆发啊...吃了顿烧烤居然这么管事.....本弱渣居然做出来了3道,而且B题是我第一次在CF中用到算法..(以前最多也就是贪心...). 题目地址:codeforces#225 A题: 水题..不解释..5分钟1Y. 代码如下: #include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include

【LeetCode】Insert Interval 解题报告

[题目] Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1: Given intervals [1,3],[6,9], insert and m

解题报告 之 HDU5334 Virtual Participation

解题报告 之 HDU5334 Virtual Participation Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he asks rikka to have some practice on codeforces. Then she opens the problem B: Given an integer , she needs to come up wit

【LeetCode】3Sum Closest 解题报告 (Java)

[题目] Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {

2020-3-14 acm训练联盟周赛Preliminaries for Benelux Algorithm Programming Contest 2019 解题报告+补题报告

2020-3-15比赛解题报告+2020-3-8—2020-3-15的补题报告 2020-3-15比赛题解 训练联盟周赛Preliminaries for Benelux Algorithm Programming Contest 2019  A建筑(模拟) 耗时:3ms 244KB 建筑 你哥哥在最近的建筑问题突破大会上获得了一个奖项 并获得了千载难逢的重新设计城市中心的机会 他最喜欢的城市奈梅根.由于城市布局中最引人注目的部分是天际线, 你的兄弟已经开始为他想要北方和东方的天际线画一些想法

解题报告 之 POJ3057 Evacuation

解题报告 之 POJ3057 Evacuation Description Fires can be disastrous, especially when a fire breaks out in a room that is completely filled with people. Rooms usually have a couple of exits and emergency exits, but with everyone rushing out at the same time