Codeforces Round #377 (Div. 2) D. Exams
Codeforces Round #377 (Div. 2) D. Exams
题意:给你n个考试科目编号1~n以及他们所需要的复习时间ai;(复习时间不一定要连续的,可以分开,只要复习够ai天就行了) 然后再给你m天,每天有一个值di; 其中,di==0代表这一天没有考试(所以是只能拿来复习的); 若di不为0,则di的值就代表第i天可以考编号为di的科目 ;(当然这一天也可以不考而拿来复习) 。 问你最少能在第几天考完所有科目,无解则输出-1。
题解:首先,先想想最暴力的方法:从第1天到第n天这样一天天地试,每次判断前k天能否考完所有科目(至于怎么判断前k天能否够考完所有科目下面会说)。 这样子的最坏情况的复杂度是O(n^2); 那么如果不要这样一天天地判断过去的话,要进行优化;那么,由于题目要问最小天数能考完所有科目,且天数都是升序的—这时,就可以用二分了。 二分能考完所有科目的天数,每次判断该天数能否考完。 那么问题就被简化了;原来问题是最小的天数要考完所有科目,,而现在就只需关心怎么判断用前k天时间能考完所有科目 这里用贪心的方法:建立个变量sum复习需要用到的天数 , 用第k天开始往前扫;如果di不为0(另外还要有个标记数组来标记该科目是否是第一次扫到),则将该科目所需要的复习天数加到sum上; 如果di为0,则将sum-- (前提是sum不为0)。 扫完后,用标记数组判断是否所有科目都考了且sum是否为0 ; 若sum不为0,说明复习天数是不够用的。 最后复杂度是为O(nlogn)的。
( ps: 可能有的人会觉得从后往前扫,只有遇到的科目第一次出现时才加入到sum中,但这些都是k天内各科目的最后"期限",虽然判断到用k天可行,但可能不是最优的(一开始自己就在这上面给坑了=_=)。 但是,因为上面已经有二分了,所以我们这后面只要关注在能不能用k天将所有科目考完,而不用理会它是不是最优的! )
下面是AC代码: 这一题的后台数据其实是有点水的,,如果发现代码有Bug的话欢迎各位提出 (o゜▽゜)o☆
/**
* @author geek1116
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <map>
#include <set>
const int inf=0x3f3f3f3f;
const double PI=acos(-1.0);
const double EPS=1e-;
const int MAXN=1e5;
using namespace std;
typedef long long ll;
typedef pair<int,int> P; int n,m;
int d[MAXN+];
int a[MAXN+];
int book[MAXN+]; //标记数组 bool judge(int x) //判断用前x天是否可行
{
memset(book,,sizeof(book));
int sum=,cnt=;
//
for(int i=x;i>=;i--)
{
if(d[i])
{
if(!book[d[i]]) sum+=a[d[i]],book[d[i]]=,cnt++;
else if(sum!=) sum--;
}
else if(sum!=)
sum--;
}
//
if(sum||cnt!=m) return false;
else return true;
}
int main()
{
//freopen("input.txt","r",stdin);
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) scanf("%d",&d[i]);
for(int i=;i<=m;i++) scanf("%d",&a[i]);
//
int l=,r=n,mid;
while(l<r) //二分结束天数
{
mid=(l+r)>>;
if(judge(mid)) r=mid;
else l=mid+;
}
//
if(judge(l)) printf("%d\n",l);
else if(judge(r)) printf("%d\n",r);
else printf("%d\n",-);
return ;
}
Codeforces Round #377 (Div. 2) D. Exams的更多相关文章
- Codeforces Round #377 (Div. 2) D. Exams(二分答案)
D. Exams Problem Description: Vasiliy has an exam period which will continue for n days. He has to p ...
- Codeforces Round #377 (Div. 2) D. Exams 二分
D. Exams time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...
- Codeforces Round #377 (Div. 2) D. Exams 贪心 + 简单模拟
http://codeforces.com/contest/732/problem/D 这题我发现很多人用二分答案,但是是不用的. 我们统计一个数值all表示要准备考试的所有日子和.+m(这些时间用来 ...
- Codeforces Round #274 (Div. 1) A. Exams 贪心
A. Exams Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/480/problem/A Des ...
- Codeforces Round #274 (Div. 2)-C. Exams
http://codeforces.com/contest/479/problem/C C. Exams time limit per test 1 second memory limit per t ...
- Codeforces Round #377 (Div. 2)
#include <iostream> #include <stdio.h> #include <string.h> using namespace std; in ...
- Codeforces Round #377 (Div. 2)D(二分)
题目链接:http://codeforces.com/contest/732/problem/D 题意: 在m天中要考k个课程, 数组a中有m个元素,表示第a[i]表示第i天可以进行哪门考试,若a[i ...
- Codeforces Round #377 (Div. 2) A B C D 水/贪心/贪心/二分
A. Buy a Shovel time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- Codeforces Round #377 (Div. 2) E. Sockets
http://codeforces.com/contest/732/problem/E 题目说得很清楚,每个电脑去插一个插座,然后要刚好的,电脑的power和sockets的值相同才行. 如果不同,还 ...
随机推荐
- 玩游戏(dfs)
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2566 #include <stdio.h ...
- 0423-mysql插入语句大全
/*注意: 1.字段和值要一一对应 2.值的数据类型是字段的数据类型 3.当输入的字段是表中全部字段时,字段可以省略不写: insert into login values ('zhangsan',‘ ...
- CMS内容管理系统 -- WorkSpace
- 从0开始学习BFC
为什么需要BFC? <style> .red { background: red; } .blue { background: #1890ff; } .green { background ...
- C# 导出word 表格代码
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- webApi上传服务,可重命名,可创建文件夹
webApi上传服务,根据FileName重命名,根据Path创建文件夹 /// <summary> /// 上传文件 /// </summary> /// <retur ...
- MyBatis动态条件、一对多、整合spring(二)
输入映射和输出映射 Mapper.xml映射文件定义了操作数据库的sql,每一个sql是一个statement,映射文件是mybatis的核心. parameterType输入类型 1.传递简单类型 ...
- # --with-http_sub_module模块
作用: http内容替换 语法 第一种语法: sub_filter string:要替换的内容 替换后的内容 这个模块只能替换第一个匹配的字符串,如果需要匹配全部替换,则用到下面的第三种语法配置 第二 ...
- 魅族和三星Galaxy 5.0webView 问题Android Crash Report - Native crash at /system/lib/libc.so caused by webvi
解决办法是当前activity 销毁的时候 webView.destroy(); hine: ConnectedState (when=-2ms what= arg1=!CMD_RSSI_POLL : ...
- 努比亚(nubia) V18 NX612J 解锁BootLoader 并刷入recovery ROOT
recovery制作来自绯色玻璃 努比亚(nubia) V18 NX612J 解锁BootLoader 并刷入recovery ROOT 工具下载链接:https://pan.baidu.com/s/ ...