1,Borrow Books
[Problem description]
Dilhao has a total of N textbooks, each of which has a difficulty value. Each time he answers a question, he will pick two textbooks for reference. If the difficulty of the two books is different, the more complicated the questions that result from dilhao, that is,The complexity of a question equals the absolute value of the difficulty difference between the two books.
This is the turn of ldxxx. He wants to ask about the problem by using the m book. dilhao wants to know if ldxxx givesChoose the two books with the least difficulty in the m book.Question, then the question from ldxxxMaximum complexityWhat is it?
[Input format]
The first line is N and M.
In the next n rows, each line has an integer "AI", indicating the difficulty of the I-th book.
[Input format]
An integer is the maximum complexity of a question produced by ldxxx.
[Input example]
6 3
5
7
1
17
13
10
[Output example]
7
[Example]
Dilhao gave ldxxx three books with difficulty 1, 10, and 17, ldxxx selected two books with difficulty 10 and 17, and the complexity of the question was 7;
If dilhao provides any other three books, the minimum difficulty between the two books is less than 7, so the greatest complexity of ldxxx is 7.
[Data description]
For 30% of data: 2 <= n <= 20;
For 60% of data: 2 <= n <= 1000;
For 100% of data: 2 <= n <= 100000, 2 <= m <= N, 0 <= AI <= 1000000000.
Ideas:
Question at a glance
For the original array, we can divide it
Then, give two answers.
Scan the original sequence for verification.
Code:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#define rii register int i#define rij register int jusing namespace std;int n,m,x[100005],cha[100005];bool cmp(int lk,int kl){ return lk<kl;}bool check(int val){ int jsq=0,ch=0; for(rii=1;i<n;i++) { ch+=cha[i]; if(ch>=val) { jsq++,ch=0; } } if(jsq>=m-1) { return true; } return false;}int main(){ freopen("margin.in","r",stdin); freopen("margin.out","w",stdout); scanf("%d%d",&n,&m); int r=0,l=0; for(rii=1;i<=n;i++) { scanf("%d",&x[i]); r=max(r,x[i]); } sort(x+1,x+n+1,cmp); for(rii=1;i<n;i++) { cha[i]=x[i+1]-x[i]; } while(l<=r) { if(r-l==1) { if(check(r)==true) { l=r; } break; } int mid=(l+r)/2; if(check(mid)==true) { l=mid; } else { r=mid; } } cout<<l;}
T1 81029noip simulation competition T1