这道题目的题意就是使用题目中所给的Gate 函数,模拟出输入的结果

当然我们分析的时候可以倒着来,就是拿输入去减

每次Gate 函数都会有一个有效范围

这道题目求的就是,找出一种模拟方法,使得最小的有效范围最大化。

是一道【贪心】题

参考了https://github.com/boleynsu/acmicpc-codes 的做法

b 数组中存放是 Sequence 的下标
这是一个O(n)的算法 if (a[i-1]<a[i]){
int k=a[i]-a[i-1];
while (k--) b[++bt]=i;
}
就是把 i 加进 b 数组,加 a[i] - a[i - 1]次
比如 a[i - 1] 为3 a[i] 为 5
那么在 b[ ] 中就会加两次 3 else if (a[i-1]>a[i]){
int k=a[i-1]-a[i];
while (k--){
++bh;
}
answer = min(answer,i - b[bh - 1]);
}
bh 指针右移 k 次, 由于b 数组为非递减数组,故最后一位一定是最大的
取i - b[bh - 1] 与 answer比较,这个意思就是比较 Gate 函数的有效范围
如果有更小的范围,那么更新一遍
这里的 i 就是当前的位置, b[bh - 1]的意思……

  

//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <algorithm>
#define ll long long
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
using namespace std; const int MAXN = ; int N;
int a[MAXN];
int b[MAXN];
int bh,bt; int main(){
int T;
scanf("%d",&T);
while (T--){
scanf("%d",&N);
for (int i=;i<=N;i++)
scanf("%d",a+i);
bh=,bt=-;
int answer=N;
a[]=a[N+]=;
for (int i=;i<=N+;i++){
if (a[i-]<a[i]){
int k=a[i]-a[i-];
while (k--) b[++bt]=i;
}
else if (a[i-]>a[i]){
int k=a[i-]-a[i];
while (k--){
++bh;
}
answer = min(answer,i - b[bh - ]);
}
}
printf("%d\n",answer);
}
}

当然在这里,也有一种更简单的方法也能过,不知道是不是算是数据水呢

#include<stdio.h>
int main(){
int t, n, i, j, k, cnt;
int a[], ans;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(i = ; i <= n; ++i){
scanf("%d",&a[i]);
}
i = ;
ans = 0x3f3f3f3f;
while(i <= n){
j = i;
while(a[j+] >= a[j]){
--a[j];
++j;
}
--a[j];
cnt = j - i + ;
if(cnt < ans)
ans = cnt;
while(a[i] == )
++i;
}
printf("%d\n",ans);
}
return ;
}

HDU 3916 Sequence Decomposition 【贪心】的更多相关文章

  1. HDU 4442 Physical Examination(贪心)

    HDU 4442 Physical Examination(贪心) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=4442 Descripti ...

  2. HDU 3397 Sequence operation(线段树)

    HDU 3397 Sequence operation 题目链接 题意:给定一个01序列,有5种操作 0 a b [a.b]区间置为0 1 a b [a,b]区间置为1 2 a b [a,b]区间0变 ...

  3. Least Cost Bracket Sequence(贪心)

    Least Cost Bracket Sequence(贪心) Describe This is yet another problem on regular bracket sequences. A ...

  4. HDU 5783 Divide the Sequence (贪心)

    Divide the Sequence 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5783 Description Alice has a seq ...

  5. hdu 6047 Maximum Sequence(贪心)

    Description Steph is extremely obsessed with "sequence problems" that are usually seen on ...

  6. ACM学习历程——HDU 5014 Number Sequence (贪心)(2014西安网赛)

    Description There is a special number sequence which has n+1 integers. For each number in sequence, ...

  7. hdu 6299 Balanced Sequence (贪心)

    Balanced Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  8. hdu 6299 Balanced Sequence(贪心)题解

    题意:题意一开始不是很明白...就是他给你n个串,让你重新排列组合这n个串(每个串内部顺序不变),使得匹配的括号长度最大.注意,题目要求not necessary continuous,括号匹配不需要 ...

  9. HDU 5821 Ball (贪心)

    Ball 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5821 Description ZZX has a sequence of boxes nu ...

随机推荐

  1. 【转】C++ stringstream介绍,使用方法与例子

    原文来自:http://www.cnblogs.com/lancidie/archive/2010/12/03/1895161.html C++引入了ostringstream.istringstre ...

  2. android搭建环境错误 daemon not running. starting it now on port 5037 ADB server didn't ACK

    android搭建环境错误 daemon not running. starting it now on port 5037 ADB server didn't ACK ADB server didn ...

  3. poj 1050 To the Max(线性dp)

    题目链接:http://poj.org/problem?id=1050 思路分析: 该题目为经典的最大子矩阵和问题,属于线性dp问题:最大子矩阵为最大连续子段和的推广情况,最大连续子段和为一维问题,而 ...

  4. swjtu 2213 A Game About Cards(模拟题)

    题目链接:http://acm.swjtu.edu.cn/JudgeOnline/showproblem?problem_id=2213 思路分析:该问题与约瑟夫问题相似:每次将前n张牌放到队列的最后 ...

  5. Android学习笔记(十五)——碎片的生命周期(附源代码)

    碎片的生命周期 点击下载源代码 与活动类似.碎片具有自己的生命周期.理解了碎片的生命周期后.我们能够在碎片被销毁时正确地保存事实上例,在碎片被重建时将其还原到前一个状态. 1.使用上一篇的项目Frag ...

  6. c#程序添加其他网站的webservice引用。

    前言: 有A网站,B网站两个网站独立运行.某一天,需要两个网站进行合作来完成一件事,比如用A网站接收一些数据进行处理,存入中转数据库,B网站定时去取数据进入自己的系统进行处理. 可能我们会先想到在B网 ...

  7. Android 屏幕尺寸知识

    转自:http://www.zcool.com.cn/article/ZNjI3NDQ=.html 1.了解几个概念 (1)分辨率.分辨率就是手机屏幕的像素点数,一般描述成屏幕的“宽×高”,安卓手机屏 ...

  8. JDBC----数据库连接池(connection pool)

    •数据库连接池的基本思想就是为数据库连接建立一个"缓冲池".预先在缓冲池中放入一定数量的连接,当需要建立数据库连接时,只需从"缓冲池"中取出一个,使用完毕之后再 ...

  9. Nginx 之五: Nginx服务器的负载均衡、缓存与动静分离功能

    一.负载均衡: 通过反向代理客户端的请求到一个服务器群组,通过某种算法,将客户端的请求按照自定义的有规律的一种调度调度给后端服务器. Nginx的负载均衡使用upstream定义服务器组,后面跟着组名 ...

  10. iOS显示PDF

    使用UIWebView来显示 //locale file NSString *html = [NSString stringWithContentsOfFile:path1 encoding:NSUT ...