题意:
给定一个长度为 的序列,其中一个连续子串的价值为该子串中所有数字之和(重复出现的数字只统计一次)。求价值第 大的子串的价值。
输入输出格式:
输入第一行两个整数 分别表示序列的长度和需要统计的第 大的和。
第二行 个数 表示整个整数序列。
输出一个整数表示第 大的价值。
样例
输入
7 5
3 -2 1 2 2 1 3输出
4样例解释
以下标 为开头的子串的价值分别为 。
以下标 为开头的子串的价值分别为 。
以下标 为开头的子串的价值分别为 。
以下标 为开头的子串的价值分别为 。
以下标 为开头的子串的价值分别为 。
以下标 为开头的子串的价值分别为 。
以下标 为开头的子串的价值为 。
此时,前 大的价值分别为 ,因此第 大的价值为 。
数据范围
对于 的数据,。
另有 的数据,。
对于 的数据,。
。
此时可以暴力枚举以每个下标为开头,之后在枚举结尾的过程中顺序将数插入。如果一个数已经在之前出现过,则忽略;如果没有出现过,则增加该子串的价值。离散化后用 维护或者直接使用如 维护即可。使用优先队列维护前 大的价值。
总时间复杂度 。
(感觉代码很好懂就不解释了)
const ll maxn=100010;
int n,k,sk;
int a[maxn],b[maxn];
priority_queue<ll,vector<ll>,std::greater<ll> >q;
map<int,int>m;
int main(){
freopen("kstring.in","r",stdin);
freopen("kstring.out","w",stdout);
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=n;i++)
m[a[i]]=1;
map<int,int>::iterator it=m.begin();
int cnt=1;
while(it!=m.end()){
it->second=cnt;
it++; cnt++;
}
for(int i=1;i<=n;i++)
b[i]=m[a[i]];
for(int i=1;i<=n;i++){
bitset<maxn>cnt;
cnt.reset();
ll sum=0;
for(int j=i;j<=n;j++){
if(cnt[b[j]]==0)
sum+=a[j];
cnt[b[j]]=1;
q.push(sum);
if(q.size()>k) q.pop();
}
}
printf("%lld",q.top());
return 0;
}(区分度是这样的())
考虑记录以 开头的所有子串的价值,然后转移至以 为开头。
例如样例 ,以下标 为开头的子串的价值分别为 。
显然,当删去第一个数时,以第 到以第 个数为结尾的子串的价值都要减去 。而由于第 个数也为 ,于是以第 个数为结尾的子串显然价值不变。
考虑通用转移,记录下标 对应的数下一次出现的位置为 。对于一次从第 位到 位转的转移,显然对于 到 价值都要减去 。
考虑用线段树维护以每一个下标为开头的子串最大的贡献,将每一颗线段树的最大值取出用优先队列维护。每次取出一个数就将其在原线段树上删除,再在原线段树上取出删除后的最大值放入优先队列中。
显然开 棵线段树并不现实,于是使用主席树维护。每棵树都由上一棵树通过转移的修改而来。
总时间复杂度 。
对于仙丹树封装中:
ll modify(ll l,ll r,ll k)为以最新一个版本的树为基础,将 到 中的每个数都加 ,会创建一个新的版本,返回根节点编号。
ll askmax(ll root,ll l,ll r);为查询以 为根的树在区间 到 的最大值。
ll askmaxl(ll root,ll l,ll r,ll k)为查询以 为根的树在区间 到 中等于 的数中最靠前的一个的下标。
(封装比较丑看接口就行)
主要代码:
const ll maxn=110000;
struct q4{
ll root;
ll max;
ll l,r;
bool operator<(const q4&b) const {
return max<b.max;
}
bool operator>(const q4&b) const {
return max>b.max;
}
};
ll n,k;
ll a[maxn];
ll next[maxn];
std::unordered_map<ll,ll>last;
ll r[maxn];
SegmentTree tr;
priority_queue<q4,std::vector<q4>,std::less<q4> >q;
int main(){
freopen("kstring.in","r",stdin);
freopen("kstring.out","w",stdout);
scanf("%lld%lld",&n,&k);
tr.init(n);
for(ll i=1,sum=0;i<=n;i++){
next[i]=n+1;
scanf("%lld",&a[i]);
if(last.find(a[i])!=last.end())
next[last[a[i]]]=i;
else
sum+=a[i];
last[a[i]]=i;
tr.modify(i,i,sum);
}
r[0]=tr.modify(1,1,0);
q.push({r[0],tr.askmax(r[0],1,n),1,n});
for(ll i=1;i<=n;i++){
r[i]=tr.modify(i,next[i]-1,-a[i]);
q.push({r[i],tr.askmax(r[i],i+1,n),i+1,n});
}
for(ll i=1;i<k;i++){
q4 t=q.top();
q.pop();
ll mid=tr.askmaxl(t.root,t.l,t.r,t.max);
if(t.l<mid) q.push({t.root,tr.askmax(t.root,t.l,mid-1),t.l,mid-1});
if(mid<t.r) q.push({t.root,tr.askmax(t.root,mid+1,t.r),mid+1,t.r});
}
q4 t=q.top();
printf("%lld",t.max);
return 0;
}完整代码:
(真动态开点仙丹树喵~)
#include <cstdio>
#include <iostream>
#include <vector>
#include <unordered_map>
#include <queue>
using std::max; using std::min; using std::priority_queue;
using std::cin; using std::cout; using std::cerr; using std::endl;
typedef long long ll;
const ll INFLL=1152921504606846976;
const ll ParallelArrayLen=128;
template<typename T>
class ParallelArray{
private:
std::vector<T*>d;
std::vector<ll>cnt;
public:
ParallelArray(){
d.push_back(new T[ParallelArrayLen]);
cnt.push_back(0);
}
ll push_back(T _d){
if(cnt[cnt.size()-1]>=ParallelArrayLen){
d.push_back(new T[ParallelArrayLen]);
cnt.push_back(0);
}
d[cnt.size()-1][cnt[cnt.size()-1]]=_d;
cnt[cnt.size()-1]++;
return cnt[cnt.size()-1]+(cnt.size()-1)*ParallelArrayLen-1;
}
ll size(){
return cnt[cnt.size()-1]+(cnt.size()-1)*ParallelArrayLen;
}
T& operator[](const ll& key){
// ll begk=key/ParallelArrayLen;
ll begk=key>>7;
ll endk=key-begk*ParallelArrayLen;
if(begk>=d.size()) return d[0][0];
return d[begk][endk];
}
};
struct SegmentTreeNode{
ll max,lazy;
ll lson,rson;
SegmentTreeNode() { max=0; lazy=0; lson=-1; rson=-1; }
};
class SegmentTree{
private:
ll n;
ParallelArray<SegmentTreeNode>t;
ParallelArray<ll>Root;
void updata(ll w){
ll lson=t[w].lson;
ll rson=t[w].rson;
t[w].max=0;
if(lson>0)
t[w].max=t[lson].max;
if(rson>0)
t[w].max=max(t[w].max,t[rson].max);
return;
}
void pushdown(ll w){
if(t[w].lazy==0) return;
ll lson=t[w].lson;
ll rson=t[w].rson;
if(lson>0){
int temp=t.push_back(SegmentTreeNode());
t[temp]=t[lson];
t[w].lson=temp;
t[temp].lazy+=t[w].lazy;
t[temp].max+=t[w].lazy;
}
if(rson>0){
int temp=t.push_back(SegmentTreeNode());
t[temp]=t[rson];
t[w].rson=temp;
t[temp].lazy+=t[w].lazy;
t[temp].max+=t[w].lazy;
}
t[w].lazy=0;
updata(w);
return;
}
void modify(ll l,ll r,ll L,ll R,ll k,ll w){
if(l<=L&&R<=r){
t[w].lazy+=k;
t[w].max+=k;
return;
}
pushdown(w);
ll lson=t[w].lson;
ll rson=t[w].rson;
ll mid=(L+R)>>1;
if(l<=mid){
ll temp=t.push_back(SegmentTreeNode());
if(lson>0)
t[temp]=t[lson];
t[w].lson=temp;
this->modify(l,r,L,mid,k,temp);
}
if(r>mid){
ll temp=t.push_back(SegmentTreeNode());
if(rson>0)
t[temp]=t[rson];
t[w].rson=temp;
this->modify(l,r,mid+1,R,k,temp);
}
updata(w);
return;
}
ll askmax(ll l,ll r,ll L,ll R,ll w){
if(l<=L&&R<=r)
return t[w].max;
pushdown(w);
ll lson=t[w].lson;
ll rson=t[w].rson;
ll mid=(L+R)>>1;
ll ret=-INFLL;
if(l<=mid&&lson>0)
ret=this->askmax(l,r,L,mid,lson);
if(r>mid&&rson>0)
ret=max(ret,this->askmax(l,r,mid+1,R,rson));
updata(w);
return ret;
}
ll askmaxl(ll l,ll r,ll L,ll R,ll k,ll w){
if(t[w].max<k)
return INFLL;
if(t[w].max==k&&L==R)
return L;
pushdown(w);
ll lson=t[w].lson;
ll rson=t[w].rson;
ll mid=(L+R)>>1;
ll ret=INFLL;
if(l<=mid&&lson>0){
ret=this->askmaxl(l,r,L,mid,k,lson);
if(ret<INFLL)
return ret;
}
if(r>mid&&rson>0)
return this->askmaxl(l,r,mid+1,R,k,rson);
return ret;
}
public:
void init(ll _n){
n=_n;
t.push_back(SegmentTreeNode());
Root.push_back(0);
for(ll i=1;i<=n;i++)
this->modify(i,i,1,n,0,0);
return;
}
ll modify(ll l,ll r,ll k){
ll temp=t.push_back(t[Root[Root.size()-1]]);
Root.push_back(temp);
this->modify(l,r,1,n,k,temp);
return temp;
}
ll askmax(ll root,ll l,ll r){
return this->askmax(l,r,1,n,root);
}
ll askmaxl(ll root,ll l,ll r,ll k){
return this->askmaxl(l,r,1,n,k,root);
}
};
const ll maxn=110000;
struct q4{
ll root;
ll max;
ll l,r;
bool operator<(const q4&b) const {
return max<b.max;
}
bool operator>(const q4&b) const {
return max>b.max;
}
};
ll n,k;
ll a[maxn];
ll next[maxn];
std::unordered_map<ll,ll>last;
ll r[maxn];
SegmentTree tr;
priority_queue<q4,std::vector<q4>,std::less<q4> >q;
int main(){
freopen("kstring.in","r",stdin);
freopen("kstring.out","w",stdout);
scanf("%lld%lld",&n,&k);
tr.init(n);
for(ll i=1,sum=0;i<=n;i++){
next[i]=n+1;
scanf("%lld",&a[i]);
if(last.find(a[i])!=last.end())
next[last[a[i]]]=i;
else
sum+=a[i];
last[a[i]]=i;
tr.modify(i,i,sum);
}
r[0]=tr.modify(1,1,0);
q.push({r[0],tr.askmax(r[0],1,n),1,n});
for(ll i=1;i<=n;i++){
r[i]=tr.modify(i,next[i]-1,-a[i]);
q.push({r[i],tr.askmax(r[i],i+1,n),i+1,n});
}
for(ll i=1;i<k;i++){
q4 t=q.top();
q.pop();
ll mid=tr.askmaxl(t.root,t.l,t.r,t.max);
if(t.l<mid) q.push({t.root,tr.askmax(t.root,t.l,mid-1),t.l,mid-1});
if(mid<t.r) q.push({t.root,tr.askmax(t.root,mid+1,t.r),mid+1,t.r});
}
q4 t=q.top();
printf("%lld",t.max);
return 0;
}(如果有看不懂的可以私信询问)
(神秘 做法)
我们考虑并不一定要枚举所有的子串才能找到最大值,更早开头的似乎会更有优势(尤其对于 为正整数的部分分)。于是暴力做法卡满时间()
赛事代码(节选):
const ll maxn=100010;
const ll maxtime=4800000;
int n,k,sk;
int a[maxn],b[maxn];
priority_queue<ll,vector<ll>,std::greater<ll> >q;
map<int,int>m;
int main(){
freopen("kstring.in","r",stdin);
freopen("kstring.out","w",stdout);
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=n;i++)
m[a[i]]=1;
map<int,int>::iterator it=m.begin();
int cnt=1;
while(it!=m.end()){
it->second=cnt;
it++; cnt++;
}
for(int i=1;i<=n;i++)
b[i]=m[a[i]];
for(int i=1;i<=n;i++){
if(clock()>maxtime) break;
bitset<maxn>cnt;
cnt.reset();
ll sum=0;
for(int j=i;j<=n;j++){
if(cnt[b[j]]==0)
sum+=a[j];
cnt[b[j]]=1;
q.push(sum);
if(q.size()>k) q.pop();
}
}
printf("%lld",q.top());
return 0;
}