Codeforces 631E:Product Sum
来源:互联网 发布:怎么连接台湾的网络 编辑:程序博客网 时间:2024/06/06 00:46
Blake is the boss of Kris, however, this doesn't spoil their friendship. They often gather at the bar to talk about intriguing problems about maximising some values. This time the problem is really special.
You are given an array a of length n. The characteristic of this array is the value — the sum of the products of the valuesai by i. One may perform the following operation exactly once: pick some element of the array and move to any position. In particular, it's allowed to move the element to the beginning or to the end of the array. Also, it's allowed to put it back to the initial position. The goal is to get the array with the maximum possible value of characteristic.
The first line of the input contains a single integer n (2 ≤ n ≤ 200 000) — the size of the array a.
The second line contains n integers ai (1 ≤ i ≤ n, |ai| ≤ 1 000 000) — the elements of the array a.
Print a single integer — the maximum possible value of characteristic of a that can be obtained by performing no more than one move.
44 3 2 5
39
51 1 2 7 1
49
31 1 2
9
In the first sample, one may pick the first element and place it before the third (before 5). Thus, the answer will be3·1 + 2·2 + 4·3 + 5·4 = 39.
In the second sample, one may pick the fifth element of the array and place it before the third. The answer will be1·1 + 1·2 + 1·3 + 2·4 + 7·5 = 49.
题意是给出n个数,计算sum(i*a[i])i从1到n。然后你可以任意挪动一个数的位置,使得和最大,求和。
感觉是个三分的题,但是我没学过三分。。。
枚举每一个可能被挪动的元素,然后二分其前后位置,计算每一个位置产生的结果差值,求其最大。自己瞎搞水过的。。。
代码:
#pragma warning(disable:4996)#include <iostream>#include <functional>#include <algorithm>#include <cstring>#include <vector>#include <string>#include <cstdio>#include <cmath>#include <queue>#include <stack>#include <deque>#include <set>#include <map>using namespace std;typedef long long ll;#define INF 0x3fffffffffffffffconst ll mod = 1e9 + 7;const int maxn = 1e6 + 5;ll n, sum;ll val[maxn], pre[maxn];ll cal(ll pos, ll mid){ll res;if (pos > mid){res = (mid - pos)*val[pos] + (pre[pos - 1] - pre[mid - 1]);}else{res = (mid - pos)*val[pos] - (pre[mid] - pre[pos]);}return res;}void solve(){ll i, j, k;scanf("%I64d", &n);sum = 0;for (i = 1; i <= n; i++){scanf("%I64d", &val[i]);sum += i*val[i];pre[i] = val[i] + pre[i - 1];}ll le, ri, mid;ll ans = 0;for (i = 1; i <= n; i++){le = 1, ri = n;while (le < ri){mid = (le + ri) / 2;if (cal(i, mid) >= cal(i, mid + 1)){ri = mid;}else{le = mid + 1;}}ans = max(ans, cal(i, ri));}for (i = 1; i <= n; i++){le = 1, ri = n;while (le < ri){mid = (le + ri + 1) / 2;if (cal(i, mid) >= cal(i, mid - 1)){le = mid;}else{ri = mid - 1;}}ans = max(ans, cal(i, le));}printf("%I64d", ans + sum);}int main(){#ifndef ONLINE_JUDGE freopen("i.txt", "r", stdin);freopen("o.txt", "w", stdout);#endif solve();return 0;}
- Codeforces 631E:Product Sum
- codeforces 631E Product Sum (dp凸单调性)
- CodeForces 631E Product Sum(斜率优化DP+二分|三分)
- CodeForces 631E Product Sum(斜率优化DP+二分|三分) ★
- Codeforces Round #344 (Div. 2) E - Product Sum (斜率dp)
- Codeforces Round #344 (Div. 2) E. Product Sum (三分)
- Codeforces Round #344 (Div. 2) E - Product Sum(三分 (java))
- codeforces 488 E. Prefix Product Sequence
- Codeforces 616E Sum of Remainders
- Codeforces 616E Sum Of Reminders
- codeforces 616E - Sum of Remainders
- Codeforces 616E - Sum of Remainders
- Codeforces Round #278 (Div. 2) E. Prefix Product Sequence
- Codeforces Gym 100339I Sum vs Product 剪枝搜索
- Educational Codeforces Round 5 E. Sum of Remainders 数论
- Educational Codeforces Round 5 E. Sum of Remainders(数学)
- Codeforces educational round 5. E Sum of Remainders 数论 分块
- 【数论】【分块】【乱搞】codeforces 616E Sum of Remainders
- 国外程序员推荐:每个程序员都应读的书
- Android各种屏幕分辨率(VGA、HVGA、QVGA、WQVGA、WVGA、FWVGA) 详解
- HDU2519新生晚会(组合数公式)
- 蓝桥杯--2012--奇怪的比赛(全排列)
- Android新手入门2016(3)--Android真机调试
- Codeforces 631E:Product Sum
- C# 使用keybd_event()函数模拟键盘按键
- 【BZOJ1044】【tyvj3511】【codevs1870】木棍分割,二分答案+滚动数组+前缀和DP
- 【译】UNIVERSAL IMAGE LOADER.PART 2---ImageLoaderConfiguration详解
- kmeans++算法流程
- Codeforces #345 div1 D. Zip-line LIS dp
- Equipment uva1508
- 从两个有序数组的并集中寻找第k小元素
- bzoj 1085 骑士精神