题意:初始有n堆积木,每次能移动一块积木,求使得有w区域的积木的高度等于h,最少要移动多少的积木
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5191
思路:在两端都加上宽度为w的空格,将整个区间扫描一遍,求出一个w区间中花费最少的区间。
注意点:无
以下为AC代码:
Run ID | Submit Time | Judge Status | Pro.ID | Exe.Time | Exe.Memory | Code Len. | Language | Author |
13202060 | 2015-03-21 23:33:41 | Accepted | 5191 | 405MS | 5308K | 4431 B | C++ | luminous11 |
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <cctype>
#include <climits>
#include <iomanip>
#include <cstdlib>
#include <algorithm>
//#include <unordered_map>
//#include <unordered_set>
#define ll long long
#define ull unsigned long long
#define all(x) (x).begin(), (x).end()
#define clr(a, v) memset( a , v , sizeof(a) )
#define pb push_back
#define RDI(a) scanf ( "%d", &a )
#define RDII(a, b) scanf ( "%d%d", &a, &b )
#define RDIII(a, b, c) scanf ( "%d%d%d", &a, &b, &c )
#define RS(s) scanf ( "%s", s )
#define PI(a) printf ( "%d", a )
#define PIL(a) printf ( "%d\n", a )
#define PII(a,b) printf ( "%d %d", a, b )
#define PIIL(a,b) printf ( "%d %d\n", a, b )
#define PIII(a,b,c) printf ( "%d %d %d", a, b, c )
#define PIIIL(a,b,c) printf ( "%d %d %d\n", a, b, c )
#define PL() printf ( "\n" )
#define PSL(s) printf ( "%s\n", s )
#define rep(i,m,n) for ( int i = m; i < n; i ++ )
#define REP(i,m,n) for ( int i = m; i <= n; i ++ )
#define dep(i,m,n) for ( int i = m; i > n; i -- )
#define DEP(i,m,n) for ( int i = m; i >= n; i -- )
#define repi(i,m,n,k) for ( int i = m; i < n; i += k )
#define REPI(i,m,n,k) for ( int i = m; i <= n; i += k )
#define depi(i,m,n,k) for ( int i = m; i > n; i += k )
#define DEPI(i,m,n,k) for ( int i = m; i >= n; i -= k )
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
using namespace std;
const double pi = acos(-1);
template <class T>
inline bool RD ( T &ret )
{
char c;
int sgn;
if ( c = getchar(), c ==EOF )return 0; //EOF
while ( c != '-' && ( c < '0' || c > '9' ) ) c = getchar();
sgn = ( c == '-' ) ? -1 : 1;
ret = ( c == '-' ) ? 0 : ( c - '0' );
while ( c = getchar() , c >= '0' && c <= '9' ) ret = ret * 10 + ( c - '0' );
ret *= sgn;
return 1;
}
inline void PD ( int x )
{
if ( x > 9 ) PD ( x / 10 );
putchar ( x % 10 + '0' );
}
const double eps = 1e-10;
const int dir[4][2] = { 1,0, -1,0, 0,1, 0,-1 };
struct node{
int x, y, cnt;
double val;
node(){}
node( int _x, int _y ) : x(_x), y(_y) { val = (double)y/x;}
node( int _x, int _y, int _cnt ) : x(_x), y(_y), cnt(_cnt) {}
friend bool operator < ( const node &a, const node & b ){
return a.val < b.val;
}
};
struct edge{
int to, wei;
edge(){}
edge( int _to, int _wei ) : to(_to), wei(_wei) {}
};
vector<int> num;
ll add[150005];
ll del[150005];
int main()
{
int n, w, h;
ll sum;
while ( scanf ( "%d%d%d", &n, &w, &h ) != EOF ){
num.clear();
int tmp;
sum = 0;
rep ( i, 0, w )
num.push_back ( 0 );
REP ( i, 1, n ){
RDI ( tmp );
sum += tmp;
num.push_back ( tmp );
}
rep ( i, 0, w )
num.push_back ( 0 );
ll cmp = (ll)w * h;
if ( sum < cmp ){
PIL ( -1 );
continue;
}
ll ans = 0x3f3f3f3f;
clr ( add, 0 );
clr ( del, 0 );
add[0] = max ( 0, num[0] - h );
del[0] = max ( 0, h - num[0] );
rep ( i, 1, w ){
add[i] = add[i-1];
del[i] = del[i-1];
if ( num[i] > h )add[i] += ( num[i] - h );
if ( num[i] < h )del[i] += ( h - num[i] );
}
REP ( i, w, num.size() ){
add[i] = add[i-1];
del[i] = del[i-1];
if ( num[i] > h )add[i] += ( num[i] - h );
if ( num[i] < h )del[i] += ( h - num[i] );
ans = min ( ans, max ( add[i] - add[i-w], del[i] - del[i-w] ) );
}
PIL ( ans );
}
return 0;
}