codeforce #387 D. Winter Is Coming

D. Winter Is Coming
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

The winter in Berland lasts n days. For each day we know the forecast for the average air temperature that day.

Vasya has a new set of winter tires which allows him to drive safely no more than k days at any average air temperature. After k days of using it (regardless of the temperature of these days) the set of winter tires wears down and cannot be used more. It is not necessary that these k days form a continuous segment of days.

Before the first winter day Vasya still uses summer tires. It is possible to drive safely on summer tires any number of days when the average air temperature is non-negative. It is impossible to drive on summer tires at days when the average air temperature is negative.

Vasya can change summer tires to winter tires and vice versa at the beginning of any day.

Find the minimum number of times Vasya needs to change summer tires to winter tires and vice versa to drive safely during the winter. At the end of the winter the car can be with any set of tires.

Input

The first line contains two positive integers n and k (1 ≤ n ≤ 2·1050 ≤ k ≤ n) — the number of winter days and the number of days winter tires can be used. It is allowed to drive on winter tires at any temperature, but no more than k days in total.

The second line contains a sequence of n integers t1, t2, ..., tn ( - 20 ≤ ti ≤ 20) — the average air temperature in the i-th winter day.

Output

Print the minimum number of times Vasya has to change summer tires to winter tires and vice versa to drive safely during all winter. If it is impossible, print -1.

Examples
input
4 3
-5 20 -3 0
output
2
input
4 2
-5 20 -3 0
output
4
input
10 6
2 -5 1 3 0 0 -4 -3 1 0
output
3
Note

In the first example before the first winter day Vasya should change summer tires to winter tires, use it for three days, and then change winter tires to summer tires because he can drive safely with the winter tires for just three days. Thus, the total number of tires' changes equals two.

In the second example before the first winter day Vasya should change summer tires to winter tires, and then after the first winter day change winter tires to summer tires. After the second day it is necessary to change summer tires to winter tires again, and after the third day it is necessary to change winter tires to summer tires. Thus, the total number of tires' changes equals four.


       题意:一个冬季轮胎 一个夏季轮胎 夏季轮胎只能在温度>=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 
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<,greater >que3;//注意“>>”会被认为错误,
priority_queue,less >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  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<=0;t++) tmp++;//夏季轮胎
			if (t>n) last=tmp;
			else q.insert(tmp);
		}
	}

	multiset  :: 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;
}


你可能感兴趣的:(STL,set)