cf1090 I.Minimal Product(贪心)

时间:2018-12-10
本文章向大家介绍cf1090 I.Minimal Product(贪心),主要包括cf1090 I.Minimal Product(贪心)相关应用实例、知识点总结和注意事项,具有一定的参考价值,需要的朋友可以参考一下。

题意

题目链接

给出长度为\(n\)的序列\(a\),序列中的元素取值为\([-2e9, 2e9]\)

找到两个位置\((i, j) (i <j, a[i] < a[j])\),最小化\(a[i] * a[j]\)

Sol

当时在做的时候思路是直接维护大于\(0\)的最大/最小值,小于\(0\)的最大/最小值然从这四个里面转移

然而是有反例的,比如\(-100, -3, -5, -4\)

当时没有仔细往下想。

出现错误的本质原因还是因为负负的正的性质。

那么我们直接来分类讨论一下

整个序列可以分成四种情况

  • 全为正

这时候直接维护出前缀最小值

  • 存在一个位置为正数且前面有负数

同样维护前缀最小值

  • 前一半为正后一半为负

可分成两段分别做

  • 全为负

这是我自己没想出来的,看了dyh的代码只能Orzzz

这时候我们倒着考虑,不难发现一个小于\(0\)的数,乘上小于\(0\)的最大的数,得到的数一定是最小的。

那么直接维护一下最大值就好了。

#include<bits/stdc++.h>
#define LL long long 
//#define int long long 
#define uint unsigned int 
#define chmax(a, b) (a = (a > b ? a : b))
#define chmin(a, b) (a = (a < b ? a : b))
using namespace std;
const int MAXN = 1e7 + 10;
LL mod = 1ll << 32, INF = 9223372036854775806;
inline LL read() {
    char c = getchar(); LL x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int N;
LL L, R, X, Y, Z, a[MAXN];
uint b[MAXN];
LL add(LL x, LL y) {
    if(x + y < 0) return x + y + mod;
    return x + y >= mod ? x + y - mod : x + y;
}
LL mul(LL x, LL y) {
    return 1ll * x * y % mod;
}
void solve() {
    N = read(); 
    L = read(); R = read(); X = read(); Y = read(); Z = read(); b[1] = read(); b[2] = read();
    for(int i = 3; i <= N; i++) b[i] = (b[i - 2] * X % mod + b[i - 1] * Y % mod + Z) % mod; 
    for(int i = 1; i <= N; i++) a[i] = b[i] % (R - L + 1) + L;
    //puts("");for(int i = 1; i <= N; i++) printf("%d ", a[i]);
    //'for(int i = 1; i <= N; i++) a[i] = read();
    LL mn = INF, ans = INF;
    //cout << ans << endl;
    for(int i = 1; i <= N; i++) {
        if(mn < a[i]) chmin(ans, mn * a[i]);
        chmin(mn, a[i]);
    }
    mn = -INF;
    for(int i = N; i >= 1; i--) {
        if(a[i] < mn) chmin(ans, mn * a[i]);
        chmax(mn, a[i]);
    }
    if(ans == INF) printf("IMPOSSIBLE\n");
    else cout << ans << endl;
}
signed main() {
    for(int T = read(); T; T--, solve());
    return 0;
}