hdu5248---序列变换(二分答案+贪心)

先二分答案,然后从后往前贪心就行

/*************************************************************************
    > File Name: hdu5248.cpp
    > Author: ALex
    > Mail: [email protected]
    > Created Time: 2015年05月31日 星期日 10时46分06秒
 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

static const int N = 100110;
int arr[N];

int main() {
    int t, icase = 1;
    scanf("%d", &t);
    while (t--) {
        int n;
        scanf("%d", &n);
        for (int i = 1; i <= n; ++i) {
            scanf("%d", &arr[i]);
        }
        int l = 0, r = 1000000000;
        int mid;
        int ans = -1;
        while (l <= r) {
            mid = (l + r) >> 1;
            bool flag = 1;
            int last = arr[n] + mid;
            for (int i = n - 1; i >= 1; --i) {
                if (arr[i] + mid < last) {
                    last = arr[i] + mid;
                }
                else if (arr[i] - mid >= last) {
                    flag = 0;
                    break;
                }
                else {
                    --last;
                }
            }
            if (flag) {
                ans = mid;
                r = mid - 1;
            }
            else {
                l = mid + 1;
            }
        }
        printf("Case #%d:\n", icase++);
        printf("%d\n", ans);
    }
    return 0;
}
时间: 2024-08-30 02:39:28

hdu5248---序列变换(二分答案+贪心)的相关文章

BC 2015年百度之星程序设计大赛 - 初赛(1)(序列变换-二分答案贪心)

序列变换 Accepts: 816 Submissions: 3578 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Description 给定序列A={A 1 ,A 2 ,...,A n } , 要求改变序列A中的某些元素,形成一个严格单调的序列B(严格单调的定义为:B i <B i+1 ,1≤i<N ). 我们定义从序列A到序列B变换的代价为cost(A,B

HDU ACM 5248 序列变换-&gt;二分代价+贪心

分析:二分代价,由于序列是单增的,我们使前面一个数相对取最小,这样后面的数变化的值也能相对较小(贪心). #include<iostream> using namespace std; #define N 100010 #define max(a,b) ((a)>(b)?(a):(b)) int num[N],tmp[N],n; bool valid(int cost) { int i; for(i=1;i<=n;i++) tmp[i]=num[i]; tmp[1]=tmp[1]-

Gym 100886J Sockets 二分答案 + 贪心

Description standard input/outputStatements Valera has only one electrical socket in his flat. He also has m devices which require electricity to work. He's got n plug multipliers to plug the devices, the i-th plug multiplier has ai sockets. A device

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]及之后的数都至少需要

【二分答案+贪心】UVa 1335 - Beijing Guards

Beijing was once surrounded by four rings of city walls: the Forbidden City Wall, the Imperial City Wall, the Inner City Wall, and finally the Outer City Wall. Most of these walls were demolished in the 50s and 60s to make way for roads. The walls we

Luogu P1084 疫情控制 | 二分答案 贪心

题目链接 观察题目,答案明显具有单调性. 因为如果用$x$小时能够控制疫情,那么用$(x+1)$小时也一定能控制疫情. 由此想到二分答案,将问题转换为判断用$x$小时是否能控制疫情. 对于那些在$x$小时内不能够走到根节点的子节点上的军队,让他们尽量往上走即可,走到哪里是哪里,这样显然不会更劣. 对于那些在$x$小时内能走到根节点的子节点上的军队,就让他们先走到根节点的子节点上. 然后搞搞贪心即可. #include<iostream> #include<cstdio> #incl

[CSP-S模拟测试]:kill(二分答案+贪心)

题目传送门(内部题50) 输入格式 第一行包含四个整数$n,m,s$,表示人数.怪物数及任务交付点的位置.第二行包含$n$个整数$p_1,p_2,...,p_n$.第三行包含$n$个整数$q_1,q_2,...,q_n$. 输出格式 输出一行包含一个整数$ans$,表示答案. 样例 样例输入: 2 4 52 106 1 4 8 样例输出: 5 数据范围与提示 样例解释: 第一个人打位置为$4$的怪物,第二个人打位置为$8$的怪物,前者花$3$的时间,后者花$5$的时间,该方案对应的时间为$5$,

Educational Codeforces Round 3:D. Gadgets for dollars and pounds(二分答案+贪心)

看了菊苣的理解才知道怎么写..根本没思路啊一开始... 天数肯定在1~n之间,二分答案. 可以用保存前i天的最低美元和英镑汇率,没有规定哪天买,每天也没有购买数量限制,所以二分出一个答案mid的后,就在前mid天中汇率最低的时候一次性购入最便宜的就行了,judge一下总花费 打印答案的时候以ans为结果,再模拟一遍就好 #include"cstdio" #include"queue" #include"cmath" #include"s

HDU - 5884 Sort (二分答案+贪心)

有n个数字,你需要把这n个数字合成一个数字,每次只能把k个数字合并成一个,花费为这k个数字的和. 给一个最大花费,问不超过这个最大花费的情况下,k的最小值. Sample Input 1 5 25 1 2 3 4 5 Sample Output 3 这个题很容易想到二分答案+优先队列check 然而这样复杂度是 O(n logn*logn ),会TLE(这特么都会TLE?加个读入优化就过了) 可以先给所有数字排个序,然后用两个队列,一个存原来的数字,一个存新合成的数字. 所以两个队列都是有序的.