D. Array Splitting(后缀数组)
You are given an array 1,2,…, and an integer
.
You are asked to divide this array into
non-empty consecutive subarrays. Every element in the array should be included in exactly one subarray. Let () be the index of subarray the -th element belongs to. Subarrays are numbered from left to right and from 1 to
.
Let the cost of division be equal to ∑=1(⋅())
. For example, if =[1,−2,−3,4,−5,6,−7] and we divide it into 3 subbarays in the following way: [1,−2,−3],[4,−5],[6,−7], then the cost of division is equal to 1⋅1−2⋅1−3⋅1+4⋅2−5⋅2+6⋅3−7⋅3=−9
.
Calculate the maximum cost you can obtain by dividing the array
into
non-empty consecutive subarrays.
The first line contains two integers
and (1≤≤≤3⋅105
).
The second line contains
integers 1,2,…, (||≤106
).
Print the maximum cost you can obtain by dividing the array
into
nonempty consecutive subarrays.
5 2
-1 -2 5 -4 8
15
7 6
-3 0 -1 -2 -2 -4 -1
-45
4 1
3 -1 6 0
8
题解:将n个数的数组分成k个连续的子数组,并且第i个子数组的权值为i,则我们可以用后缀和。首先我们一定每个数都得至少取一次,则我么一定要取a[1],然后将后面的n-1个数排序,
因为题目要求获得答案最大,并且这后n-1个数我们任意取k-1个都能保证符合题意中的分法,则为了保证答案最大我们就要取比较大的前k-1个了~~
#include<bits/stdc++.h>
#include<iostream>
#include<stdio.h>
#include<iomanip>
#include<stack>
#include<queue>
#include<algorithm>
#include<cstring>
#include<map>
#include<vector>
#include<numeric>
#include<iterator>
#include<cmath>
#define mem(a,x) memset(a,x,sizeof(a));
using namespace std;
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a%b); }
int lcm(int a, int b) { return a * b / gcd(a, b); }
const int INF = 0x3f3f3f3f;
typedef long long ll;
const int mod=1000000007;
typedef pair<int,int>Pi;
typedef pair<ll, ll>Pii;
map<int,int>mp;
map<int, char *>mp1;
map<char *, int>mp2;
map<char, int>mp3;
map<string,int>mp4;
map<char,int>mp5;
const int maxn = 300010;
ll a[maxn]; int read(){
int flag=1;
int sum=0;
char c=getchar();
while(c<'0'||c>'9'){
if(c=='-')flag=-1;
c=getchar();
}
while(c>='0'&&c<='9'){
sum=sum*10+c-'0';
c=getchar();
}
return sum*flag;
}
ll Read(){
int flag=1;
ll sum=0;
char c=getchar();
while(c<'0'||c>'9'){
if(c=='-')flag=-1;
c=getchar();
}
while(c>='0'&&c<='9'){
sum=sum*10+c-'0';
c=getchar();
}
return sum*flag;
}
ll quickmul(ll a,ll b){
ll ans=0;
while(b){
if(b&1){
ans=(ans+a)%mod;
}
a=(a+a)%mod;
b>>=1;
}
return ans;
}
ll quickpow(ll a,ll b){
ll ans=1;
while(b){
if(b&1)
ans=(ans*a)%mod;
a=(a*a)%mod;
b>>=1;
}
return ans;
}
int main()
{
int n,k;
cin>>n>>k;
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=n;i>=1;i--)a[i]+=a[i+1];
sort(a+2,a+1+n,greater<ll>());
ll ans=0;
for(int i=1;i<=k;i++)ans+=a[i];
cout<<ans<<endl;
return 0;
}
D. Array Splitting(后缀数组)的更多相关文章
- CodeForces - 1175D Array Splitting(数组划分+后缀和+贪心)
You are given an array a1,a2,…,ana1,a2,…,an and an integer kk. You are asked to divide this array in ...
- 后缀数组(suffix array)详解
写在前面 在字符串处理当中,后缀树和后缀数组都是非常有力的工具. 其中后缀树大家了解得比较多,关于后缀数组则很少见于国内的资料. 其实后缀数组是后缀树的一个非常精巧的替代品,它比后缀树容易编程实现, ...
- 后缀数组(suffix array)
参考: Suffix array - Wiki 后缀数组(suffix array)详解 6.3 Suffix Arrays - 算法红宝书 Suffix Array 后缀数组 基本概念 应用:字 ...
- 利用后缀数组(suffix array)求最长公共子串(longest common substring)
摘要:本文讨论了最长公共子串的的相关算法的时间复杂度,然后在后缀数组的基础上提出了一个时间复杂度为o(n^2*logn),空间复杂度为o(n)的算法.该算法虽然不及动态规划和后缀树算法的复杂度低,但其 ...
- 笔试算法题(40):后缀数组 & 后缀树(Suffix Array & Suffix Tree)
议题:后缀数组(Suffix Array) 分析: 后缀树和后缀数组都是处理字符串的有效工具,前者较为常见,但后者更容易编程实现,空间耗用更少:后缀数组可用于解决最长公共子串问题,多模式匹配问题,最长 ...
- 数据结构之后缀数组suffix array
在字符串处理当中,后缀树和后缀数组都是非常有力的工具,其中后缀树大家了解得比较多,关于后缀数组则很少见于国内的资料.其实后缀是后缀树的一个非常精巧的替代品,它比后缀树容易编程实现,能够实现后缀树的很多 ...
- suffix array后缀数组
倍增算法 基本定义子串:字符串 S 的子串 r[i..j],i≤j,表示 r 串中从 i 到 j 这一段也就是顺次排列 r[i],r[i+1],...,r[j]形成的字符串. 后缀:后缀是指从某个位置 ...
- 后缀数组 (Suffix Array) 学习笔记
\(\\\) 定义 介绍一些写法和数组的含义,首先要知道 字典序 . \(len\):字符串长度 \(s\):字符串数组,我们的字符串存储在 \(s[0]...s[len-1]\) 中. \(suff ...
- Suffix Array 后缀数组
后缀数组 顾名思义.SuffixArray(下面有时简称SA) 和字符串的后缀有关. 后缀:字符串中某个位置一直到结尾的子串.(SA中讨论包含了原串和空串).所以共同拥有len+1个后缀. 后缀数组: ...
随机推荐
- Day 6:Vector类和实现Hashset以及登录窗口的模拟
LinkedList作业:生成扑克牌并且洗牌? import java.util.*; class Poker{ String color; String number; public Poker(S ...
- Ubuntu下运行python文件
方法一: 直接> python2.7/python3.6 test.py 方法二: 在文件首行写上 #!/usr/bin/python3 这个是配置的路径 也可以通过 > which p ...
- 使用文件流创建File文件和目录以及其他的一些操作
我们创建文件时可以直接通过File f=new File(path)来创建一个文件对象,然后再通过 f.createNewFile() 就创建出来了一个文件.比如设置 path 为 C:\Users\ ...
- float 格式化到TCHAR 数组
<span style="white-space:pre"> </span>float lCount = 123.3; TCHAR tcBalance[MA ...
- vue element-ui Table数据解除自动响应方法
在对列表Table进行数据编辑时,会存在table的增删改操作后,列表view也自动响应发生了变化,原因是赋值的数据是一个引用类型共享一个内存区域的.所以我们就不能直接连等复制,需要重新克隆一份新的数 ...
- 计算广告-GD广告
算法 分配算法主要是解优化问题. 流量预测(traffic forecasting), 流量预估 库存分配, 粗力度的分配. 流量分配, 排单算法 在线分配(Online Allocation) 资料 ...
- jenkins忘记登录密码解决方法
第一步:修改配置文件 修改jenkins的配置文件,找到如下几行删除(删除前一定要备份) <useSecurity>true</useSecurity> <authori ...
- E - Tokitsukaze and Duel CodeForces - 1190C (博弈 + 窗体移动)
"Duel!" Betting on the lovely princess Claris, the duel between Tokitsukaze and Quailty ha ...
- Java并发分析—Lock
1.Lock 和 Condition 当使用synchronied进行同步时,可以在同步代码块中只用常用的wait和notify等方法,在使用显示锁的时候,将通过Condition对象与任意Lock实 ...
- 从编程实现角度学习Faster R-CNN(附极简实现)
https://www.jianshu.com/p/9da1f0756813 从编程实现角度学习Faster R-CNN(附极简实现) GoDeep 关注 2018.03.11 15:51* 字数 5 ...