582B - Once Again - CodeForces Solution


constructive algorithms dp matrices *1900

Please click on ads to support us..

Python Code:

f = lambda: map(int, input().split())
n, m = f()
t = list(f())
s = [0] * 301
d = s[:]
for i in t: d[i] += 1
for i in t * min(m, 2 * n): s[i] = max(s[:i + 1]) + 1
print(max(s) + max((m - n * 2) * max(d), 0))

C++ Code:

#include <bits/stdc++.h>

using namespace std;

const int mxN = 300;
int bit[mxN], cnt[mxN];

void update(int idx, int x) {
    int s = 0;
    for (; idx < mxN; idx = (idx | (idx+1))) {
        bit[idx] = max(bit[idx], x);
    }
}
int mx(int idx) {
    int s = 0;
    for (; idx >= 0; idx = (idx & (idx + 1)) - 1) {
        s = max(s, bit[idx]);
    }
    return s;
}
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n, t;
    cin >> n >> t;
    const int d = n*min(n+1, t); // mozda iz n u n+1??
    int a[d];
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        a[i]--;
        cnt[a[i]]++;
    }
    for (int i = n; i < d; ++i) {
        a[i] = a[i-n];
    }
    int dp[d];
    for (int i = 0; i < d; ++i) {
        dp[i] = mx(a[i])+1;
        update(a[i], dp[i]);
    }
    int x = 0;
    for (int i = 0; i < 300; ++i) {
        if (cnt[x] < cnt[i]) {
            x = i;
        }
    }
    long long ans = mx(300);
    if (t > n+1) {
        ans+= (t-n-1)*cnt[x];
    }
    cout << ans << '\n';
}


Comments

Submit
0 Comments
More Questions

973. K Closest Points to Origin
969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST
445. Add Two Numbers II
442. Find All Duplicates in an Array
437. Path Sum III
436. Find Right Interval