题意:一个冬季轮胎 一个夏季轮胎 夏季轮胎只能在温度>=0的天气使用 但可以用无限次 冬季轮胎可以在任何温度下使用 但有天数和数量限制, 问最少换多少次轮胎。
下面给出代码 :
/*
_...---.._
,' ~~"--..
/ ~"-._
/ ~-.
/ . `-.
\ -.\ `-.
\ ~-. `-.
,-~~\ ~.
/ \ `.
. \ `.
| \ .
| \ \
. `. \
\ \
` `. \
` \. \
` \`. \
. \ -. \
` -. \
. ` - \ .
` \ ~- \
` . ~. \
. \ -_ \
` - \
. | ~. \
` | \ \
. | \ \
` | `. \
` ` \ \
. . `. `.
` : \ `.
\ ` \ `.
\ . `. `~~-.
\ : ` \ \
. . \ : `.\
` : \ | | .
\ . \ | |
\ : \ ` | `
. . | |_ .
` `. ` ` | ~.;
\ `. . . .
. `. ` ` `
`. `._. \ `.\
` < \ `. | .
` ` : ` | |
` \ ` | |
`. | \ : .' |
"Are you crying? " ` | \ `_-' |
"It's only the rain." : | | | : ;
"The rain already stopped."` ; |~-.| : '
"Devils never cry." : \ | ` ,
` \` : '
: \` `_/
` .\ "For we have none. Our enemy shall fall."
` ` \ "As we apprise. To claim our fate."
\ | : "Now and forever. "
\ .' : "We'll be together."
: : "In love and in hate"
| .'
| : "They will see. We'll fight until eternity. "
| ' "Come with me.We'll stand and fight together."
| / "Through our strength We'll make a better day. "
`_.' "Tomorrow we shall never surrender."
sao xin*/
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define INF 0x3f3f3f3
#define pi acos(-1)
//const LL INF = 1e15;
const int maxn=1e3+5;
const int maxx=1e5+5;
//const double q = (1 + sqrt(5.0)) / 2.0; // 黄金分割数
/*
std::hex <<16进制 cin>>std::hex>>a>>std::hex>>b
cout.setf(ios::uppercase);cout<<std::hex<<c<<endl;
//f[i]=(i-1)*(f[i-1]+f[i-2]); 错排
priority_queue<int,vector<int>,greater<int> >que3;//注意“>>”会被认为错误,
priority_queue<int,vector<int>,less<int> >que4;最大值优先
//str tmp vis val cnt 2486
struct point {
int id;
int ed;
bool operator < (const point &a) const {
return ed > a.ed;//结束时间早的优先级高
}
} p;
*/
int n,m,ans,sum,last;
int a[300001];
multiset <int> q;
int main()
{
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++) scanf("%d",&a[i]);
int t;
for (t=1;t<=n&&a[t]>=0;t++); last=-1;//夏季轮胎可以用多少天
while (t<=n)
{
int tmp=0; ans++;
//cout<<ans<<endl;
if (a[t]<0)
{
for (;t<=n&&a[t]<0;t++) tmp++;//冬季轮胎
m-=tmp;
} else {
for (;t<=n&&a[t]>=0;t++) tmp++;//夏季轮胎
if (t>n) last=tmp;
else q.insert(tmp);
}
}
multiset <int> :: iterator it;
if (m<0) printf("-1\n");
else {
while (!q.empty())//当set里面还有元素的时候进去判断你使用的夏天的轮胎的天数是否在冬季能使用的天数之内
{
if (*q.begin()<=m)
{
ans-=2; m-=*q.begin();//ans是存的换轮胎的次数 如果能就就减少两次换轮胎次数 然后冬季轮胎寿命减少那么多次
q.erase(q.begin());//然后循环到不能减少换胎次数
} else break;
}
if (last!=-1&&last<=m) ans--;//last记录的最后一程 最后可能为夏天轮胎能走的一程 然后如果那一程的天数小于轮胎寿命 那么就少换一次轮胎直接开过去
printf("%d\n",ans);
}
return 0;
}