练习题(2)

3 篇文章 0 订阅
ROOM 676
SRM 477 DIV 2

Problem Statement

 The king and the queen want to go on a vacation together. Since the queen seldom asks for anything, the king is more than willing to reschedule his meetings if they conflict with the vacation.The vacation must last for K contiguous days, and must lie between day 1 and dayN inclusive. You are given vector <int> workingDays, where each element is a day on which the king has a meeting scheduled. The king will have at most one meeting scheduled per day. Return the minimum number of meetings that must be rescheduled so that the king and the queen can have a happy vacation.

Definition

 
Class:VacationTime
Method:bestSchedule
Parameters:int, int, vector <int>
Returns:int
Method signature:int bestSchedule(int N, int K, vector <int> workingDays)
(be sure your method is public)

Limits

 
Time limit (s):2.000
Memory limit (MB):64

Constraints

-N will be between 1 and 1000, inclusive.
-K will be between 1 and N, inclusive.
-workingDays will contain between 1 and 50 elements, inclusive.
-Each element of workingDays will be between 1 and N, inclusive.
-Elements of workingDays will be distinct.

Examples

0) 
 
3
3
{2}
Returns: 1
The vacation must last from day 1 to day 3. Hence, the meeting on day 2 must be rescheduled.
1) 
 
4
3
{3, 1, 2}
Returns: 2
There are two options for the vacation: days 1 to 3, or days 2 to 4. The first option would require 3 meetings to be rescheduled, and the second requires 2 meetings to be rescheduled.
2) 
 
5
3
{4, 1}
Returns: 1
Any 3 consecutive days have exactly one meeting within them.
3) 
 
9
2
{7, 4, 5, 6, 2}
Returns: 0
The king will not have to reschedule any meetings, but the queen will have to wait until day 8 for the vacation to start.
4) 
 
1000
513
{808, 459, 792, 863, 715, 70, 336, 731}
Returns: 2


解法提示: 
动态规划
f( x + 1) = min(f(x), last_k(x+1)) 
产生最小替换数的序列,要么出现在前x个元素中,要么出现在以 x + 1元素结尾的长度为K的序列中

#include<iostream>
#include<vector>
#include<set>
using namespace std;
int min(int a,int b){
	if(a<b){
		return a;
	}else{

		return b;
	}

}
class VacationTime{
	public:
		int bestSchedule(int N, int K, vector<int> wd){
			int res =10000;
			int temp=0;
			set<int> md(wd.begin(),wd.end());
			cout<<md.size()<<endl;
			for(int i=1;i<K+1;i++){
				if(md.count(i)){
					temp++;
				}
				cout<<" "<<i<<" "<<temp<<endl;
			}	
			res  = temp;
			cout<<"res "<<res<<endl;
			for(int i=2;i<=N+1-K;i++){

				if(md.count(i-1)){
					temp--;
				}
				if(md.count(i + K - 1)){

					temp++;
				}
				res = min(res,temp); 

			}
			return res;

		}

};

int main(){
	int N  = 5;
	int K = 3;
	int a[]={4,1};
	vector<int> md(a,a+2);
	VacationTime v;
	cout<<v.bestSchedule(N,K,md)<<endl;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值