美文网首页
D,Equalize Them All CodeForces (

D,Equalize Them All CodeForces (

作者: Celia_QAQ | 来源:发表于2019-04-10 19:00 被阅读0次

    参考:

        Equalize Them All CodeForces - 1144D (贪心) - Suprit_Young's blog - CSDN博客


    题目:You are given an array aa consisting of nn integers. You can perform

    the following operations arbitrary number of times (possibly, zero):

    Choose

    a pair of indices (i,j)(i,j) such

    that |i−j|=1|i−j|=1 (indices ii and jj are adjacent) and

    set ai:=ai+|ai−aj|ai:=ai+|ai−aj|;

    Choose a pair of

    indices (i,j)(i,j) such that |i−j|=1|i−j|=1 (indices ii and jj are

    adjacent) and set ai:=ai−|ai−aj|ai:=ai−|ai−aj|.

    The value |x||x| means the absolute value of xx. For example, |4|=4|4|=4, |−3|=3|−3|=3.

    Your

    task is to find the minimum number of operations required to obtain the

    array of equal elements and print the order of operations to do it.

    It is guaranteed that you always can obtain the array of equal elements using such operations.

    Note that after each operation each element of the current array should not exceed 10181018 by absolute value.

    Input

    The first line of the input contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of elements in aa.

    The

    second line of the input

    contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤2⋅1050≤ai≤2⋅105),

    where aiai is the ii-th element of aa.

    Output

    In the first line print one integer kk — the minimum number of operations required to obtain the array of equal elements.

    In

    the next kk lines print operations itself. The pp-th operation should

    be printed as a triple of integers (tp,ip,jp)(tp,ip,jp), where tptp is

    either 11 or 22 (11 means that you perform the operation of the first

    type, and 22 means that you perform the operation of the second type),

    and ipip and jpjp are indices of adjacent elements of the array such

    that 1≤ip,jp≤n1≤ip,jp≤n, |ip−jp|=1|ip−jp|=1. See the examples for better

    understanding.

    Note that after each operation each element of the current array should not exceed 10181018 by absolute value.

    If there are many possible answers, you can print any.

    Examples

    Input

    5

    2 4 6 6 6

    Output

    2

    1 2 3

    1 1 2

    Input

    3

    2 8 10

    Output

    2

    2 2 1

    2 3 2

    Input

    4

    1 1 1 1

    Output

    0

    喵神的代码:(目前还看不懂。。。)

    A.Diverse Strings+B.Parity Alternated Deletions+C. Two Shuffled Sequences+D. Equalize Them All - 菜鸡成长史 - CSDN博客

    题意:两种操作,输出最少操作次数使得序列所有元素相同。操作一:选定相邻两个元素,较小的变成较大的元素;操作二:选定相邻元素,较大的变成较小的元素。

    思路:显然把元素变成出现次数最多的数即可。

    所用到的知识:栈STL之栈(stack)和队列(queue)的定义与基本用法 - 菜鸡成长史 - CSDN博客

    #include <bits/stdc++.h>

    using namespace std;

    const int maxn = 200000+5;

    struct p{

        int t,pos1,pos2;

    }f;

    int main()

    {

        int n; scanf("%d",&n);

        int vis[maxn]={0},a[maxn],ma=0;

        for(int i=1;i<=n;i++){

            scanf("%d",&a[i]);

            vis[a[i]]++;

            if(vis[a[i]]>vis[a[ma]]) ma=i;

        }

        if(vis[a[ma]]==n) puts("0");

        else{

            queue <p> q;

            for(int i=ma;i>1;i--)

                if(a[i-1]<a[ma]){

                    f.t=1,f.pos1=i-1,f.pos2=i;

                    q.push(f);

                }

                else if(a[i-1]>a[ma]){

                    f.t=2,f.pos1=i-1,f.pos2=i;

                    q.push(f);

                }

            for(int i=ma+1;i<=n;i++)

                if(a[i]<a[ma]){

                    f.t=1,f.pos1=i,f.pos2=i-1;

                    q.push(f);

                }

                else if(a[i]>a[ma]){

                    f.t=2,f.pos1=i,f.pos2=i-1;

                    q.push(f);

                }

            printf("%d\n",q.size());

            while(!q.empty()){

                f=q.front(); q.pop();

                printf("%d %d %d",f.t,f.pos1,f.pos2);

            }

        }

        return 0;

    }


    网上找的博客的代码:(Equalize Them All CodeForces - 1144D (贪心) - Suprit_Young's blog - CSDN博客

    题意:

    给出N个数, 对任意相邻的两个数ai, aj有两种操作

    set ai:=ai+|ai−aj|;

    set ai:=ai−|ai−aj|.

    求最少进行多少次操作, 可以使得所有数全部相等

    题解:

    CF题目的一贯套路, 仔细思考, 我们发现对于任意的两个数进行一次操作均可使其相等, 同时可以向左/向右传递.

    这样一来使得所有数均为出现次数最多的数毫无疑问就是最优策略了

    经验小结:

    有了简易思路后, 别急着去模拟操作, 先仔细想想能不能通过构造等思维方法来直接演示状态

    ---------------------

    作者:Suprit       原文:https://blog.csdn.net/a1097304791/article/details/89081637

    #include <cstdio>

    #include <iostream>

    #include <algorithm>

    #include <cstring>

    #include <string>

    #include <stdlib.h>

    #include <vector>

    #include <queue>

    #include <cmath>

    #include <stack>

    #include <map>

    #include <set>

    using namespace std;

    #define ms(x, n) memset(x,n,sizeof(x));

    typedef  long long LL;

    const int inf = 1<<30;

    const LL maxn = 2*1e5+10;

    int N, a[maxn];

    map<int, int> ma; //该数出现的次数

    int main()

    {

        int tag = 0, pos = 0;

        cin >> N;

        for(int i = 1; i <= N; i++){

            cin >> a[i];;

            ++ma[a[i]];

            if(ma[a[i]] > tag)

                tag = ma[a[i]], pos = i;

        }

        cout << N-tag << endl;

        for(int i = pos-1; i > 0; i--){

            if(a[i]!=a[pos]){

                if(a[i]<a[pos]) printf("1 %d %d\n",i,i+1);

                else printf("2 %d %d\n",i,i+1);

            }

        }

        for(int i = pos+1; i <= N; i++){

            if(a[i]!=a[pos]){

                if(a[i]<a[pos]) printf("1 %d %d\n",i,i-1);

                else printf("2 %d %d\n",i,i-1);

            }

        }

    return 0;//

    }

    相关文章

      网友评论

          本文标题:D,Equalize Them All CodeForces (

          本文链接:https://www.haomeiwen.com/subject/nuvoiqtx.html