#36622: 救我,我覺得已經很簡短了就輸入,一直找選大邊找小數字,輸出<


goodlogic (GoodLogic)

學校 : 中原大學
編號 : 236459
來源 : [140.135.40.12]
最後登入時間 :
2024-12-19 16:44:59
g277. 3. 幸運數字 -- 2021年9月APCS | From: [1.170.124.141] | 發表日期 : 2023-07-29 16:36

#include <iostream>

#include <stdio.h>

#include <stdlib.h>

#include <algorithm>

#define int long long

 

using namespace std;

 

int sumarr[300005];

int input[300005];

 

int FindMinIndex(int start, int theend){

int themin = input[start];

int out = start;

for (int i = start; i <= theend; i++) {

if (input[i] < themin){

themin = input[i];

out = i;

}

}

return out;

}

 

signed main()

{

int n;

cin >> n;

 

int i;

int min = 0;

// input

for (i = 0; i < n; i++){

scanf("%lld", &input[i]);

if (i==0) sumarr[i] = input[i];

if (i>0) sumarr[i] = sumarr[i-1] + input[i];

}

 

int themin = 0, themax = n-1;

int curr = FindMinIndex(themin, themax); // curr = mid

 

// select

while(themax != themin){

int left = sumarr[curr-1] - sumarr[themin] + input[themin];

int right = sumarr[themax] - sumarr[curr+1] + input[curr+1];

// cout << left << " " << right << endl;

if (left > right ){

themax = curr-1;

curr = FindMinIndex(themin, themax);

}

else{

themin = curr+1;

curr = FindMinIndex(themin, themax);

}

}

 

// output

printf("%lld", input[themax]);

}

 

 
ZeroJudge Forum