Codeforces 425A Sereja and Swaps(暴力枚舉)

來源:互聯網
上載者:User

題目連結:A. Sereja and Swaps

題意:給定一個序列,可以交換k次,問交換完後的子序列最大值的最大值是多少

思路:暴力枚舉每一個區間,然後每個區間[l,r]之內的值先存在優先隊列內,然後找區間外如果有更大的值就替換掉。求出每個區間的最大值,最後記錄下所有區間的最大值

代碼:

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;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.