HDU 3916 Sequence Decomposition 【贪心】
这道题目的题意就是使用题目中所给的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 【贪心】的更多相关文章
- HDU 4442 Physical Examination(贪心)
HDU 4442 Physical Examination(贪心) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=4442 Descripti ...
- 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变 ...
- Least Cost Bracket Sequence(贪心)
Least Cost Bracket Sequence(贪心) Describe This is yet another problem on regular bracket sequences. A ...
- HDU 5783 Divide the Sequence (贪心)
Divide the Sequence 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5783 Description Alice has a seq ...
- hdu 6047 Maximum Sequence(贪心)
Description Steph is extremely obsessed with "sequence problems" that are usually seen on ...
- ACM学习历程——HDU 5014 Number Sequence (贪心)(2014西安网赛)
Description There is a special number sequence which has n+1 integers. For each number in sequence, ...
- hdu 6299 Balanced Sequence (贪心)
Balanced Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- hdu 6299 Balanced Sequence(贪心)题解
题意:题意一开始不是很明白...就是他给你n个串,让你重新排列组合这n个串(每个串内部顺序不变),使得匹配的括号长度最大.注意,题目要求not necessary continuous,括号匹配不需要 ...
- HDU 5821 Ball (贪心)
Ball 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5821 Description ZZX has a sequence of boxes nu ...
随机推荐
- 【转载】python 模块 - random生成随机数模块
随机数种子 要每次产生随机数相同就要设置种子,相同种子数的Random对象,相同次数生成的随机数字是完全相同的: random.seed(1) 这样random.randint(0,6, (4,5)) ...
- (Problem 53)Combinatoric selections
There are exactly ten ways of selecting three from five, 12345: 123, 124, 125, 134, 135, 145, 234, 2 ...
- 关于Qt信号与槽机制的传递方向性研究(结论其实是错误的,但是可以看看分析过程)
最近由于项目的需求,一直在研究Qt.信号与槽机制是Qt的一大特色,该机制允许两者间传递参数,依次来实现对象间的通信.这个参数会分别存在于信号的参数列表和槽函数的参数列表中.需要注意的是,若将槽函数绑定 ...
- ORACLE中date类型字段的处理
(1)在英文版本的ORACLE中默认日期格式为'DD-MON-YY',例如'01-JAN-98' 在汉化的中文版本中ORACLE默认日期格式为'日-月-年',例如'21-8月-2003'或'21-8月 ...
- js遍历对象的属性并且动态添加属性
var person= { name: 'zhangsan', pass: '123' , 'sni.ni' : 'sss', hello:function (){ for(var i=0;i< ...
- 74HC595的中文资料
74HC595--具有三态输出锁存功能的8位串行输入.串行/并行输出移位寄存器 本文翻译自NXP的74HC595的datasheet 74HC595和74HCT595是带有存储寄存器和三态输出的8位串 ...
- javascript类型转换、运算符、语句
1.类型转换: 分为自动转换和强制转换,一般用强制转换. 其他类型转换为整数:parseint(): 其他类型转换为小数:parsefloat(): 判断是否是一个合法的数字类型:isNaN(): 是 ...
- Longest Substring Without Repeating Characters - 哈希与双指针
题意很简单,就是寻找一个字符串中连续的最长包含不同字母的子串. 其实用最朴素的方法,从当前字符开始寻找,找到以当前字符开头的最长子串.这个方法猛一看是个n方的算法,但是要注意到由于字符数目的限制,其实 ...
- debian6 更新python版本到python3.3
1.下载python3.3安装包 #wget wget --no-cookie --no-check-certificate --header "Cookie:gpw_e24=http%3A ...
- 《Java TCP/IP Socket 编程 》读书笔记之十一:深入剖析socket——TCP套接字的生命周期
转载请注明出处:http://blog.csdn.net/ns_code/article/details/16113083 建立TCP连接 新的Socket实例创建后,就立即能用于发送和接收 ...