博客
关于我
Codeforces Round #377 Exams(二分)
阅读量:633 次
发布时间:2019-03-14

本文共 1205 字,大约阅读时间需要 4 分钟。

一开始想从数组尾部遍历到头部,不断更新每个考试的最小值,但是过程中无法处理0在前后的变换,放弃此思路。队友的提点,将答案二分查找,此中的答案是指:“以当前点为尾部是否可以满足题目条件”,如果不满足,其尾部必然在下一半,否则在上一段,直到确定。

判段函数挺简单的,我竟然墨迹半天,woc,心累,代码能力太差了。

#include 
#include
#include
#include
using namespace std;const int N = 1e5+10;int a[N],b[N];int v[N];int m;bool Judge(int n){ int sum,x,y,num; memset(v,0,sizeof(v)); x=y=num=0; for(int i=n;i>=1;i--) { if(!a[i]) { if(x>y) y++; } else { if(v[a[i]]) { if(x>y) y++; } else { num++; x+=b[a[i]]; v[a[i]]=true; } } if(x==y&&num==m) return true; } return false;}int main(){ int n; scanf("%d%d",&n,&m); for(int i = 1; i <= n; i++) scanf("%d",&a[i]); for(int i = 1; i <= m; i++) scanf("%d",&b[i]); int l = 1, r = n; int mid; while(l <= r) { mid = (l+r) >> 1; if(Judge(mid)) r = mid-1; else l = mid+1; } if(l > n) printf("-1\n"); else printf("%d\n",l); return 0;}

转载地址:http://dyaoz.baihongyu.com/

你可能感兴趣的文章
MySQL 索引的面试题总结
查看>>
mysql 索引类型以及创建
查看>>
MySQL 索引连环问题,你能答对几个?
查看>>
Mysql 索引问题集锦
查看>>
Mysql 纵表转换为横表
查看>>
mysql 编译安装 window篇
查看>>
mysql 网络目录_联机目录数据库
查看>>
MySQL 聚簇索引&&二级索引&&辅助索引
查看>>
Mysql 脏页 脏读 脏数据
查看>>
mysql 自增id和UUID做主键性能分析,及最优方案
查看>>
Mysql 自定义函数
查看>>
mysql 行转列 列转行
查看>>
Mysql 表分区
查看>>
mysql 表的操作
查看>>
mysql 视图,视图更新删除
查看>>
MySQL 触发器
查看>>
mysql 让所有IP访问数据库
查看>>
mysql 记录的增删改查
查看>>
MySQL 设置数据库的隔离级别
查看>>
MySQL 证明为什么用limit时,offset很大会影响性能
查看>>