codeforce 732D . Exams

D . Exams

题意:一共有n天 m个考试 di表示第i天可以进行第m个考试 ai 表示第i个考试需要准备ai天才能通过考 问最少需要多少天才能通过所有考试

思路: 二分时间 每一次判断一下能否通过所有考试 得到最优解  每次二分到第x天时 从后面开始计算 尽量在后面的时间里考试 留更多的时间给前面的考试(比如第一次考试,如果可以在第5天和第7天考 如果第5天可以通过考试(即准备的时间大于等于ai),那么在第7天也一定可以通过,但是如果在第7天可以通过,在第5天不一定可以通过,所以一律选择时间靠后的那天进行考试)

AC代码:

#include "iostream"
#include "string.h"
#include "stack"
#include "queue"
#include "string"
#include "vector"
#include "set"
#include "map"
#include "algorithm"
#include "stdio.h"
#include "math.h"
#define ll long long
#define bug(x) cout<<x<<" "<<"UUUUU"<<endl;
#define mem(a) memset(a,0,sizeof(a))
using namespace std;
int n,m,a[100010],d[100010];
set<int> se;
bool solve(int mid){
    se.clear();
    for(int i=mid; i>=1; i--){
         if(d[i] && !se.count(d[i])){
             if(i>a[d[i]]){
               se.insert(d[i]);
             }
         }
    }
    return se.size()==m;
}
int main(){
    mem(a),mem(d);
    scanf("%d%d",&n,&m);
    int l=0,r=n,ans=0;
    for(int i=1; i<=n; ++i){
        scanf("%d",&d[i]);
    }
    for(int i=1; i<=m; ++i){
        scanf("%d",&a[i]);
        l+=a[i]+1;
    }
    while(l<=r){
        int mid=l+r>>1;
        if(solve(mid)) ans=mid,r=mid-1;
        else l=mid+1;
    }
    printf("%d\n",ans==0?-1:ans);
    return 0;
}
时间: 2024-08-11 07:43:03

codeforce 732D . Exams的相关文章

CodeForces 732D Exams

二分. 二分答案,然后贪心验证一下即可. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include<map> #include<set> #include&l

Exams(二分求左界+贪心)

用力戳我直达原题:D - Exams 题意: 有N天和M门课程. 接下来给你N天的行为,0表示这一天只能预习,[1,m]表示这一天可以考这门课(当然这一天你也可以选择不考或者预习). 接下来给你M个数cost[i],代表第i门课需要预习cost[i]天才能PASS. 求从第一天起算,最少需要几天才能PASS所有功课,如果N天都PASS不了,则输出-1. 做法: 1.先判断用N天能否PASS,不能就输出-1. 2.low = m, high = n.求左界. 3-1 judge的时候,一门课肯定越

Ural 1091 Tmutarakan Exams

Tmutarakan Exams Time Limit: 1000ms Memory Limit: 16384KB This problem will be judged on Ural. Original ID: 109164-bit integer IO format: %lld      Java class name: (Any) University of New Tmutarakan trains the first-class specialists in mental arith

CodeForce 448C 木片填涂问题

题目大意:有多片木片需要填涂,可以每次横着涂一行,也可以一次涂一列,当然你涂一行时遇到中间长度不够高的木片,填涂到此中断 这题目运用dfs能更容易的解出,虽然还是十分不容易理解 1 #include <iostream> 2 3 using namespace std; 4 5 #define N 5010 6 int a[N],n; 7 8 int Min(int c,int d) 9 { 10 return c<d?c:d; 11 } 12 int dfs(int c,int d,i

Codeforces Round #274 (Div. 2) C. Exams

Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly n exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several

Codeforce 水题报告(2)

又水了一发Codeforce ,这次继续发发题解顺便给自己PKUSC攒攒人品吧 CodeForces 438C:The Child and Polygon: 描述:给出一个多边形,求三角剖分的方案数(n<=200). 首先很明显可能是区间dp,我们可以记f[i][j]为从i到j的这个多边形的三角剖分数,那么f[i][j]=f[i][k]*f[j][k]*(i,j,k是否为1个合格的三角形) CodeForces 438D:The Child and Sequence 描述:给一个序列,要求支持区

codeforce 285 div2 D 题解

codeforce 285 div2 D 题解 说明 这道题目是看了思路分析才知道的,关键问题在于数据量过大,需要快速检索的同时不能辅助空间过大. 因此利用了下面3种方法结合解决该问题 康拓展开与逆康拓展开 树状数组 二分查找 代码 /** * @brief Codeforces Round #285 (Div. 2) d * @file d.cpp * @author mianma * @created 2015/01/27 18:18 * @edited 2015/01/29 22:45 *

codeforce Pashmak and Buses(dfs枚举)

1 /* 2 题意:n个同学,k个车, 取旅游d天! 3 要求所有的学生没有两个或者两个以上的在同一辆车上共同带d天! 输出可行的方案! 4 5 对于d行n列的矩阵,第i行第j列表示的是第i天第j个同学所在的车号! 6 也就是保证所有行不全相同,即每一列都是不相同的! 7 如果每一列都不相同就是表示第j个同学(第j列)在这d天中不会和其他同学(列)在这d天中 都在同一辆车中! 8 9 思路:对于每一列我们枚举d天该学生所在的车号!它的下一列只保证有一个元素和它不同就行了!依次下去! 10 11

2014 Super Training #3 H Tmutarakan Exams --容斥原理

原题: URAL 1091  http://acm.timus.ru/problem.aspx?space=1&num=1091 题意:要求找出K个不同的数字使他们有一个大于1的公约数,且所有的数字都不能大于一个指定的数字S. 解法:可以考虑每个S内的素数,此素数和它的所有倍数构成一个集合,则可以在这些集合中任意去k个元素,C(n,k)即为这种情况下的方法种数,比如K = 3,S = 10, 则可以形成3个集合: {2,4,6,8,10} , {3,6,9}, {5,10} ,第一个集合C(5,