1. 程式人生 > >hdu-5248(貪心+二分)

hdu-5248(貪心+二分)

思路:二分求最小代價,使前面的值相對較小,這樣後面的數值增加相對較小。

#include<iostream>
#include<stdio.h>
#include<math.h>
#include <string>
#include<string.h>
#include<map>
#include<utility>
#include<set>
#include<queue>
#include<algorithm>
#include<stdlib.h>
using namespace std;
#define eps 1e-8
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d%d",&x,&y)
#define rd3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define pii pair<int,int>
#define ll long long int
#define maxn 100005
int a[maxn],b[maxn],n;
bool check(int x)
{
    for(int i=1;i<=n;i++)
        b[i]=a[i];
    for(int i=1;i<=n;i++)
    {
        if(i==1) {
            b[i]=b[i]-x;
            continue;
        }
        if(b[i]<=b[i-1]){
            if(b[i-1]+1-b[i]<=x) b[i]=b[i-1]+1;
            else return false;
        }
        else if(b[i]-x>b[i-1]) b[i]=b[i]-x;
        else b[i]=b[i-1]+1;
    }
    return true;
}
int main()
{
    int T,t=0;
    rd(T);
    while(T--){
        printf("Case #%d:\n",++t);
        rd(n);
        for(int i=1;i<=n;i++)
        {
            rd(a[i]);
        }
        int l=0;
        int r=maxn*10;
        int ans=0;
        while(l<r){
            int mid=(l+r)>>1;
            if(check(mid)) {
                    ans=mid;
                    r=mid;
            }
            else l=mid+1;
        }
        printf("%d\n",ans);
    }
    return 0;
}


相關推薦

hdu-5248貪心+二分

思路:二分求最小代價,使前面的值相對較小,這樣後面的數值增加相對較小。 #include<iostream> #include<stdio.h> #include<math.h> #include <string> #inc

Road to Cinema貪心+二分

int -s 最長 blog for spl tex long long ssi https://www.cnblogs.com/flipped/p/6083973.html 原博客轉載 http://codeforces.com/group/1EzrF

ZOJ——Copying Books 最大值最小化問題 貪心 + 二分

題目連結: #include <cstdio> #include <cmath> #include<vector> #include<cstring> #include<algorithm> #include<cmath>

CodeForces 767D Cartons of milk 貪心+二分

題目連結:http://codeforces.com/problemset/problem/767/D #include<bits/stdc++.h> using namespace std; #define debug puts("YES"); #define rep(x,y,z

CodeForces 767D Cartons of milk 貪心+二分

#include<bits/stdc++.h> using namespace std; #define debug puts("YES"); #define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++) #def

poj2456貪心+二分

/* 題意: 有n個牛欄,選m個放進牛,相當於一條線段上有 n 個點,選取 m 個點, 使得相鄰點之間的最小距離值最大 思路:貪心+二分 二分列舉相鄰兩牛的間距,判斷大於等於此間距下能否放進所有的牛。 */ #include <cstdio>

POJ-1505&&UVA-714 抄書貪心+二分

題目傳送門:http://poj.org/problem?id=1505 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8

NJUST1925 sequence貪心+二分

sequence Time Limit: 1000MS Memory Limit: 65536KB Description 將一個給定的數列,拆分成K個不降序列,每個數出現且只出現一次

HDU 1677 貪心 || 貪心二分

題意: 有T個案例,給n個俄羅斯套娃的寬高,隨後是n個w、h,如果當前的娃娃比其他的娃娃寬高就要小,那麼就可以把當前娃娃放到這個大娃娃中。 思路&做法: 我最初的思路是遍歷輸入的娃娃也就是a陣列,當前娃娃比其他娃娃都小,那麼就放到第二小中。當然我也是卡死在了

HDU 2143 Can you find it?基礎二分

i++ php http miss then represent ise for stream Can you find it? Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/10000 K (

HDU - 4803 Poor Warehouse Keeper 貪心模擬

Jenny is a warehouse keeper. He writes down the entry records everyday. The record is shown on a screen, as follow:  There are only two b

HDU 4803 Poor Warehouse Keeper貪心+精度

題解: 先求出最後的單價,我們每一步都於逼近單價。 因為只有總價部分可以優化成加一個整數,所以每一步去就加上目標總價與當前總價差多少即可。 #include <algorithm> #include <iostream> #include

2017.10.10 貪心+二分答案+DP

*56學長們出的題,做的時候覺得好難QAQ,講完題後自己又打了一遍,開始懷疑起自己的智商T^T。。。好吧就這樣吧 _ψ(‥ ) 注:題目的資料範圍以資料範圍及提示裡的為準。 t1: 剛開始做時感覺好像不是很難,因此下決心一定要把這道題

迷瘴 HDU - 2570貪心

本題可以用貪心演算法來做,將濃度從小到大排序,判斷將其加入已選集合後濃度是否大於w,若大於,則不入選。 #include <stdio.h> #include <algorithm> using namespace std; bool cmp(i

HDU 4268 Alice and Bob貪心+STL

該題一開始我用multiset寫了一發,寫的比較裸,TLE了 。 後來隊友想到了一個比較好的方法 :將兩個結構體排序之後,從大到小列舉Alice的h,對於每個h,將Bob中滿足h小於當前h的牌的w加進multiset,然後用二分函式查詢一下大於等於當前w的第一個數,當前迭代

HDU 4334思維+二分答案

傳送門題面:TroubleTime Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6560    Accepted Sub

HDU 5726 GCDRMQ+二分線段樹也可

Problem Description Give you a sequence of N(N≤100,000) integers : a1,...,an(0<ai≤1000,000,000). There are Q(Q≤100,000) queries

HDU 1051 Wooden Sticks 貪心入門

The input consists of T test cases. The number of test cases (T) is given in the first line of the input file. Each test case consists of two lines: The

HDU4763-Theme SectionKMP+二分

mat 20px 題意 ngs algo get names col con Theme Section Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other

ZOJ 3689 Digging貪心+dp

ron tex calendar using gyp mic tar cmp rec Digging Time Limit: 2 Seconds Memory Limit: 65536 KB When it comes to the Maya Civili