Description
一個陽光明媚的周末,二哥出去游山玩水,然而粗心的二哥在路上把錢包弄丟了。傍晚時分二哥來到了一家小旅店,他翻便全身的口袋也沒翻著多少錢,而他身上唯一值錢的就是一條漂亮的金鏈。這條金鏈散發著奇異的光澤,據說戴上它能保佑考試門門不掛,RP++。好心的老板很同情二哥的遭遇,同意二哥用這條金鏈來結帳。雖然二哥很舍不得這條金鏈,但是他必須用它來付一晚上的房錢了。
金鏈是環狀的,一共有 N 節,老板的要價是 K 節。隨便取下其中 K 節自然沒問題,然而金鏈上每一節的 RP 值其實并不一樣,有高有低,二哥自己非常清楚。另外二哥并不希望把整個金鏈都拆散了,他只愿意在這條環形的金鏈上切兩刀,從而切下一段恰好為 K 節的金鏈給老板。因為 RP 值越高的節越稀有,因此他希望給老板的金鏈上最高的 RP 值最小。
Input Format
第一行兩個整數 N 和 K,表示金項鏈有 N 節,老板要價 K 節。
第二行用空格隔開的N個正整數 a1...aN ,表示每一節金鏈的價值為多少。
Output Format
輸出一個整數,表示二哥給老板的金鏈上最高的 RP 值最小多少。
Sample Input
5 2
1 2 3 4 5
Sample Output
2
Sample Input
6 3
1 4 7 2 8 3
Sample Output
4
說明
對40%的數據,3≤N≤200
;
對70%的數據,3≤N≤20000
;
對100%的數據,3≤N≤200000
, 0<ai≤109
。
數據規模較大,建議用scanf("%d", &a[i]);來讀數據。
?
?
?
?
?
#include <iostream>#include <stdio.h>#include <algorithm>#include <vector>using namespace std;int min_value(int chain[], int n, int k){vector<int> vchain(chain,chain+n);vector<int>::iterator start = vchain.begin();int res = 10000000001, sub_max;while(start+k<=vchain.end()){vector<int>::iterator iter = max_element(start,start+k);sub_max = *iter;start = iter + 1;res = min(sub_max, res);//cout<<*start<<" "<<sub_max<<endl; }return res;}int main() {int n,k;cin>>n>>k;if(k>n)return 0;int chain[n];for(int i=0;i<n;i++){scanf("%d",&chain[i]);}int elem_max_1 = min_value(chain,n,k);int chain_cir[2*(k-1)];int j = 0;for(int c = n-k+1;c<n;c++){chain_cir[j++] = chain[c];}for(int s = 0;s<k-1;s++){chain_cir[j++] = chain[s];}int elem_max_2 = min_value(chain_cir, 2*(k-1), k);cout<<min(elem_max_1,elem_max_2);return 0;}
?