试题描述
 

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

输入
第一行为一个整数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. BZOJ 1002 [ FJOI 2007 ]

    -------------------------萌萌哒分割线------------------------- 题目很容易看懂,数据范围也不大.当然可以卡过暴力的人了. 在n=1时很明显是一种,如下 ...

  2. 【Spring】Spring系列2之bean的配置

    2.bean的配置 2.1.IOC概述 2.2.bean的获取 2.3.依赖注入方式 2.4.属性注入细节 内部bean,不需要ID,ID无效,外部不能引用: 2.5.集合属性注入 2.6.使用p命名 ...

  3. Subarray Sum & Maximum Size Subarray Sum Equals K

    Subarray Sum Given an integer array, find a subarray where the sum of numbers is zero. Your code sho ...

  4. 【leetcode】Interleaving String

    Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...

  5. jdk新特性

    自动拆装箱子: import org.junit.Test; public class Demo { /* * 自动拆装箱 * */ @Test public void ZhuangXiang() { ...

  6. iOS 中的Push Notifications简单实现(APNS)

    Android中的通知只有一种,就是Local Notifications,而iOS中除了Local Notifications外,还有一种Push Notifications.ios的这2种noti ...

  7. 在cmd命令行中弹出Windows对话框

    有时候用bat写一些小脚本最后会弹出对话框提示操作成功,可以用mshta.exe来实现,它是Windows系统的相关程序,用来执行.HTA文件,一般计算机上面都有这个程序,实现如下: mshta vb ...

  8. DP:Ant Counting(POJ 3046)

    数蚂蚁 题目大意:一只牛想数蚂蚁,蚂蚁分成很多组,每个组里面有很多只蚂蚁,现在问你有多少种组合方式 (说白了就是问1,1,1,...,2...,3...,4...)这些东西有多少种排列组合方式 这一道 ...

  9. mybatisforeach循环,传入多个参数

    上代码: controller: @RequestMapping(value = "/findPage", method = RequestMethod.POST) @Respon ...

  10. [Android Pro] 通过IMSI判断手机是移动、联通、电信

    TelephonyManager telManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); /** 获取 ...