C - Candles
题解
点燃的一定是连续的一段,枚举左端点即可
代码
#include <bits/stdc++.h>
#define enter putchar('\n')
#define space putchar(' ')
#define pii pair<int,int>
#define fi first
#define se second
#define mp make_pair
#define MAXN 1000005
#define mo 999999137
#define pb push_back
//#define ivorysi
using namespace std;
typedef long long int64;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) out(x / 10);
putchar('0' + x % 10);
}
int N,K;
int64 a[100005];
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
read(N);read(K);
for(int i = 1 ; i <= N ; ++i) {
read(a[i]);
}
int64 ans = 1e16;
for(int i = 1 ; i <= N - K + 1; ++i) {
int t = i + K - 1;
if(1LL * a[i] * a[t] > 0) {
ans = min(ans,max(abs(a[i]),abs(a[t])));
}
else ans = min(ans,min(-2 * a[i] + a[t],2 * a[t] - a[i]));
}
out(ans);enter;
return 0;
}#i