Codeforces 425A Sereja and Swaps(暴力枚举)
2014-04-28 13:21
393 查看
题目链接:A.
Sereja and Swaps
题意:给定一个序列,可以交换k次,问交换完后的子序列最大值的最大值是多少
思路:暴力枚举每一个区间,然后每个区间[l,r]之内的值先存在优先队列内,然后找区间外如果有更大的值就替换掉。求出每个区间的最大值,最后记录下所有区间的最大值
代码:
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; }
相关文章推荐
- CodeForces 550B Preparing Olympiad(DFS回溯+暴力枚举)
- CodeForces 593A 2Char(暴力枚举26个字母的组合)
- Codeforces 724B Batch Sort【暴力枚举】
- Codeforces 426 B Sereja and Mirroring【暴力枚举+模拟】水题
- codeforces 487 A Fight the Monster 暴力枚举
- codeforces 324# E. Anton and Ira (暴力枚举+贪心)
- CodeForces 550B Preparing Olympiad(DFS回溯+暴力枚举)
- codeforces 908 C. New Year and Curling【暴力枚举+图解】
- Codeforces 466B Wonder Room【思维+暴力枚举】直觉题
- Codeforces 626D Jerry's Protest(暴力枚举+概率)
- Codeforces 327A Flipping Game(区间DP,暴力枚举)
- Codeforces 769D k-Interesting Pairs Of Integers【思维+预处理+暴力枚举】
- CodeForces - 863B Kayaking 暴力枚举
- codeforces 675B B. Restoring Painting(暴力枚举)
- codeforces 577A(暴力枚举)
- CodeForces - 863B Kayaking 暴力枚举
- Codeforces-----233B---Non-square Equation暴力枚举
- codeforces 380B Sereja and Tree 暴力枚举
- Codeforces 425A Sereja and Swaps(暴力枚举)
- Codeforces Round #434 (Div. 2, based on Technocup 2018 Elimination Round 1)&&Codeforces 861B Which floor?【枚举,暴力】