COJ574 数字序列
|
试题描述
|
|
霸中智力测试机构的一项工作就是按照一定的规则删除一个序列的数字,得到一个确定的数列。陈思同学很渴望成为霸中智力测试机构的主管,但是他在这个工作上做的并不好,俗话说熟能生巧,他打算做很多练习,所以他希望你写一个程序来快速判断他的答案是否正确。 |
|
输入
|
|
第一行为一个整数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 数字序列的更多相关文章
- 找出数组中最长的连续数字序列(JavaScript实现)
原始题目: 给定一个无序的整数序列, 找最长的连续数字序列. 例如: 给定[100, 4, 200, 1, 3, 2], 最长的连续数字序列是[1, 2, 3, 4]. 小菜给出的解法: functi ...
- 九度OJ 1544 数字序列区间最小值
题目地址:http://ac.jobdu.com/problem.php?pid=1544 题目描述: 给定一个数字序列,查询任意给定区间内数字的最小值. 输入: 输入包含多组测试用例,每组测试用例的 ...
- 【BZOJ】【1049】【HAOI2006】数字序列
DP 第一问比较水……a[i]-=i 以后就变成最长不下降子序列问题了,第二问这个结论好神奇,考试的时候怎么破?大胆猜想,不用证明?TAT 题解:http://pan.baidu.com/share/ ...
- kaggle之数字序列预测
数字序列预测 Github地址 Kaggle地址 # -*- coding: UTF-8 -*- %matplotlib inline import pandas as pd import strin ...
- string 数字序列大小比较
string 数字序列大小比较 string.compare string a = "022"; string b="1"; 比较结果 '022' < ' ...
- codevs 2622 数字序列
2622 数字序列 提交地址:http://codevs.cn/problem/2622/ 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 黄金 Gold 题目描述 De ...
- Shell生成数字序列
转自http://kodango.com/generate-number-sequence-in-shell Shell里怎么输出指定的数字序列: for i in {1..5}; do echo $ ...
- 《剑指offer》第四十四题(数字序列中某一位的数字)
// 面试题44:数字序列中某一位的数字 // 题目:数字以0123456789101112131415…的格式序列化到一个字符序列中.在这 // 个序列中,第5位(从0开始计数)是5,第13位是1, ...
- 【BZOJ1049】 [HAOI2006]数字序列
BZOJ1049 [HAOI2006]数字序列 dp好题? 第一问 第一问我会做!令\(b_i=a_i-i\),求一个最长不下降子序列. \(n-ans\)就是最终的答案. 第二问 好难啊.不会.挖坑 ...
随机推荐
- mysql中binary相加的问题
我在mysql中有这样一段代码 SQL code ? 1 2 3 4 5 6 7 8 declare @byte1 binary(1) declare @byte2 binary(1) decla ...
- maven项目,导入的jar包,没有包含在pom文件中,install失败
[INFO] BUILD FAILURE[INFO] ------------------------------------------------------------------------[ ...
- 2.2 编程之美--不要被阶乘吓到[zero count of N factorial]
[本文链接] http://www.cnblogs.com/hellogiser/p/zero-count-of-N-factorial.html [题目] 问题1:给定一个整数N,那么N的阶乘N! ...
- Maven发布web项目到tomcat
在java开发中经常要引入很多第三方jar包:然而无论是java web开发还是其他java项目的开发经常会由于缺少依赖包引来一些不必要的异常.常常也是因为这样的原因导致许多简单的缺包和版本问题耗费大 ...
- android.content.ActivityNotFoundException: Unable to find explicit activity class have you declared this activity in your AndroidManifest.xml?
在整合PullToRefresh的时候出现如下异常 10-22 23:20:01.826 32331-32331/com.example.news.andoridnewsapp E/AndroidRu ...
- @SuppressWarnings注解
简介:java.lang.SuppressWarnings是J2SE 5.0中标准的Annotation之一.可以标注在类.字段.方法.参数.构造方法,以及局部变量上.作用:告诉编译器忽略指定的警告, ...
- Androidi性能优化之高效使用内存
应用生存期的绝大多数时间都在用于处理内存中的数据 性能主要取决于以下三个因素: a:CPU如何操作特定的数据类型 b: 数据和指令需要占用多少存储空间 c: 数据在内存中的布局 访问内存: 因为访问内 ...
- 谈JavaScript代码封装
前言 也算老生常谈的问题了,再深入搞一搞怎么玩儿封装,如果看到这篇文章的你,正好你也是追求完美的代码洁癖狂者,那么这篇文章相信非常适合你. 举一个例子,编写一个Person类,具有name和birth ...
- date +%s 能打印出自1970-01-01 00:00:00到当前时间的秒数
[root@bass Desktop]# date +%s 1466561580 [root@bass Desktop]# python Python 2.6.6 (r266:84292, Jul 2 ...
- SQLite常用网址
英文版SQLite官网: http://www.sqlite.org/rescode.html中文版SQLite官网:http://www.helplib.net/s/sqlite/9/167.sht ...