[普及]NOIP 2015 推销员 贪心

NOIP 2015 推销员 

题意:

    有一个喜欢疲劳的推销员,告诉你在一个单口胡同(数轴)中的n户家庭的位置,和向他们推销可以获得的疲劳度。分别输出向(1,2,3,4...n)户人家推销可以得到的最大疲劳值。对了,这个推销员走一格,疲劳度也会加一。

思路:

  贪心,首先按每户人家的推销疲劳度从大到小排序,考虑选定一组,走路带来的疲劳度是定的,就是最远那个*2.

所以对于每个答案$= max(sum[ i ]  + mx * 2 , sum [i - 1] + h[i] )$。其中sum是排序后对推销疲劳度做的前缀和,而h[i] 保存 从 i 到 n中,最大的(2 * 距离 + 推销疲劳度)。

 

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <cstdlib>
#include <iterator>
#include <cmath>
#include <iomanip>
#include <bitset>
#include <cctype>
using namespace std;
//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000")  //c++
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue



typedef long long ll;
typedef unsigned long long ull;

typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int ,pii> p3;
//priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n'

#define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A)  //用来压行
#define REP(i , j , k)  for(int i = j ; i <  k ; ++i)
//priority_queue<int ,vector<int>, greater<int> >que;

const ll mos = 0x7FFFFFFFLL;  //2147483647
const ll nmos = 0x80000000LL;  //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3fLL; //18
const double PI=acos(-1.0);

template<typename T>
inline T read(T&x){
    x=0;int f=0;char ch=getchar();
    while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar();
    while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x=f?-x:x;
}
// #define _DEBUG;         //*//
#ifdef _DEBUG
freopen("input", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
/*-----------------------show time----------------------*/

            const int maxn = 1e5+9;
            struct node
            {
                int s,p;
            }a[maxn];
            bool cmp(const node &a,const node &b){
                return a.p > b.p;
            };
            int sum[maxn],mx,h[maxn]; 
int main(){ 
            int n;
            scanf("%d", &n);
            for(int i=1; i<=n; i++)scanf("%d", &a[i].s);
            for(int i=1; i<=n; i++)scanf("%d", &a[i].p);
            sort(a+1,a+1+n,cmp);

            for(int i=1; i<=n; i++){
                sum[i] = sum[i-1] + a[i].p;
            }
            for(int i=n; i>=1; i--){
                h[i] = max(h[i+1],a[i].s * 2 + a[i].p);
            }

            for(int i=1; i<=n; i++){
                if(mx < a[i].s) mx = a[i].s;
                int tmp = sum[i] + 2 * mx;
                tmp = max(tmp , sum[i-1] + h[i]);
                printf("%d\n", tmp);
            }
            return 0;    
}
LUOGU 2672

 

posted @ 2018-08-22 10:19  ckxkexing  阅读(360)  评论(0编辑  收藏  举报