link

唉,我连pj题都不会做,今年tg AFO稳了...

考虑枚举最远走到哪户,那么在能走到的范围内其他去的住户一定是 $A_i$ 最大的那些。

这样就有一个$O(n^2 \log n)$的优秀做法了。

或者说,除了你要走到最远的住户以外,其它住户一定是能选择的$A_i$最大的住户。

那么对于选$x$个住户,就有两种情况:

  1. 选的是$A_i$最大的(此基础上$S_i$也要最大)$x$个,
  2. 选的是$A_i$最大的$x-1$个,另一个选的是剩下的里面$A_i+S_i$最大的。

正确性显然。

/*
Author: CNYALI_LK
LANG: C++
PROG: 2672.cpp
Mail: cnyalilk@vip.qq.com
*/
#include<bits/stdc++.h>
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define DEBUG printf("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__)
#define Debug debug("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__)
#define all(x) x.begin(),x.end()
#define x first
#define y second
using namespace std;
const double eps=1e-8;
const double pi=acos(-1.0);
typedef long long ll;
typedef pair<int,int> pii;
template<class T>int chkmin(T &a,T b){return a>b?a=b,1:0;}
template<class T>int chkmax(T &a,T b){return a<b?a=b,1:0;}
template<class T>T sqr(T a){return a*a;}
template<class T>T mmin(T a,T b){return a<b?a:b;}
template<class T>T mmax(T a,T b){return a>b?a:b;}
template<class T>T aabs(T a){return a<0?-a:a;}
template<class T>int dcmp(T a,T b){return a>b;}
template<int *a>int cmp_a(int x,int y){return a[x]<a[y];}
#define min mmin
#define max mmax
#define abs aabs
namespace io {
    const int SIZE = (1 << 21) + 1;
    char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr;
    // getchar
    #define gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
    // print the remaining part
    inline void flush () {
        fwrite (obuf, 1, oS - obuf, stdout);
        oS = obuf;
    }
    // putchar
    inline void putc (char x) {
        *oS ++ = x;
        if (oS == oT) flush ();
    }
    // input a signed integer
    inline void read (int &x) {
        for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;
        for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f;
    }
    inline void read (char &x) {
        x=gc();
    }
    inline void read(char *x){
        while((*x=gc())=='\n' || *x==' '||*x=='\r');
        while(!(*x=='\n'||*x==' '||*x=='\r'))*(++x)=gc();
    }
    template<typename A,typename ...B>
    inline void read(A &x,B &...y){
        read(x);read(y...);
    }
    // print a signed integer
    inline void write (int x) {
        if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x;
        while (x) qu[++ qr] = x % 10 + '0',  x /= 10;
        while (qr) putc (qu[qr --]);
    }
    inline void write (char x) {
        putc(x);
    }
    inline void write(const char *x){
        while(*x){putc(*x);++x;}
    }
    inline void write(char *x){
        while(*x){putc(*x);++x;}
    }
    template<typename A,typename ...B>
    inline void write(A x,B ...y){
        write(x);write(y...);
    }
    //no need to call flush at the end manually!
    struct Flusher_ {~Flusher_(){flush();}}io_flusher_;
}
using io :: read;
using io :: putc;
using io :: write;
pii a[100005];
int qs[100005],qmx[100005],hmx[100005];
int main(){
#ifdef cnyali_lk
    freopen("2672.in","r",stdin);
    freopen("2672.out","w",stdout);
#endif
    int n;
    read(n);
    for(int i=1;i<=n;++i){read(a[i].y);a[i].y*=2;}
    for(int i=1;i<=n;++i)read(a[i].x);
    sort(a+1,a+n+1,dcmp<pii>);
    for(int i=1;i<=n;++i){qmx[i]=max(qmx[i-1],a[i].y);qs[i]=qs[i-1]+a[i].x;}
    for(int i=n;i;--i)hmx[i]=max(hmx[i+1],a[i].x+a[i].y);
    for(int i=1;i<=n;++i)printf("%d\n",max(qmx[i]+qs[i],qs[i-1]+hmx[i]));
    return 0;
}

标签: 贪心

添加新评论