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 ...
随机推荐
- PGA与SGA
当用户进程连接到数据库并创建一个对应的会话时,Oracle服务进程会为这个用户专门设置一个PGA区,用来存储这个用户会话的相关内容.当这个用户会话终止时,数据库系统会自动释放这个PAG区所占用的内存. ...
- wsdl透明解析
1.逐个分析wsdl文件中的元素: <types>:数据类型定义的容器,一般使用 xml schema类型系统. <message>:通信消息的数据结构的抽象化定义,使用< ...
- 调色板QPalette类用法详解(附实例、源码)(很清楚:窗口背景色 前景色 按钮的颜色 按钮文本的颜色 )
http://blog.csdn.net/rl529014/article/details/51589096
- Qt Creator needs a compiler set up to build. Configure a compiler in the kit options - Stack Overflow
Qt Creator needs a compiler set up to build. Configure a compiler in the kit options - Stack Overflo ...
- iOS 基于Socket 的 C/S 网络通信结构(下一个)
以前实现简单 Server 程序,服务端通过 void WriteStreamClientCallBack(CFWriteStreamRef stream, CFStreamEventType eve ...
- ps快速删除圆角图片旁白的白色区域方法
简单实用5招的ps快速删除圆角图片旁白的白色区域方法 1.图像-模式-rgb颜色 2.双击背景取消图层锁定 3.用魔棒工具点击要删除的区域 4.delete删除 5.另存为png图片
- openstack之keystone
一.什么是keystone 用于为openstack家族中的其它组件成员提供统一的认证服务,包括身份认证.令牌发放和校验.服务列表.用户权限定义等: 基本概念: 用户User:用于身份认证.一个用户可 ...
- js中new构造函数的研究
<javascript高级编程>里对new操作符的解释: new操作符会让构造函数产生如下变化: 1. 创建一个新对象: 2. 将构造函数的作用域赋给新对象(因此t ...
- C#基础面试
1. 简述Private.Protected.Public.Internal 等访问修饰符的访问权限问题 Private:私有成员,只有类的内部成员可以访问 Protected:保护成员,在类的内部和 ...
- URAL 1203 Scientific Conference dp?贪心
题目:click here 分明就是贪心怎么会在dp的专题 #include <bits/stdc++.h> using namespace std; typedef unsigned l ...