Codeforces Round #462 (Div. 2)

20 篇文章 0 订阅
15 篇文章 0 订阅

A. A Compatible Pair

Nian is a monster which lives deep in the oceans. Once a year, it shows up on the land, devouring livestock and even people. In order to keep the monster away, people fill their villages with red colour, light, and cracking noise, all of which frighten the monster out of coming.
Little Tommy has n lanterns and Big Banban has m lanterns. Tommy’s lanterns have brightness a1, a2, …, an, and Banban’s have brightness b1, b2, …, bm respectively.
Tommy intends to hide one of his lanterns, then Banban picks one of Tommy’s non-hidden lanterns and one of his own lanterns to form a pair. The pair’s brightness will be the product of the brightness of two lanterns.
Tommy wants to make the product as small as possible, while Banban tries to make it as large as possible.
You are asked to find the brightness of the chosen pair if both of them choose optimally.
Input
The first line contains two space-separated integers n and m (2 ≤ n, m ≤ 50).
The second line contains n space-separated integers a1, a2, …, an.
The third line contains m space-separated integers b1, b2, …, bm.
All the integers range from  - 109 to 109.
Output
Print a single integer — the brightness of the chosen pair.
Examples
inputCopy
2 2
20 18
2 14
output
252
inputCopy
5 3
-1 0 1 2 3
-1 0 1
output
2
Note
In the first example, Tommy will hide 20 and Banban will choose 18 from Tommy and 14 from himself.
In the second example, Tommy will hide 3 and Banban will choose 2 from Tommy and 1 from himself.

题意:
T和S两个人且各有两个序列,T想从第一个序列中隐藏一个数,然后S再分别从两个序列中各取一个数,使乘积最大,T隐藏的目的是使成绩尽可能小;
分析:
有负数存在,就不要枚举最大值和次大值了,乘完后排序也不行,因为次大值和最大值可能来自于隐藏的那个数。对于第一个序列的每个值都和第二个序列乘一遍,保留最大值,最终排序取次大输出即可;(避免同一个数造成两个值)

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long LL;

const int mod = 1e9 + 7;
const int MAXN = 1e3 + 5;
LL a[55], b[3010], c[3010];

int main() {
    int n, m;
    scanf("%d %d", &n, &m);
    for(int i = 0; i < n; i++) {
        scanf("%lld", &a[i]);
    }
    int p = 0;
    for(int i = 0; i < m; i++) {
        scanf("%lld", &b[i]);
    }
    for(int i = 0; i < n; i++) {
        LL ans = -2e18;
        for(int j = 0; j < m; j++) {
             ans = max(ans, b[j] * a[i]);
        }
        c[p++] = ans;
    }
    sort(c, c + p);
    printf("%lld\n", c[p - 2]);
    return 0;
}

B - A Prosperous Lot

Apart from Nian, there is a daemon named Sui, which terrifies children and causes them to become sick. Parents give their children money wrapped in red packets and put them under the pillow, so that when Sui tries to approach them, it will be driven away by the fairies inside.
Big Banban is hesitating over the amount of money to give out. He considers loops to be lucky since it symbolizes unity and harmony.
He would like to find a positive integer n not greater than 1018, such that there are exactly k loops in the decimal representation of n, or determine that such n does not exist.
A loop is a planar area enclosed by lines in the digits’ decimal representation written in Arabic numerals. For example, there is one loop in digit 4, two loops in 8 and no loops in 5. Refer to the figure below for all exact forms.
Input
The first and only line contains an integer k (1 ≤ k ≤ 106) — the desired number of loops.
Output
Output an integer — if no such n exists, output -1; otherwise output any such n. In the latter case, your output should be a positive decimal integer not exceeding 1018.
Examples
input
2
output
462
input
6
output
8080

题意:
0~9这十个数字,0 4 6 9有一个环,8有两个环,给你一个n,输出1个带有n个环的数字(没有前导0);
分析:
SP,看数据范围,分奇偶输出8的数量;

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long LL;

const int INF = 0x3f3f3f3f;
const int MAXN = 2e3 + 10;

int main() {
    int n;
    scanf("%d", &n);
    if(n > 36) printf("-1\n");
    else {
        int m = n / 2;
        int k = n % 2;
        for(int i = 0; i < m; i++) {
            printf("8");
        }
        for(int i = 0; i < k; i++) {
            printf("4");
        }
    }
    printf("\n");
    return 0;
}

C. A Twisty Movement

A dragon symbolizes wisdom, power and wealth. On Lunar New Year’s Day, people model a dragon with bamboo strips and clothes, raise them with rods, and hold the rods high and low to resemble a flying dragon.
A performer holding the rod low is represented by a 1, while one holding it high is represented by a 2. Thus, the line of performers can be represented by a sequence a1, a2, …, an.
Little Tommy is among them. He would like to choose an interval [l, r] (1 ≤ l ≤ r ≤ n), then reverse al, al + 1, …, ar so that the length of the longest non-decreasing subsequence of the new sequence is maximum.
A non-decreasing subsequence is a sequence of indices p1, p2, …, pk, such that p1 < p2 < … < pk and ap1 ≤ ap2 ≤ … ≤ apk. The length of the subsequence is k.
Input
The first line contains an integer n (1 ≤ n ≤ 2000), denoting the length of the original sequence.
The second line contains n space-separated integers, describing the original sequence a1, a2, …, an (1 ≤ ai ≤ 2, i = 1, 2, …, n).
Output
Print a single integer, which means the maximum possible length of the longest non-decreasing subsequence of the new sequence.
Examples
inputCopy
4
1 2 1 2
output
4
inputCopy
10
1 1 2 2 2 1 1 2 2 1
output
9
Note
In the first example, after reversing [2, 3], the array will become [1, 1, 2, 2], where the length of the longest non-decreasing subsequence is 4.
In the second example, after reversing [3, 7], the array will become [1, 1, 1, 1, 2, 2, 2, 2, 2, 1], where the length of the longest non-decreasing subsequence is 9.

题意:
让你翻转一个区间过后,求非严格LIS;
分析:一开始当成连续的去写了。。。
之前遇到过类似的枚举区间问题。暴力翻转每一个区间,对这个区间求非严格递减最长(nlogn),这个区间左端的1的个数+右端的2的个数+区间LIS长度,一直取最大值就行了;
求非严格递减最长时,取符号转换为非严格LIS,二分求解就行了;

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long LL;

const int mod = 1e9 + 7;
const int MAXN = 2e3 + 5;
int dp[MAXN], L[MAXN], R[MAXN];
int a[MAXN], c[MAXN];

int main() {
    int n;
    scanf("%d", &n);
    for(int i = 1; i <= n; i++) {
        scanf("%d", &a[i]);
        c[i] = a[i] & 1;
    }
    int ans1 = 0, ans2 = 0;
    for(int i = 1; i <= n; ++i) {
        if(a[i] == 1) ans1++;
        L[i] = ans1;
    }
    for(int i = n; i >= 1; --i) {
        if(a[i] == 2) ans2++;
        R[i] = ans2;
    }
    int cnt = 0;
    for(int i = 1; i <= n; i++) {
        int p = 2;
        dp[1] = c[i];
        cnt = max(cnt, L[i - 1] + R[i + 1] + p - 1);
        for(int j = i + 1; j <= n; ++j) {
            int id = upper_bound(dp + 1, dp + p, c[j]) - dp;
            if(id >= p || c[j] >= dp[p - 1]) dp[p++] = c[j];
            else dp[id] = c[j];
            cnt = max(cnt, L[i - 1] + R[j + 1] + p - 1);
        }
        //printf("%d -> %d\n", i, p - 1);
    }
    printf("%d\n", cnt);
    return 0;
}

/*

10
2 2 2 1 2 2 1 1 1 1

*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值