试题描述
 

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

输入
第一行为一个整数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. docker的四种网络模式

    /* 1. host模式 : docker run 使用 --net=host指定 docker使用的网络实际上和宿主机一样 2. container模式: 使用 --net=container:co ...

  2. html span标签 不换行(有时span带中文时候是可以自动换行的)

    <span>你好111111111111111111111111111111111111111111111111111aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ...

  3. (原创)Python字符串系列(1)——str对象

    在本博客 <Python字符串系列> 中,将介绍以下内容: Python内置的str对象及操作 字符串的格式化 Python中的正则表达式 re模块 本文将介绍Python内置的 str ...

  4. Java for LeetCode 025 Reverse Nodes in k-Group

    Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...

  5. July 13th, Week 29th Wednesday, 2016

    Travel imparts new vigor to the mind. 旅行能给思想带来新的活力. Travel can give us opportunities to experience m ...

  6. Win7下Event_Log服务4201错误的有效解决方法

    在对Windows7系统进行某些优化或者更改了用户权限之后,会导致Window7系统的“事件查看器”无法启动,显示相关服务没有运行,而对相应服务Windows Event Log进行手动启动的时候,会 ...

  7. Solr的函数查询(FunctionQuery)

    作用 通过函数查询让我们可以利用 numeric域的值或者与域相关的的某个特定的值的函数,来对文档进行评分. 如何使用 这里主要有两种方法可以使用函数查询,这两种方法都是通过solr http 接口的 ...

  8. Python读写文件乱码问题

    对开发者来说,最恼人的问题之一莫过于读写文件的时候,由于编码千差万别,出现乱码问题.好难快速解决啊... 最近我也遇到了这样的问题,经研究,把大致的解决思路拿出来共享. 1. python中习惯首先声 ...

  9. Mysql 对数字的格式化

    format函数:     格式化浮点数 format(number, length); Formats the number X to a format like '#,###,###.##', r ...

  10. cf 333b

    G - Chips Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit S ...