练习题(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):64Constraints
-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: 1The 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: 2There 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: 1Any 3 consecutive days have exactly one meeting within them.3)
9
2
{7, 4, 5, 6, 2}
Returns: 0The 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
- 练习题(2)
- 练习题2
- 练习题2
- CCNA 基础练习题(2)
- SQL Server练习题2
- SQL Server练习题2
- SQL Server练习题2
- SQL Server练习题2
- SQL Server练习题2
- SQL Server练习题2
- 练习题(2)
- SQL Server练习题2
- SQL Server练习题2
- SQL Server练习题2
- SQL Server练习题2
- SQL Server练习题2
- SQL Server练习题2
- SQL Server练习题2
- .Java的垃圾回收机制(GC)
- 把heritrix抓取的网页在eclipse里读进,然后写出文件时出现乱码
- update-rc.d使用
- ruby on rails 指导入门 http://ihower.tw/rails3/firststep.html
- 剖析网页从浏览器输入地址到加载完毕的全过程——建立连接
- 练习题(2)
- 并发管理器2——OPP的概念
- weblogic eclipse 运行web应用程序
- 三十分钟掌握STL
- adb client, adb server, adbd原理浅析(附带我的操作过程)
- Javascript 对象方式实现命名参数调用(下):自动包装偏函数
- 深入理解JavaScript中的prototype和call()的作用
- opencv 模板匹配目标检测
- MOOC程序设计期中考试1.1