试题描述
 

霸中智力测试机构的一项工作就是按照一定的规则删除一个序列的数字,得到一个确定的数列。陈思同学很渴望成为霸中智力测试机构的主管,但是他在这个工作上做的并不好,俗话说熟能生巧,他打算做很多练习,所以他希望你写一个程序来快速判断他的答案是否正确。

输入
第一行为一个整数m(1<=m<=1000000),
第二行包括m个用空格分开的整数ai(1<=ai<=1000000),组成了最初的序列,
第三行为一个整数n(1<=n<=1000000),表示n个陈思经过一系列删除得到的序列,每个序列两行,第一行给出长度L(1<=L<=m),然后下一行为L个由空格分开的整数bi(1<=bi<=1000000)。
输出
共n行,如果陈思的序列确实是由最初的序列删除一些数得到,就输出TAK,否则输出NIE。
输入示例
7
1 5 4 5 7 8 6
4
5
1 5 5 8 6
3
2 2 2
3
5 7 8
4
1 5 7 4
输出示例
TAK
NIE
TAK
NIE
其他说明
二分2083

对每个权值建立位置的集合,如果用vector要写二分,如果用平衡树就直接lower_bound了。

vector,用now表示当前位置+1,下同(3979ms)

#include<cstdio>
#include<cctype>
#include<vector>
#include<cstring>
#include<algorithm>
#define rep(s,t) for(int i=s;i<=t;i++)
#define ren for(int i=first[x];i!=-1;i=next[i])
using namespace std;
inline int read() {
int x=,f=;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-;
for(;isdigit(c);c=getchar()) x=x*+c-'';
return x*f;
}
const int maxn=;
vector<int> T[maxn];
int main() {
int n=read();
rep(,n) T[read()].push_back(i);
int m=read();
while(m--) {
int k=read(),now=,ok=;
rep(,k) {
int v=read();
if(!T[v].size()||T[v][T[v].size()-]<now) ok=;
else {
int l=,r=T[v].size()-,mid;
while(l<r) if(T[v][mid=l+r>>]<now) l=mid+; else r=mid;
now=T[v][l]+;
}
}
if(ok) puts("TAK");
else puts("NIE");
}
return ;
}

偷懒用set结果T飞了(TLE)

#include<cstdio>
#include<cctype>
#include<set>
#include<cstring>
#include<algorithm>
#define rep(s,t) for(int i=s;i<=t;i++)
#define ren for(int i=first[x];i!=-1;i=next[i])
using namespace std;
inline int read() {
int x=,f=;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-;
for(;isdigit(c);c=getchar()) x=x*+c-'';
return x*f;
}
const int maxn=;
set<int> T[maxn];
set<int>::iterator it;
int main() {
int n=read();
rep(,n) T[read()].insert(i);
int m=read();
while(m--) {
int k=read(),now=,ok=;
rep(,k) {
int v=read();
if(!T[v].size()) ok=;
else {
it=T[v].lower_bound(now);
if(it==T[v].end()) ok=;
else now=(*it)+;
}
}
if(ok) puts("TAK");
else puts("NIE");
}
return ;
}

STL太慢了,我们可以写一个数组,以权值为第一关键字,以位置为第二关键字排序,然后再上面继续二分。(827ms)

我竟然又WA了一发,越来越感觉要直播了。

#include<cstdio>
#include<cctype>
#include<set>
#include<cstring>
#include<algorithm>
#define rep(s,t) for(int i=s;i<=t;i++)
#define ren for(int i=first[x];i!=-1;i=next[i])
using namespace std;
inline int read() {
int x=,f=;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-;
for(;isdigit(c);c=getchar()) x=x*+c-'';
return x*f;
}
const int maxn=;
struct Arr {
int p,v;
bool operator < (const Arr& ths) const {
if(v!=ths.v) return v<ths.v;
return p<ths.p;
}
}A[maxn];
int L[maxn],R[maxn];
int main() {
int n=read();
rep(,n) A[A[i].p=i].v=read();
sort(A+,A+n+);
int cur=;
rep(,) {
if(A[cur].v!=i) continue;
L[i]=cur;while(A[cur+].v==i) cur++;R[i]=cur++;
}
int m=read();
while(m--) {
int k=read(),now=,ok=;
rep(,k) {
int v=read();if(!L[v]||A[R[v]].p<now) ok=;
if(!ok) continue;
int l=L[v],r=R[v],mid;
while(l<r) if(A[mid=l+r>>].p<now) l=mid+; else r=mid;
now=A[l].p+;
}
if(ok) puts("TAK");
else puts("NIE");
}
return ;
}

COJ574 数字序列的更多相关文章

  1. 找出数组中最长的连续数字序列(JavaScript实现)

    原始题目: 给定一个无序的整数序列, 找最长的连续数字序列. 例如: 给定[100, 4, 200, 1, 3, 2], 最长的连续数字序列是[1, 2, 3, 4]. 小菜给出的解法: functi ...

  2. 九度OJ 1544 数字序列区间最小值

    题目地址:http://ac.jobdu.com/problem.php?pid=1544 题目描述: 给定一个数字序列,查询任意给定区间内数字的最小值. 输入: 输入包含多组测试用例,每组测试用例的 ...

  3. 【BZOJ】【1049】【HAOI2006】数字序列

    DP 第一问比较水……a[i]-=i 以后就变成最长不下降子序列问题了,第二问这个结论好神奇,考试的时候怎么破?大胆猜想,不用证明?TAT 题解:http://pan.baidu.com/share/ ...

  4. kaggle之数字序列预测

    数字序列预测 Github地址 Kaggle地址 # -*- coding: UTF-8 -*- %matplotlib inline import pandas as pd import strin ...

  5. string 数字序列大小比较

    string 数字序列大小比较 string.compare string a = "022"; string b="1"; 比较结果 '022' < ' ...

  6. codevs 2622 数字序列

    2622 数字序列 提交地址:http://codevs.cn/problem/2622/  时间限制: 1 s  空间限制: 32000 KB  题目等级 : 黄金 Gold     题目描述 De ...

  7. Shell生成数字序列

    转自http://kodango.com/generate-number-sequence-in-shell Shell里怎么输出指定的数字序列: for i in {1..5}; do echo $ ...

  8. 《剑指offer》第四十四题(数字序列中某一位的数字)

    // 面试题44:数字序列中某一位的数字 // 题目:数字以0123456789101112131415…的格式序列化到一个字符序列中.在这 // 个序列中,第5位(从0开始计数)是5,第13位是1, ...

  9. 【BZOJ1049】 [HAOI2006]数字序列

    BZOJ1049 [HAOI2006]数字序列 dp好题? 第一问 第一问我会做!令\(b_i=a_i-i\),求一个最长不下降子序列. \(n-ans\)就是最终的答案. 第二问 好难啊.不会.挖坑 ...

随机推荐

  1. 07 day 2

    又是惨烈的一天 第一题 多重背包.二进制拆分即可. #include <stdio.h> #define max(a,b) ((a)>(b)?(a):(b)) int n,m,i,j ...

  2. linux开机启动服务和chkconfig使用方法(自定义服务路径启动)

    服务概述在linux操作系统下,经常需要创建一些服务,这些服务被做成shell脚本,这些服务需要在系统启动的时候自动启动,关闭的时候自动关闭.将 需要自动启动的脚本/etc/rc.d/init.d目录 ...

  3. iOS 一个工程中引用其他工程时要注意Skip Install选项

    当主工程引用其他工程,以便使用他们生成的库的时候,在发布时,主要注意这个选项.这个选项的说明如下 Activating this setting when deployment locations a ...

  4. iOS constraint被应用于view上的时间

    在viewdidload时,constraint是没有被应用的,之后在layoutSubviews时,系统应用了constraint.但是我感觉在viewWillLayoutSubviews函数时就已 ...

  5. Java for LeetCode 062 Unique Paths

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  6. Gym 100801E Easy Arithmetic (思维题)

    题目:传送门.(需要下载PDF) 题意:给定一个长度不超过1000的字符串表达式,向该表达式中加入'+'或'-',使得表达式的值最大,输出该表达式. 题解:比如300-456就改成300-4+56,遇 ...

  7. ubuntu下eclipse无法启动问题

    添加-vm和对应参数 -vm/jdk安装目录/bin/java-startupplugins/org.eclipse.equinox.launcher_1.3.100.v20150511-1540.j ...

  8. [译]SQL Server 之 查询优化器

    因为生成查询计划的代价比较大,所以查询计划将会被缓存. 树形结构 SQL 查询首先被转化为树形结构,每个节点都是一个查询操作.例如: SELECT * FROM Customers C INNER J ...

  9. GRE红宝书5-6

    page5 adopt: adoration: adore:   --ore讲话, oration演讲 adorn:   orn表示装饰, ornate adulation:      adulate ...

  10. js实现iframe自适应高度

    转自:http://www.jb51.net/article/15780.htm 对于自适应高度的代码有很多,可效率什么的考虑进来好代码就不多见了,不过思路倒是差不多的! 不带边框的iframe因为能 ...