这道题目的题意就是使用题目中所给的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. Python之路Day2

    -->the start 养成好习惯,每次上课的内容都要写好笔记. 第二天内容主要是熟悉int.long.float.str.list.dict.tuple这几个类的内建方法. 对于Python ...

  2. Python之三层菜单

    三层菜单,根据用户所选数字,进入子菜单.一级一级呈现. menu = { 'Beijing': { "ChaoYang": { "CBD": ['CICC', ...

  3. 基于FPGA的cordic算法的verilog初步实现

    最近在看cordic算法,由于还不会使用matlab,真是痛苦,一系列的笔算才大概明白了这个算法是怎么回事.于是尝试用verilog来实现.用verilog实现之前先参考软件的程序,于是先看了此博文h ...

  4. 转: 理解AngularJS中的依赖注入

    理解AngularJS中的依赖注入 AngularJS中的依赖注入非常的有用,它同时也是我们能够轻松对组件进行测试的关键所在.在本文中我们将会解释AngularJS依赖注入系统是如何运行的. Prov ...

  5. Linux的默认编码可以通过export LC_ALL=zh_CN.GBK来修改

    http://www.cnblogs.com/malecrab/p/5300486.html

  6. ddraw 视频下画图 不闪烁的方法

    我们如果是在在RGB视频上画图(直线,矩形等),一般采用双缓冲区继续,使用内存MemoryDC,来实现画的图形在视频上显示不闪烁的功能,但是我们知道用RGB显示视频都是使用GDI进行渲染,这样很耗CP ...

  7. BZOJ 1000 A+B Problem (I/O)

    #include<cstdio> int main(){ int a,b; scanf("%d%d",&a,&b); printf("%d&q ...

  8. C++访问权限的问题

    以前一直认为对于类中的private数据成员,只有调用该方法的对象才能更能访问自身的私有成员,其他的类在该成员函数(公共接口)中也无法调用自身的私有成员,今天看到<c++ prime plus& ...

  9. 【SqlServer数据类型、C#数据类型、SqlDbType】对应关系及转换

    // sql server数据类型(如:varchar)// 转换为SqlDbType类型public static SqlDbType SqlTypeString2SqlType(string sq ...

  10. config -导航

    在config进行中配置 1在config中添加SITmap  <siteMap enabled="true" defaultProvider="UserSiteM ...