【codeforces 798D】Mike and distribution

Keywords: iOS

[Title link] http://codeforces.com/contest/798/problem/D

[title]

Let you select a subscript set
p1,p2,p3..pk
Make 2*(a[p1]+a[p2]+. +a[pk])>ai
At the same time, 2* (b [p1]+b [p2]+. +b [pk])> _bi

[Abstract]

Both formulas can be transformed into
A [p1]+a [p2]+. +a [pk]>The remaining elements
(You can get it by moving items)
Then we use the method of structure.
First, descending the A array;
(Record each element's original subscript when arranging)
That is a[i].val and a[i].id.
Then add a[1].id to the answer.
For the remaining n-1
Processing in pairs sequentially
Namely
For a[i] and a[i+1]
Look at which big is b[a[i].id] and b[a[i+1].id];
Which is bigger?
Add the corresponding id to the answer sequence.
Because a is in descending order;
And the largest a has been put in the head position.
This ensures that there is a smaller AI for each ai.
And we add big b to the answer in pairs each time.
It also ensures that every bi has a smaller bi than him.
Great idea.

[Number Of WA]

0

[Complete code]

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1e5+100;

struct abc
{
    int val,id;
};

int n,b[N];
abc a[N];
vector <int> ans;

bool cmp1(abc a,abc b)
{
    return a.val > b.val;
}

int main()
{
    //freopen("F:\\rush.txt","r",stdin);
    ios::sync_with_stdio(false),cin.tie(0);//scanf,puts,printf not use
    cin >> n;
    rep1(i,1,n)
        cin >> a[i].val,a[i].id = i;
    rep1(i,1,n)
        cin >> b[i];
    sort(a+1,a+1+n,cmp1);
    ans.pb(a[1].id);
    for (int i = 2;i <= n;i+=2)
    {
        int po = a[i].id;
        if (i+1<=n && b[a[i+1].id]>b[po])
            po = a[i+1].id;
        ans.pb(po);
    }
    int len = ans.size();
    cout << len << endl;
    rep1(i,0,len-1)
        cout << ans[i] <<' ';
    return 0;
}

Posted by steviemac on Wed, 13 Feb 2019 16:33:18 -0800