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 ...
随机推荐
- appium 学习各种小功能总结--功能有《滑动图片、保存截图、验证元素是否存在、》---新手总结(大牛勿喷,新手互相交流)
1.首页滑动图片点击 /** * This Method for swipe Left * 大距离滑动 width/6 除数越大向左滑动距离也越大. * width:720 *height:1280 ...
- ID卡
ID卡全称为身份识别卡(Identification Card),是一种不可写入的感应卡,含固定的编号,主要有台湾SYRIS的EM格式.美国HIDMOTOROLA等各类ID卡.ID卡与磁卡一样,都仅仅 ...
- 再探java基础——break和continue的用法
再探java基础——break和continue的用法 break break可用于循环和switch...case...语句中. 用于switch...case中: 执行完满足case条件的内容内后 ...
- libcurl post上传文件
#include <stdio.h>#include <string.h> #include <curl/curl.h> int main(int argc, ch ...
- Ext JS学习第二天 我们所熟悉的javascript(一)
此文用来记录学习笔记: •ExtJS是一个强大的javascript框架,如果想真正的掌握ExtJS,那么我们必须要对javascript有一定的认识,所以很有必要静下心来,抱着一本javascrip ...
- c 按输入的字母来输出对应效果
输入一个大写字母,如 F,输出 比如: 输入:F 输出: F EFE DEFED CDEFEDC BCDEFEDCB ABCDEFEDCBA #include<stdio.h> int m ...
- ansible模块
ansible模块: 模块(Modules),类似于 "任务插件"("task plugins")或"库插件"("library ...
- FtpManager类
public class FtpManager { /// <summary> /// 主机名 /// </summary> string ftpServerIP; /// & ...
- Android操作HTTP实现和服务器通信
众所周知,Android与服务器通信通常采用HTTP通信方式和Socket通信方式,而HTTP通信方式又分get和post两种方式.至于Socket通信会在以后的博文中介绍. HTTP协议简介: HT ...
- vagrant 入门2
创建第一个Vagrant虚拟环境以及工程: (1)创建工程目录, 并且执行vagrant init命令,该命令会产生最初的 Vagrantfile $ mkdir vagrant_guide $ cd ...