hdu5248 序列变换
百度之星的题。其实最简单的方法是二分答案,我竟然没想到,直接去想O(n)的去了,最后导致滚粗。。。
题意就是给一个数列,要求把它处理成递增序列。
首先我想到了O(n^2)的算法,然后再优化成O(n)过的。
n^2的做法是,弄一个尾指针e,从后往前扫,一旦发现a[e-1]>=a[e],说明a[e]之后的所有数都需要加一个数tmp了。
确定这个tmp的简单方法是直接看a[e-1]。比如1 3 4 2 10 2 3 5 8 9这个序列,当a[e]=2时,通过a[e-1]发现a[e]及之后的数都至少需要加上tmp=5
而对于a[e]之前的数,a[0]必然减去tmp,之后一直到a[e-1]的每一个数都分情况讨论,确定是加上[-tmp,tmp]之间的哪一个数。比如上面这个例子,第一轮处理后的数列应该是
-4 6 7 8 9 7 (8 10 13 14) 括号之后的数是不用实际算出来的,标在这里是方便理解。
每一轮处理是O(n),最多有n轮,所以复杂度是O(n^2)
在这个过程中可以发现存在许多冗余计算。
换一个例子,对于10 9 8 7 6 5 4 3 2 1,a[e]=1时第一轮处理之后是9 10 9 8 7 6 5 4 3 2,第二轮处理之后是8 9 10 9 8 7 6 5 4 3,第三轮处理之后是7 8 9 10 9 8 7 6 5 4……
发现前面也不用全都处理。
于是再引入一个头指针s,同时,在确定tmp时,不仅看a[e-1],还看一看a[s],得到最大的tmp,例如10 9 8 7 6 5 4 3 2 1,当a[e]=1时,通过a[s]=10可以知道tmp至少是9,于是这一轮这后数列成了1 2 3 4 5 6 7 8 9 10。每一轮要么e前移一位,要么s后移一位,所以最多还是n轮。更进一步,对a[s+1]...a[e-1]的处理不必每一轮都进行,处理a[s]时,只需要看a[s+1],并标记,省掉这里O(n)的循环。于是整个算法就变成了O(n)了。
当然,因为我做题的时候是在n^2算法的基础上改的,改一会儿就提交一次,还没等我完全改成O(n)的算法(没有去掉那个O(n)的处理过程),就已经过了。所以下面的代码仅供参考。
/*
* Author : ben
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <functional>
#include <numeric>
#include <cctype>
using namespace std;
typec get_int() {
typec res = , ch;
while (!((ch = getchar()) >= '' && ch <= '')) {
if (ch == EOF)
return -;
}
res = ch - '';
while ((ch = getchar()) >= '' && ch <= '')
res = res * + (ch - '');
return res;
}
const int MAXN = ;
int data[MAXN]; int main() {
int T, N;
T = get_int();
for (int t = ; t <= T; t++) {
N = get_int();
for (int i = ; i < N; i++) {
data[i] = get_int();
}
int ans = , tmp, last = N - , start = , tmp2;
while (last > start) {
if (data[last] - data[last - ] > ) {
last--;
continue;
}
tmp = ceil((data[last - ] - data[last] + ) / 2.0);
tmp2 = ceil((data[start] - data[last] + last - start) / 2.0);
tmp = tmp > tmp2 ? tmp : tmp2;
ans += tmp;
data[last] += tmp;
data[start] -= tmp;
for (int i = start + ; i < last; i++) {
if (data[i] - tmp > data[i - ]) {
data[i] -= tmp;
} else if (data[i] + tmp > data[i - ]) {
data[i] = data[i - ] + ;
} else {
data[i] += tmp;
}
}
}
printf("Case #%d:\n%d\n", t, ans);
}
return ;
}
hdu5248 序列变换的更多相关文章
- hdu5248序列变换(二分+贪心)基础题
题意(中文的直接粘题意吧) 序 ...
- 序列变换(hdu5248)
序列变换 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 2015年百度之星初赛(1) --- C 序列变换
序列变换 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 序列变换(Lis变形)
序列变换 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- hdu 5256 序列变换 (LIS变形)
序列变换 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- 51Nod 欢乐手速场1 B 序列变换[容斥原理 莫比乌斯函数]
序列变换 alpq654321 (命题人) 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 lyk有两序列a和b. lyk想知道存在多少对x,y,满足以下两个条件. 1:gcd( ...
- LIS 2015百度之星初赛2 HDOJ 5256 序列变换
题目传送门 题意:中文题面 分析:LIS(非严格):首先我想到了LIS,然而总觉得有点不对:每个数先减去它的下标,防止下面的情况发生:(转载)加入序列是1,2,2,2,3,这样求上升子序列是3,也就是 ...
- 二分搜索 2015百度之星初赛1 HDOJ 5248 序列变换
题目传送门 /* 二分搜索:在0-1e6的范围找到最小的max (ai - bi),也就是使得p + 1 <= a[i] + c or a[i] - c 比赛时以为是贪心,榨干智商也想不出来:( ...
- luogu P3411 序列变换
链接 P3411 序列变换 如果要最小化答案,那么就最大化不移动的数. 那么不移动的子序列一定是最后答案的一段连续区间,而移动的数我们是一定有办法把他们还原的. 设\(f_{i}\)表示\(i\)点的 ...
随机推荐
- Struts2笔记——result结果类型
result > 每个 action方法都将返回一个 String 类型的值,Struts 将根据这个值来决定响应什么结果. > 每个 Action声明都必须包含有数量足够多的 resul ...
- 【原创】如何在Android中为TextView动态设置drawableLeft等
如何在Android中为TextView动态设置drawableLeft等 两种方式: 方式1:手动设置固有边界 Drawable drawable = getResources().getD ...
- USACO Section 2.3: Money Systems
简单的dp题 /* ID: yingzho1 LANG: C++ TASK: money */ #include <iostream> #include <fstream> # ...
- [转]c/c++输入函数
最全输入函数 c/c++ 一: c=getchar(); 功能:读入一个字符 说明:调用此函数时要求在程序的第一行有预编译命令:#include<stdio>,不过在做c++时 有#inc ...
- YTU 2619: B 友元类-计算两点间距离
2619: B 友元类-计算两点间距离 时间限制: 1 Sec 内存限制: 128 MB 提交: 469 解决: 252 题目描述 类Distance定义为类Point的友元类来实现计算两点之间距 ...
- ZOJ Problem Set - 3861 Valid Pattern Lock(dfs)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3861 这道题当时没做出来,后来经过队友提醒才做出来. 3*3的九宫格,给你 ...
- android backlight
背光设置是在:设置->声音和显示->亮度,通过进度条来设置的. 文件:packages/apps/Settings/src/com/android/settings/BrightnessP ...
- 网易新闻页面信息抓取 -- htmlagilitypack搭配scrapysharp
最近在弄网页爬虫这方面的,上网看到关于htmlagilitypack搭配scrapysharp的文章,于是决定试一试~ 于是到https://www.nuget.org/packages/Scrapy ...
- 使用.9.png报错 Exception raised during rendering
Exception raised during rendering: Index: 2, Size: 2Exception details are logged in Window > Show ...
- git大文件管理
由于git在每一个commit时都会变动过的文件全部保存(不像其他的系统,只做文件增量存储),外加未变动文件的引用,这样如果在文件系统中有一些大的二进制文件,比如图片,视频,那么很快你的repo就将变 ...