CodeForces 1165E Two arrays and the sum of functions (思维)

Topic links: http://codeforces.com/contest/1165/problem/E

Meaning of the questions:
f(l,r) = \sum_{l<=i<=r}a[i]*b[i]

Change the order of the array B so that the \sum_{1<=l<=r<=n}f(l,r)minimum

\sum_{1<=l<=r<=n}f(l,r)=\sum_{1<=i<=n}a[i]*(i+1)*(n-i)*b[i]

We can put a [i] * (i + 1) * (n - i) can be processed to a predetermined value, and then sorted in descending order from small to large array b is multiplied is the answer

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <cmath>
#include <queue>
#define rep(i, s, e) for(int i = s; i < e; ++i)
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define P pair<int, int>
#define INF 0x3f3f3f3f
#define Mod 998244353
using namespace std;
typedef long long ll;
static const int N = 305;
static const int MAX_N = 2e5 + 5;
ll a[MAX_N], b[MAX_N];
void solve(){
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    //ios::sync_with_stdio(false);
    int n;
    while(scanf("%d", &n) != EOF){
        for(int i = 0; i < n; ++i){
            V LL; 
            Scanf ( " % I64d " , & V); 
            A [I] = V * (I + . 1 ) * (n-- I);     // this is not the first modulo, or later ordered greedy strategy will be a problem 
        }
         for ( int I = 0 ; I <n-; I ++) Scanf ( " % I64d " , & B [I]); 
        Sort (A, A + n-, Greater <LL> ()); 
        Sort (B, B + n-); 
        LL ANS = 0 ;
         for ( int I = 0; i < n; ++i) ans = (ans + (a[i] % Mod) * b[i]) % Mod;
        printf("%I64d\n", ans);
    }
}
int main() {
    solve();
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/xorxor/p/10954551.html