练习题(2)

来源:互联网 发布:论坛推广软件 编辑:程序博客网 时间:2024/05/17 07:32
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 forK 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:VacationTimeMethod:bestScheduleParameters:int, int, vector <int>Returns:intMethod signature:int bestSchedule(int N, int K, vector <int> workingDays)(be sure your method is public)

Limits

 Time limit (s):2.000Memory 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;}






0 0
原创粉丝点击