水题 ;
最优解最多只会回头一次 所以依次判断就行了
链接 :https://atcoder.jp/contests/arc101/tasks/arc101_a
#include<bits/stdc++.h> #define pb push_back #define fi first #define se second #define io std::ios::sync_with_stdio(false) using namespace std; typedef long long ll; typedef pair<ll,ll> pii; const double pi=acos(-1); const ll P = 998244353, INF = 0x3f3f3f3f; ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} ll qpow(ll a,ll n){ll r=1%P;for (a%=P; n; a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;} const double eps=1e-5; ll lcm(ll a,ll b){return a*b/gcd(a,b);}; const int maxn = 1e5+10; int sum1[maxn],sum2[maxn]; int main(){ int n,k; cin>>n>>k; std::vector<int> ans1,ans2; for(int i=1;i<=n;i++) { int x; cin>>x; if(x<0)ans1.push_back(-x); else if(x>0) ans2.push_back(x); else k--; } reverse(ans1.begin(),ans1.end()); for(int i=0;i<ans2.size();i++) { sum1[i+1]=ans2[i]; } for(int i=0;i<ans1.size();i++) { sum2[i+1]=ans1[i]; } int _min=1e9+10; if(k==0) _min=0; for(int i=1;i<=ans2.size();i++){ if(k-i>ans1.size()) continue; _min=min(sum1[i]+sum2[k-i]+min(sum1[i],sum2[k-i]),_min); } for(int i=1;i<=ans1.size();i++) {if(k-i>ans2.size()) continue; _min=min(sum2[i]+sum1[k-i]+min(sum2[i],sum1[k-i]),_min); } cout<<_min<<endl; return 0; }