这道题目的题意就是使用题目中所给的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. w3wp.exe CPU过百问题

    w3wp.exe CPU过百问题 最近发布在windows  server2012  IIS8.0上的一个WebAPI项目,才几十个人在线,CPU就会出现过百情况,并且CPU一旦过百应用程序池就自动暂 ...

  2. web应用中Spring ApplicationContext的动态更新

    在web应用中时常需要修改配置,并动态的重新加载ApplicationContext.比如,设置和切换数据库.以下给出一个方法,并通过代码验证可行性. 方法的基本思路是,为WebApplication ...

  3. perl5 第四章 列表和数组变量

    第四章 列表和数组变量 by flamephoenix 一.列表二.数组--列表的存贮  1.数组的存取  2.字符串中的方括号和变量替换   3.列表范围  4.数组的输出  5.列表/数组的长度  ...

  4. 最长回文(Manacher)

    HOT~ 杭电2015级新生如何加入ACM集训队? 最长回文 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  5. 查找jar包的站点

    1.findJAR.com: http://www.findjar.com/index.x 2.jarfire:  https://cn.jarfire.org/

  6. HDU - 5009 Paint Pearls(dp+优化双向链表)

    Problem Description Lee has a string of n pearls. In the beginning, all the pearls have no color. He ...

  7. eclipse hibernate插件在线安装

    下面介绍下 关于在eclipse下如何在线安装插件 首先需要打开eclipse  点击 安装完成后,进入hibernate视图,在左侧窗口右键add configuration, 第一个name属性, ...

  8. Oracle递归sql笔记

    查询一个机构下所辖机构: select * from t00_organ t start with t.organkey=#uporgankey# connect by prior t.organke ...

  9. linux下操作gpio寄存器的方法

    一. 在驱动中: 1. 用的时候映射端口:ioremap; #define GPIO_OFT(x) ((x) - 0x56000000) #define GPFCON (*(volatile unsi ...

  10. BZOJ 1600: [Usaco2008 Oct]建造栅栏( dp )

    QAQ我没读过书...四边形都不会判定了 简单的dp.... --------------------------------------------------------------------- ...