您的位置:首页 > 编程语言 > C语言/C++

【codeforces 115E】Linear Kingdom Races 题意&题解&代码(c++)

2015-12-11 19:22 375 查看
**

E. Linear Kingdom Races

**

time limit per test5 seconds

memory limit per test256 megabytes

You are a car race organizer and would like to arrange some races in Linear Kingdom.

Linear Kingdom has n consecutive roads spanning from left to right. The roads are numbered from 1 to n from left to right, thus the roads follow in the order of their numbers’ increasing. There will be several races that may be held on these roads. Each race will use a consecutive subset of these roads. Also, each race will pay some amount of money to you if this race is held. No races overlap in time, so some roads can be used in several races.

Unfortunately, some of the roads are in a bad condition and they need repair. Each road has repair costs associated with it, you are required to pay this cost to repair the road. A race can only take place if all the roads used in the race are renovated. Your task is to repair such roads (possibly all or none) that will maximize your profit. Your profit is defined as the total money you get from the races that are held minus the total money you spent to repair the roads. Note that you may decide not to repair any road and gain zero profit.

Print the maximum profit you can gain.

Input

The first line contains two single-space separated integers, n and m (1 ≤ n, m ≤ 2·105), denoting the number of roads and the number of races, respectively.

Then n lines follow, each line will contain a single non-negative integer not exceeding 109 denoting the cost to repair a road. The costs are given in order from road 1 to road n.

Finally, m lines follow. Each line is single-space-separated triplets of integers. Each triplet will be given as lb, ub, and p (1 ≤ lb ≤ ub ≤ n, 1 ≤ p ≤ 109), which means that the race these three integers describe will use all the roads from lb to ub, inclusive, and if it’s held you get p.

Output

Print a single integer denoting the maximum possible profit you can gain.

Please, do not use the %lld specificator to read or write 64-bit integers in C++. It is recommended to use cin, cout stream (also you may use %I64d specificator).

input

7 4

3

2

3

2

1

2

3

1 2 5

2 3 5

3 5 3

7 7 5

output

4

input

2 1

0

3

1 2 5

output

2

input

3 1

10

10

10

1 3 10

output

0

Note

In the first sample the optimal solution is to repair roads 1, 2, 3, and 7. Three races will take place which nets you 15. The road repair costs 11, hence your profit is 4.

中文题意:有N条赛道,每一条都是需要修复的,修复第i条赛道的费用是cost[i];赛道上会举办m个赛事,每个赛事会用到[L,R]之间的赛道,而且要保证赛事进行必须使得这一段的赛道完好,每项赛事还可以获得一定的钱数。问题要求安排哪些比赛可以使得收益最大,输出最大的收益。

如果数据范围够小的话,很明显可以N*2的dp写过

dp【i】表示到第i条边的最大获益则

dp[0]=0;
for (int i=1;i<=n;i++)
for (int j=1;j<i;j++)
dp[i]=max(dp[i],dp[j]-cost[j+1][i]+val[j+1][i]);
//cost[j+1][i]表示从第j+1段赛道至第i条赛道都修理后所花费的总价值
//val[j+1][i]表示第j+1段赛道至第i条赛道全部修理后可以获得的总收益


然而这道题的数据范围使得N*2必然是过不了的,因此需要优化求

dp[i]=max(dp[i],dp[j]-cost[j+1][i]+val[j+1][i]);


的时间复杂度。可以将上述的dp写成这样的形式

int num=1;//表示第num个赛事
for (int i=1;i<=n;i++)
{
//p[num].l表示赛事的左端,p[num].r表示赛事的右端
//cost[i]表示修理第i条赛道的花费
for (int j=1;j<i;j++)
dp[j]-=cost[i];
//用来实现
//for (int j=1;j<i;j++)
//dp[i]=max(dp[i],dp[j]-cost[j+1][i]+val[j+1][i]);
//中 dp[j]-cost[j+1][i]的部分
while(p[num].r<=i&&num<=m)
{
for (int j=1;j<p[num].l;j++)
dp[j]+=p[num].val;//即 dp[j]+val[j+1][i]的部分
num++;
}

for (int j=1;j<i;j++)
dp[i]=max(dp[i],dp[j]);
}


这样看起来貌似复杂了许多,但是这样的话很容易看出来这些都是区间操作,那么就可以想到用线段数来优化时间。

代码如下:

#include<iostream>
#include<algorithm>
#include<stdio.h>
#define lson id*2
#define rson id*2+1
using namespace std;
struct edge{
long long mmax;long long lazy;
}tree[800005];
struct node{
long long x;long long y;long long v;
}p[200005];
long long dp[200005],cost[200005],ans=-1;
void push_up(long long id)
{
tree[id].mmax=max(tree[lson].mmax,tree[rson].mmax);
return ;
}
void push_down(long long id,long long l,long long r)
{
long long mid=(l+r)/2;
tree[lson].mmax+=tree[id].lazy;
tree[rson].mmax+=tree[id].lazy;
tree[lson].lazy+=tree[id].lazy;
tree[rson].lazy+=tree[id].lazy;
tree[id].lazy=0;
return ;
}
void build_tree(long long id,long long l,long long r)
{
tree[id].lazy=0;
if (l==r)
{
tree[id].mmax=0;
return ;
}
long long mid=(l+r)/2;
build_tree(lson,l,mid);
build_tree(rson,mid+1,r);
push_up(id);
return ;
}
void add_tree(long long id,long long l,long long r,long long L,long long R,long long v)
{
if (l>r||L>R) return ;
if (l>=L&&r<=R)
{
tree[id].mmax+=v;
tree[id].lazy+=v;
return ;
}
push_down(id,l,r);
long long mid=(l+r)/2;
if (mid>=L)
add_tree(lson,l,mid,L,R,v);
if (mid+1<=R)
add_tree(rson,mid+1,r,L,R,v);
push_up(id);
return ;
}
void query_tree(long long id,long long l,long long r,long long L,long long R)
{
if (l>=L&&r<=R)
{
ans=max(ans,tree[id].mmax);
return ;
}
push_down(id,l,r);
long long mid=(l+r)/2;
if (mid>=L)
query_tree(lson,l,mid,L,R);
if (mid+1<=R)
query_tree(rson,mid+1,r,L,R);
push_up(id);
return ;
}
long long cmp(node hh,node aa)
{
if  (hh.y!=aa.y)
return hh.y<aa.y;
else
return hh.x<aa.x;
}
long long n,m;
int main()
{
while(scanf("%I64d%I64d",&n,&m)!=EOF)//神奇的!=EOF
{
for (long long i=1;i<=n;i++)
scanf("%I64d",&cost[i]);
for (long long j=1;j<=m;j++)
scanf("%I64d%I64d%I64d",&p[j].x,&p[j].y,&p[j].v);
sort(p+1,p+1+m,cmp);
long long num=1;
dp[0]=0;
build_tree(1,0,n);
for (long long i=1;i<=n;i++)
{
add_tree(1,0,n,0,i-1,0-cost[i]);
while(p[num].y==i&&num<=m)
{
add_tree(1,0,n,0,p[num].x-1,p[num].v);
num++;
}
dp[i]=max(dp[i-1],tree[1].mmax);
add_tree(1,0,n,i,i,dp[i]);
}
printf("%I64d\n",dp
);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: