BestCoder round#3 1001 && Hdu 4907
来源:互联网 发布:java解析syslog协议 编辑:程序博客网 时间:2024/05/18 01:54
题意:有一张时间表里面给你N个时间 这些时间是你需要执行任务的 现有M次询问 每次都给你一个时间Q 问在与时间表不冲突的情况下 在Q这个时间加进去的任务最早会被什么时候执行。
可能会忽略掉Q比所有时间表时间小或大的情况 EX:现在时间表上有 3 4 5 6 Q如果是1 就 应该输出 1 Q是20 就应该输出20
比赛的时候是用二分做的。感觉还是不行啊。想的有点复杂了
#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<iostream>#include<map>#include<set>#include<queue>#include<list>#include<stack>#include<vector>#include<math.h>#include<stdlib.h>using namespace std;#define ll long long #define mem(a) memset(a,0,sizeof(a))#define CLR(a, b) memset(a, b, sizeof(a))#define INF 0x3f3f3fint task[500000];int time[500000];bool falg[500000];int main(){int t;scanf("%d",&t);while(t--){int n,m;int k = 0;mem(falg);scanf("%d%d",&n,&m);for(int i = 0;i < n;i++){scanf("%d",&task[i]);falg[task[i]] = true;}sort(task,task+n);for(int i = task[0] ;i <= task[n-1] + 1;i++){if(falg[i] == false) time[k++] = i;}sort(time,time+k);while(m--){int q;scanf("%d",&q);if(q < task[0]) printf("%d\n",q);//是否在区间左侧else if (q == task[n - 1]) printf("%d\n",task[n - 1] + 1); else if (q > task[n - 1]) printf("%d\n",q);//是否在区间右侧else {//如果询问的时间在现有的时间表区间内的话if(q < time[0]) printf("%d\n",time[0]); //排除时间表的时间都是连续时间//应该输出时间表最大时间+1的情况else{int low = 0,mid;int half,len;len = k;while (len > 0){ //相当于lower_bound的实现half = len >> 1;mid = low + half;if (time[mid] < q){ //向右序列找low = mid + 1;len = len - half - 1;}else len = half; //向左序列找}printf("%d\n",time[low]);}}}}return 0;}
其实完全可以用hash实现 hash函数为 f(x) = x 倒着预处理一下 直接O(2*n + m)就能实现
#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<iostream>#include<map>#include<set>#include<queue>#include<list>#include<stack>#include<vector>#include<math.h>#include<stdlib.h>using namespace std;#define ll unsigned long long#define mem(a) memset(a,0,sizeof(a))#define CLR(a, b) memset(a, b, sizeof(a))int time[200005];int main(){int cas;scanf("%d",&cas);while(cas --){int n,m;CLR(time,-1);scanf("%d%d",&n,&m);for(int i = 0;i < n;i++){int tmp;scanf("%d",&tmp);time[tmp] = tmp;}int next = 0;for(int i = 200005;i > 0;i--){if(time[i] == -1) next = i;time[i] = next;}while(m--){int q;scanf("%d",&q);printf("%d\n",time[q]);}}return 0;}
0 0
- BestCoder round#3 1001 && Hdu 4907
- [BestCoder] Round #3 1001、1002 | HDU 4907、4908
- BestCoder Round #3 1001 && HDU 4907 Task schedule (预处理)
- HDU 4907 Task schedule(BestCoder Round #3 )
- HDU BestCoder Round #48 1001
- HDU 4907 - Task schedule(BestCoder Round #3 简单DP)
- BestCoder Round #3 A hdu 4907 Task schedule
- [BestCoder Round #3] hdu 4908 BestCoder Sequence (计数)
- hdu 4908 BestCoder Sequence && BestCoder Round #3 1002
- HDU 4908 BestCoder Sequence——BestCoder Round #3
- hdu 4989 Summary(BestCoder Round #8 1001)
- BestCoder Round #16 1001(hdu 5086)
- HDU 5280 BestCoder Round#47 1001
- BestCoder Round #49 ($) HDU 5339 Untitled(1001)
- BestCoder Round #84 1001 hdu 5747 模拟
- hdu 5747 Aaronson (BestCoder Round #84 1001)
- hdu 4910 String && BestCoder Round #3
- BestCoder Round #3 BestCoder Sequence
- Android开发规范——命名
- 8.1 计算器的再改进
- [1298]活动选择 山东理工OJ
- 简单的四则运算!【练习】
- hdoj.1877 又一版 A+B 20140804
- BestCoder round#3 1001 && Hdu 4907
- Linear Algebra--Length and Dot Products of Vectors
- hdu1252
- 最好用的pdf转ppt软件
- 字符串转换成整数(atoi)函数的具体实现
- 必然与偶然,本质与细节
- linux 如何添加右键terminal
- paxos 算法深入解析
- 子序列相关问题