D. Winter Is Coming
time limit per test:2 seconds memory limit per test:256 megabytes
https://codeforces.com/problemset/problem/747/D
统计并对负温度之间的正温度数目进行排序,优先选择数目少的判断是否更换,out-2。
代码中第一次统计的是从第一个温度到第一个负温度中间的正温度数量,在排序时不做考虑。
需要在最后特别判断最后一个负温度与最后一个温度之间的间隔数,如果满足条件,out-1。
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,k;
int c=0;
cin>>n>>k;
vector<int> input(n);
for(int i=0;i<n;i++){
cin>>input[i];
if(input[i]<0){
c++;
}
}
if(c>k){
cout<<-1;
return 0;
}
vector<int> save;
int work=0;
int out=0;
for(int i=0;i<n;i++){
if(input[i]<0){
out+=2;
save.push_back(i-work);
do{
i++;
}while(i<n && input[i]<0);
work=i;
}
}
if(save.size()>1)
sort(save.begin()+1,save.end());
int temp;
for(int i=1;i<save.size();i++){
temp=c+save[i];
if(temp<=k){
out-=2;
c=temp;
}
else
break;
}
if(work && c+n-work<=k)
out--;
cout<<out<<endl;
return 0;
}