Codeforces when a Sereja and Swaps (brute force enumeration)

Source: Internet
Author: User

Link: A. Sereja and Swaps

Given a sequence, you can exchange k times and ask what is the maximum value of the subsequence after exchange

Idea: enumerate each interval with brute force, and the values in each interval [l, r] First exist in the priority queue, and then find out the value out of the interval and replace it if there is a larger value. Find the maximum value of each interval, and record the maximum value of all intervals.

Code:

By lab104_yifan, contest: Codeforces Round #243 (Div. 2), problem: (C) Sereja and Swaps, Accepted, # #include <stdio.h>#include <string.h>#include <queue>using namespace std;#define INF 0x3f3f3f3f#define max(a, b) ((a)>(b)?(a):(b))int n, k, a[205], i, j, vis[205];struct cmp {    bool operator() (int &a, int &b) {        return a > b;    }};int cal(int l, int r) {    priority_queue<int, vector<int>, cmp> Q;    int sum = 0, i;    for (i = l; i <= r; i++) {        sum += a[i];        Q.push(a[i]);    }    int kk = k;    memset(vis, 0, sizeof(vis));    while (kk--) {        int maxx = -INF, max_v;        for (i = 0; i < n; i++) {            if ((i >= l && i <= r) || vis[i]) continue;            if (maxx < a[i]) {                maxx = a[i];                max_v = i;            }        }        if (Q.top() < maxx) {            sum = sum - Q.top() + maxx;            Q.pop();            Q.push(maxx);            vis[max_v] = 1;        }    }    return sum;}int main() {    int ans = -INF;    scanf("%d%d", &n, &k);    for (i = 0; i < n; i++)        scanf("%d", &a[i]);    for (i = 0; i < n; i++)        for (j = i; j < n; j++) {            int t = cal(i, j);            ans = max(ans, t);        }    printf("%d\n", ans);    return 0;}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.