题目链接:http://codeforces.com/contest/740/problem/B
【中文题意】现有n朵花,给你m组l和r,分别代表n朵花的一个子序列的左端点的位置和右端点的位置。每朵花在子序列出现的次数*它本身的价值就是可以增加的价值。这m个子序列你可以选择0-m个。但是每个最多只能选一次。
问你能增加的最大的价值是多少。
【思路分析】既然是增加的话,那么要求计入的每个子序列可以增加的价值都为正数,然后搞一个优先队列,把每个子序列可以产生的价值压进去,然后再把正的加上就好了。其实可以不用优先队列,每一次算出来大于0的话加上就好了。这个题我处理了一下数据,把所有的数据都加了100,为了方便计数。
【AC代码】
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
#define N 10000000000
#define LL long long
int a[105],book[205],book2[205],book3[205];
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
book[a[i]+100]++;
}
priority_queue<int>q;
int l,r;
for(int i=1;i<=m;i++)
{
for(int j=0;j<=200;j++)
{
book2[j]=0;
book3[j]=1;
}
scanf("%d%d",&l,&r);
for(int j=l;j<=r;j++)
{
book2[a[j]+100]++;
book3[a[j]+100]=0;
}
int sum=0;
for(int j=l;j<=r;j++)
{
if(book[a[j]+100]&&book2[a[j]+100]&&book3[a[j]+100]==0)
{
book3[a[j]+100]=1;
//printf("++%d %d\n",a[j],book2[a[j]+100]);
sum+=a[j]*book2[a[j]+100];
//printf("---%d\n",sum);
}
}
//printf("%d***\n",sum);
q.push(sum);
}
int re=0;
while(!q.empty())
{
int tt=q.top();
//printf("%d\n",tt);
q.pop();
if(tt>0)
{
re+=tt;
}
}
printf("%d\n",re);
}
return 0;
}