747C - Servers

**

D. Winter Is Coming

**
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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·105, 0 ≤ 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.

题意:
有 n 个服务器,有 q 个问题,接下来 q 行会出现三个数, t (在时刻 t 处出现问题)、k(需要 k 个服务器处理问题) 、d(需要处理 d 秒) ,要求求出是否能在 t 时刻执行问题,若能,输出服务器编号之和,否则输出 -1 。

以下是 ac 代码思路:
首先要清楚题目有多组数据,且数据之间有关联性,所以存在一个数据状态更新的问题。接下来就是简单的模拟。建立一个数组 a[] 以记录服务器可用的时间的时刻,即在 x 秒的时候及以后可用。然后判断,更新。

以下是 ac 代码实现:

#include <cstdio>
int a[101];

int main()
{
    int n,q;
    scanf ("%d%d",&n,&q);
    for (int j=1;j<=q;j++)
    {
        int t,k,d;
        scanf ("%d%d%d",&t,&k,&d);
        int t1=t+d;//记录下一轮个别服务器可用的时间坐标
        int cnt=0,sum=0;
        for (int i=1;i<=n;i++)
        {
            if (a[i]<=t)//表示当前a[I]可用
            {
                cnt++,sum+=i;
                if (cnt>=k) break;//数够服务器数跳出
            }
        }

        if (cnt<k) printf ("-1\n");
        else
        {
            for (int i=1;i<=n;i++)//对下轮服务器数据进行更新
            {
                if (a[i]<=t&&cnt>0)
                {
                    a[i]=t1;
                    cnt--;
                }
            }
            printf ("%d\n",sum);
        }
    }
}

写在最后:这题应该算一个简单的模拟题,需要抓住时间这个点,对服务器可用的时刻进行记录,然后判断,更新。比较绕不过弯来的是,如何对服务器状态进行记录,以往我对于状态的记录判断,都会记一个 vis 数组,可 vis 数组一般为 bool 型,因为这不具有时间的属性。所以关键句就在于 int t1=t+d; 及对数组的更新。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值