博客
关于我
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/

你可能感兴趣的文章
Netty源码—6.ByteBuf原理一
查看>>
Netty源码—6.ByteBuf原理二
查看>>
Netty源码—7.ByteBuf原理三
查看>>
Netty源码—7.ByteBuf原理四
查看>>
Netty源码—8.编解码原理一
查看>>
Netty源码—8.编解码原理二
查看>>
Netty源码解读
查看>>
netty的HelloWorld演示
查看>>
Netty的Socket编程详解-搭建服务端与客户端并进行数据传输
查看>>
Netty的网络框架差点让我一夜秃头,哭了
查看>>
Netty相关
查看>>
Netty简介
查看>>
Netty线程模型理解
查看>>
netty解决tcp粘包和拆包问题
查看>>
Netty速成:基础+入门+中级+高级+源码架构+行业应用
查看>>
Netty遇到TCP发送缓冲区满了 写半包操作该如何处理
查看>>
netty(1):NIO 基础之三大组件和ByteBuffer
查看>>
Netty:ChannelPipeline和ChannelHandler为什么会鬼混在一起?
查看>>
Netty:原理架构解析
查看>>
Network Dissection:Quantifying Interpretability of Deep Visual Representations(深层视觉表征的量化解释)
查看>>