On the greedy [review]

NYG backpack

【Problem Description】

NYG a magic backpack, into each article to a volume of the pack becomes large.

That is, each placed in an article, the backpack will be occupied by a certain volume, but then will increase the total volume of the backpack constant value (note that the total volume of the backpack into the article after it is increased).

NYG found this backpack is very easy to use, so can not help but think of a problem.

Backpack now given initial volume V and n items, two values ​​a, b for each item, respectively, and the volume occupied by the articles into Backpack increased volume.

NYG want to know whether all the items loaded into? Because NYG more honest, such a simple question naturally do not like to pretend. So he came to ask you.

analysis:

Because you can only ask Bahrain, which is easier to handle and more

Definitely need to put a <b is first used, it may do a set a> b of

If the former, then no law, let alone the latter more

For a <b portion, first with a definitely smaller

Because there may be only the first few space money back to hold back a few

The key lies in the case of a> b is

Then found space loss is certainly, certainly consider the best is to lose a small space, this will free up more space for rear discharge

Do not think the question is read backpack ....

code:

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn = 100004;
inline int read()
{
    int ret, f=1;
    char c;
    while((c=getchar())&&(c<'0'||c>'9'))if(c=='-')f=-1;
    ret=c-'0';
    while((c=getchar())&&(c>='0'&&c<='9'))ret = (ret<<3)+(ret<<1)+c-'0';
    return ret*f;
}
int T, n, s, cnt1, cnt2;
struct thi{
    ll a, b;
}t[maxn], f[maxn];
bool cmp1(thi x, thi y)
{
    return x.a < y.a;
}
bool cmp2(thi x, thi y)
{
    return x.b > y.b;
}
int main()
{
    T = read();
    while(T--)
    {
        n = read();s = read();
        cnt1 = cnt2 = 0;    
        for(int i = 1; i <= n; ++i)
        {
            ll x = 1LL * read(), y = 1LL * read();
            if(y > x)
                t[++cnt1] = (thi){x, y};
            else
                f[++cnt2] = (thi){x, y};
        }
        sort(t + 1, t + cnt1 + 1, cmp1);
        ll now = 1LL * s;    
        bool fl = 0;    
        for(int i = 1; i <= cnt1; ++i)
        {
            now -= t[i].a;
            if(now < 0)
            {
                fl = 1;
                break;
            }
            now += t[i].b;    
        }
        if(!fl)
        {
            sort(f + 1, f + cnt2 + 1, cmp2);
            for(int i = 1; i <= cnt2; ++i)
            {
                now -= f[i].a;
                if(now < 0)
                {
                    fl = 1;
                    break;
                }
                now += f[i].b;    
            }
        }
        printf("%s\n", fl? "No": "Yes");
    }
    return 0;
}

Describes
an ordinary chess board, which is divided into 8 by 8 (8 rows and 8 columns) of the 64 squares. With the same shape of domino tiles, each tile covers exactly two adjacent squares on the board, i.e., a domino 1 is a two row or two rows of a card. So, whether the 32 dominoes placed on a chessboard so that any two do not overlap dominoes, each covering two square dominoes, and all of the squares on the board are covered? We call such an arrangement is called a chessboard perfectly covered dominoes. This arrangement is a simple question, students can quickly construct many different perfect coverage. However, the total number of different computing perfect coverage is not an easy thing. However, the students play to their wisdom, it is possible to do.

Now we take the total number of different perfect cover n the board by 3 computer programming to calculate.

è¿ is ?? ???? ???? å å æ ?? ???? ???? ¾ç è¿ °

analysis:

First odd certainly no solution, so consider an even number of recursive transfer

Clearly set f [i] represents the number of programs before the i column

Look at two, then only three ways

It is f [i] = f [i-2] * 3

But there is a grid of four two programs will be more

One kind of six grid will be more than two programs

So there is transfer

f (n) = 3f (n-2) + 2f (n-4) + 2f (n-6) + ... + 2f (0)

then

f (n-2) = 3f (n-4) + 2f (n-6) + ...... + 2f (0)

Obtained by subtracting the two formulas

f(i)=4f(i-2)-f(i-4)

Recursive transfer on the line

Code is a recursive, too lazy to write

analysis:

After drained sequence

Answer selected interval must be a continuous interval

A continuous interval when to take it to the optimal solution?

Of course the median

Such dual ruler emulated pointer can be done O (n) determines the

code by std:

#include<cstdio>
#include<algorithm>
using namespace std;
#define rep(i,n) for(int i=1;i<=n;++i)
#define mp make_pair
#define pb push_back
#define x0 gtmsub
#define y0 gtmshb
#define x1 gtmjtjl
#define y1 gtmsf
long long s[100010];
int n,k,a[100010],ans;
bool check(int l,int r)
{
    int x=(l+r)>>1;
    return (1ll*(x-l+1)*a[x]-(s[x]-s[l-1]))+((s[r]-s[x])-1ll*(r-x)*a[x])<=k;
}
int main()
{
    freopen("signin.in", "r", stdin);
    freopen("signin.out", "w", stdout);
    scanf("%d%d",&n,&k);
    rep(i,n)scanf("%d",&a[i]);
    sort(a+1,a+n+1);
    rep(i,n)s[i]=s[i-1]+a[i];
    int now=1;
    rep(i,n)
    {
        now=max(now,i);
        for(;now<=n&&check(i,now);now++);
        ans=max(ans,now-i);
    }
    printf("%d\n",ans);
    return 0;
}

This question really enough check-ins

Tucao: n hate this point will have to consider, but also consider the question of the side

If you say that the point x considered to sunny point

The point before that point and contribution points after the points are considered

Simplification of the original formula

code by std

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#define N 1000005
#define ll long long
using namespace std;
int n,d;
int s[N];
ll sum1[N],sum2[N],sum3[N],ans[N];
ll work(int a,int b,int x){
    ll s1,s2,s3;
    if(a<=0||a>n) s1=sum1[b],s2=sum2[b],s3=sum3[b];
    else s1=sum1[b]-sum1[a],s2=sum2[b]-sum2[a],s3=sum3[b]-sum3[a];
    ll an=s3+(ll)2*s2*x+(ll)x*x*s1;
    return an;
}
int main()
{
    scanf("%d%d",&n,&d);
    for(int i=1;i<=n;i++) scanf("%d",&s[i]);
    for(int i=1;i<=n;i++) sum1[i]=sum1[i-1]+s[i],sum2[i]=sum2[i-1]+(ll)i*s[i],sum3[i]=sum3[i-1]+(ll)i*i*s[i];
    for(int i=1;i<=n+1;i++) ans[i]+=work(i-d-1,i-1,d-i+1);
    for(int i=n;i>=1;i--) sum1[i]=sum1[i+1]+s[i],sum2[i]=sum2[i+1]+(ll)(n-i+1)*s[i],sum3[i]=sum3[i+1]+(ll)(n-i+1)*(n-i+1)*s[i];
    for(int i=1;i<=n+1;i++) ans[i]+=work(i+d,i,d+i-n-1);
    ll mx=0;
    for(int i=1;i<=n+1;i++) mx=max(mx,ans[i]);
    printf("%lld\n",mx);
    return 0;
}

https://www.luogu.org/problem/P4393

Title Description

For a given sequence a1, ..., an, we have it in a single operation reduce (i), the operation is the number of columns of elements ai and ai + 1 replaced by an element max (ai, ai + 1), thus obtained a new sequence shorter than the original sequence. The cost of this operation is max (ai, ai + 1). After the operation time n-1 can be obtained as a sequence of length.

Our task is to reduce the computational cost of the minimum steps, given the length of the sequence into the sequence.

analysis:

Virtually every number is only likely to be effective plus a maximum of two (Think about why

Consolidated after another once combined, he or smaller than the number of sides (FIG a3)

And look at the situation on each side

When the number of side than a [i] is small, will ultimately make a [i] is added once, if as shown in a3, a1 one side than a3, and apparently the first plus a1 and a3 will be combined before that better results

Similarly the other side

code:

#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int n;
long long ans, a[1000100];
int main()
{
    cin >> n;
    for (int i = 0; i < n; i++){
        cin >> a[i];
    }
    for (int i = 1; i < n; i++){
        ans += max(a[i - 1], a[i]);
    }
    cout << ans;
}

Guess you like

Origin www.cnblogs.com/wzxbeliever/p/11865610.html