Codeforces 332B Maximum Absurdity(暴力)
2016-02-11 23:13
459 查看
题意:
代码:
给你一个序列,让你在里面选择两个不想交的长度为k的字段,是的和最大。 我第一次做还是用的dp,感觉复杂度有点高啊,后来发现直接预处理就好了。 预处理需要三个数组,分别是sum[i],Max[i],Max_id[i]。 sum[i]表示以i开头的长度为k的子段和 Max[i]表示在i之后的和最大的长度为k的字段和 Max_id[i]和上面对应,就是那个最大子段和的其实位置。 这样处理好之后=,直接枚举一个,就可以找到另一个了,注意多解字典序。
代码:
// // Created by CQU_CST_WuErli // Copyright (c) 2016 CQU_CST_WuErli. All rights reserved. // // #include<bits/stdc++.h> #include <iostream> #include <cstring> #include <cstdio> #include <cstdlib> #include <cctype> #include <cmath> #include <string> #include <vector> #include <list> #include <map> #include <queue> #include <stack> #include <set> #include <algorithm> #include <sstream> #define CLR(x) memset(x,0,sizeof(x)) #define OFF(x) memset(x,-1,sizeof(x)) #define MEM(x,a) memset((x),(a),sizeof(x)) #define For_UVa if (kase!=1) cout << endl #define BUG cout << "I am here" << endl #define lookln(x) cout << #x << "=" << x << endl #define SI(a) scanf("%d",&a) #define SII(a,b) scanf("%d%d",&a,&b) #define SIII(a,b,c) scanf("%d%d%d",&a,&b,&c) #define rep(flag,start,end) for(int flag=start;flag<=end;flag++) #define Rep(flag,start,end) for(int flag=start;flag>=end;flag--) #define Lson l,mid,rt<<1 #define Rson mid+1,r,rt<<1|1 #define Root 1,n,1 #define BigInteger bign const int MAX_L=2005;// For BigInteger const int INF_INT=0x3f3f3f3f; const long long INF_LL=0x7fffffff; const int MOD=1e9+7; const double eps=1e-9; const double pi=acos(-1); typedef long long ll; using namespace std; const int N=2e5+10; int n,k; ll a ,sum ; ll Max ; int Max_id ; int main() { #ifdef LOCAL freopen("C:\\Users\\john\\Desktop\\in.txt","r",stdin); // freopen("C:\\Users\\john\\Desktop\\out.txt","w",stdout); #endif while (SII(n,k)==2) { rep(i,1,n) SI(a[i]); ll tmp=0LL; rep(i,1,k) tmp+=a[i]; sum[1]=tmp; rep(i,2,n-k+1) { tmp=tmp-a[i-1]+a[i+k-1]; sum[i]=tmp; } int m=n-k+1; rep(i,1,m) Max[i]=sum[i],Max_id[i]=i; Rep(i,m-1,1) { if (Max[i+1]>Max[i]) { Max[i]=Max[i+1]; Max_id[i]=Max_id[i+1]; } else if (Max[i+1]==Max[i]) { Max[i]=Max[i+1]; Max_id[i]=i; } else { Max[i]=sum[i]; Max_id[i]=i; } } ll ans=0; int L,R; rep(i,1,n-2*k+1) { tmp=sum[i]+Max[i+k]; if (tmp>ans) { ans=tmp; L=i;R=Max_id[i+k]; } } printf("%d %d\n",L,R); } return 0; }
相关文章推荐
- I - Long Distance Racing(第二季水)
- VS2015中cout、cin未声明的标识符错误的处理
- 111. Minimum Depth of Binary Tree LeetCode
- 基于MySQLi和jQuery的评论系统
- 225. Implement Stack using Queues LeetCode
- Codeforces 519D A and B and Interesting Substrings (简单hash)
- 【CodeForces 618C】Constellation
- java基础12多线程
- 9. Palindrome Number LeetCode
- bzoj1005: [HNOI2008]明明的烦恼
- 基于SDP的提议/应答(offer/answer)模型简介
- javascript笔记7-事件
- 【Codeforces 282E】Sausage Maximization 中文题意&题解&代码(C++)
- Linux文件和目录的读、写、执行权限总结
- Atitit..net clr il指令集 以及指令分类 与指令详细说明
- 112. Path Sum LeetCode
- 9.数据结构之二叉树
- Atitit..net clr il指令集 以及指令分类 与指令详细说明
- Atitit..net clr il指令集 以及指令分类 与指令详细说明
- HDU 2126(01背包扩展,记录方案数)