Educational Codeforces Round 96(Div。2の評価)B。Barrels(欲張りアルゴリズム)

トピックリンク: ポータル
トピックを投稿する:このトピック
ここに画像の説明を挿入も水の質問ですOO、@-@、==

質問:n個のバケツを与えます。i番目のバケツの水はaiです。あるバケツ(バケツa)の水を別のバケツ(バケツb)に注ぐことができますが、バケツaは空ではないことが前提です。このようにして、1回の操作でも、バケツaからバケツbにできるだけ多くの水を注ぐことができます(すべて問題ありません)。これで、この操作を最大k回実行できますが、バケツに最も多くの水を入れることがタスクになります。水の入ったバケツの差はできるだけ大きくなります。次に、操作方法を尋ね、最大の差を取得して出力します。

アイデア:まず、最大と最小の差を最大化する必要があります。水を注ぐたびに注ぐ必要があります。こうすることで、水に注がれるバケツを最大限に活用し、他のバケツの水量を最大限に活用できます。は直接0なので、差は最大ではありませんか?したがって、水の量が最も少ないバケツは空のバケツである必要があります。水量が最も多いバケツの水量が最も多い限り、差は最大になります(必要)。
したがって、大きいものから小さいものへ、小さいものから大きいものへの並べ替えは重要ではなく、kが減算されるか、iが条件を満たさずに終了するまで、毎回k–で最大のバケットを追加します。

    #include<bits/stdc++.h>
    namespace mySpace{
    
    
        typedef long long int ll;
        typedef long double ld;
        typedef double db;
        //typedef __int64 int bi;
        //nth_element(first,pos,end) =>STL function ,求第pos小的元素
        #define fori(a,b,c) for(int a=b;a<=c;++a)
        #define me(a) memset(a,0,sizeof a)
        #define Mod 1000000009
        #define exp 1e-8
        #define fi first
        #define se second
        #define sc_int(x) scanf("%d",&x)
        #define sc_db(x) scanf("%lf",&x)
        #define sc_str(x) scanf("%s",x)
        #define sc_bi(x) scanf("%I64d",&x)
        #define pr_int(x) printf("%d\n",x)
        #define pr_bi(x) printf("%lld\n",x)
        const int INF = 0x7fffffff;
        const int MAX1 = 2e5+10;
        const int MAX2 = 1e6+10;
        const int MAX3 = 2e6+10;
        //#define IS std::ios::sync_with_stdio(false)
        //#define OS std::cin.tie(NULL)
        void readFoler()
        {
    
    
            freopen("in.txt","r",stdin);
            freopen("out.txt","w",stdout);
        }
        void closeFoler()
        {
    
    
            fclose(stdin);
            fclose(stdout);
        }
    }
    using namespace std;
    using namespace mySpace;
    ll a[MAX1];
    int main(void)
    {
    
    
        int t,n,k;
        sc_int(t);
        while(t--)
        {
    
    
            scanf("%d %d",&n,&k);
            for(int i=1;i<=n;++i){
    
    
                scanf("%I64d",&a[i]);
            sort(a+1,a+1+n);
            ll sum = a[n];
            for(int i=n-1;i>=1;--i){
    
    
                if(k<=0 || a[i]==0) break;
                --k;
                sum+=a[i];
            }
            printf("%I64d\n",sum);

        }
    }

ここに画像の説明を挿入

おすすめ

転載: blog.csdn.net/YSJ367635984/article/details/109057280