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

你可能感兴趣的文章
NN&DL4.7 Parameters vs Hyperparameters
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>