题意:给你一个数组,问你交换最多K个数以后,最大子串和为多少;
解题思路:枚举这个数组最大字串和的起点和终点,然后优先队列交换这段里面的小数去换外面的大数,即可求出答案!
解题代码:
1 // File Name: c.cpp 2 // Author: darkdream 3 // Created Time: 2014年04月28日 星期一 19时04分36秒 4 5 #include<vector> 6 #include<list> 7 #include<map> 8 #include<set> 9 #include<deque> 10 #include<stack> 11 #include<bitset> 12 #include<algorithm> 13 #include<functional> 14 #include<numeric> 15 #include<utility> 16 #include<sstream> 17 #include<iostream> 18 #include<iomanip> 19 #include<cstdio> 20 #include<cmath> 21 #include<cstdlib> 22 #include<cstring> 23 #include<ctime> 24 #include<queue> 25 using namespace std; 26 27 28 struct cmp1{ 29 bool operator()(int x,int y) 30 { 31 return x > y ; 32 } 33 }; 34 struct cmp2{ 35 bool operator()(int x,int y) 36 { 37 return x < y ; 38 } 39 }; 40 int a[300]; 41 int main(){ 42 43 44 int n , k ; 45 scanf("%d %d",&n,&k); 46 for(int i =1 ;i <= n;i ++) 47 { 48 scanf("%d",&a[i]); 49 } 50 int ans = a[1]; 51 priority_queue<int,vector<int>,cmp1> IN; 52 priority_queue<int,vector<int>,cmp2> OUT; 53 int sum[300]; 54 memset(sum,0,sizeof(sum)); 55 sum[1] = a[1]; 56 for(int i = 2;i <= n;i ++ ) 57 { 58 sum[i] = sum[i-1] + a[i]; 59 } 60 for(int begin = 1; begin <= n; begin ++ ) 61 for(int end = begin + 1 ; end <= n;end ++ ) 62 { 63 int tsum = sum[end] - sum[begin-1]; 64 ans = max(tsum,ans); 65 for(int i = begin;i <= end;i++) 66 { 67 IN.push(a[i]); 68 } 69 for(int i = 1;i < begin ;i ++) 70 OUT.push(a[i]); 71 for(int i = end+1 ; i <= n ;i ++) 72 OUT.push(a[i]); 73 74 for(int i = 1;i <= k;i ++) 75 { 76 if(IN.empty() || OUT.empty()) 77 break; 78 int t1 = IN.top(); 79 int t2 = OUT.top(); 80 IN.pop(); 81 OUT.pop(); 82 tsum += t2 -t1; 83 ans = max(tsum,ans); 84 } 85 while(!IN.empty()) IN.pop(); 86 while(!OUT.empty()) OUT.pop(); 87 } 88 printf("%d\n",ans); 89 return 0; 90 }
CF243 DIV2 C Sereja and Swaps(暴力),布布扣,bubuko.com
CF243 DIV2 C Sereja and Swaps(暴力)
原文:http://www.cnblogs.com/zyue/p/3697075.html