2018CCPC网络赛
YJJ is a salesman who has traveled through western country. YJJ is always on journey. Either is he at the destination, or on the way to destination.
One day, he is going to travel from city A to southeastern city B. Let us assume that A is (0,0)(0,0) on the rectangle map and B (109,109)(109,109). YJJ is so busy so he never turn back or go twice the same way, he will only move to east, south or southeast, which means, if YJJ is at (x,y)(x,y) now (0≤x≤109,0≤y≤109)(0≤x≤109,0≤y≤109), he will only forward to (x+1,y)(x+1,y), (x,y+1)(x,y+1) or (x+1,y+1)(x+1,y+1).
On the rectangle map from (0,0)(0,0) to (109,109)(109,109), there are several villages scattering on the map. Villagers will do business deals with salesmen from northwestern, but not northern or western. In mathematical language, this means when there is a village kk on (xk,yk)(xk,yk) (1≤xk≤109,1≤yk≤109)(1≤xk≤109,1≤yk≤109), only the one who was from (xk−1,yk−1)(xk−1,yk−1) to (xk,yk)(xk,yk) will be able to earn vkvk dollars.(YJJ may get different number of dollars from different village.)
YJJ has no time to plan the path, can you help him to find maximum of dollars YJJ can get.
InputThe first line of the input contains an integer TT (1≤T≤10)(1≤T≤10),which is the number of test cases.
In each case, the first line of the input contains an integer NN (1≤N≤105)(1≤N≤105).The following NN lines, the kk-th line contains 3 integers, xk,yk,vkxk,yk,vk (0≤vk≤103)(0≤vk≤103), which indicate that there is a village on (xk,yk)(xk,yk) and he can get vkvk dollars in that village.
The positions of each village is distinct.OutputThe maximum of dollars YJJ can get.Sample Input
1
3
1 1 1
1 2 2
3 3 1
Sample Output
3
就是现在有一个棋盘,你可以向右或者向下走,如果向右下的话就可以得到积分
dp方程就是这样dp[i][j] = max{dp[i-1][j],dp[i][j-1],dp[i-1][j-1]+v[i][j]},
可以用BIT去维护这个dp
但是点很多啊,也许又长又宽,但是能到的点和这个是无关的,所以可以离散化
为啥要离散化呢,因为有T组啊,但是放不离散化的代码AC就很不良心了吧
离散化可以先把y读入,并排序去除相同部分,所以每个的y都可以通过二分找到其位置并进行编号,所以根据其x和y和排序就可以进行了
每次找的过程都是在维护这个区间,dp[i]表示第到第a[i].y列的最大值,
因为我是排序了的,所以设置一个pos去更新的话,一定是t[pos].x<=t[i].x,所以我们在pos需要的地方更新,另外y已经被我们编号了,所以t[pos].y的值是可以去更新的,所以就做完了
#include
using namespace std;
const int N=1e5+;
struct T
{
int x,y,v;
} t[N];
bool cmp(T a, T b)
{
return a.x
return res;
}
int dp[N];
int main()
{
int T,n;
scanf("%d", &T);
while (T--)
{
scanf("%d", &n);
for(int i=; i<n; i++)scanf("%d%d%d",&t[i].x,&t[i].y,&t[i].v);
tot=,memset(c,,sizeof c);
for(int i=; i<n; i++)a[tot++]=t[i].y;
sort(a,a+tot);
tot=unique(a,a+tot)-a;
for(int i=; i<n; i++)t[i].y=lower_bound(a,a+tot,t[i].y)-a+;
sort(t,t+n,cmp);
for(int i=; i<n; i++) dp[i]=t[i].v;
int pos=,ans=;
for(int i=; i<n; i++)
{
while(pos<i&&t[pos].x!=t[i].x)update(t[pos].y,dp[pos]),pos++;
dp[i]=query(t[i].y-)+t[i].v,ans=max(ans,dp[i]);
}
printf("%d\n",ans);
}
return ;
}
Time Limit: 60 Sec Memory Limit: 128 MB
Submit: 1828 Solved: 873
[Submit][Status][Discuss]
方伯伯在自己的农田边散步,他突然发现田里的一排玉米非常的不美。
这排玉米一共有N株,它们的高度参差不齐。
方伯伯认为单调不下降序列很美,所以他决定先把一些玉米拔高,再把破坏美感的玉米拔除掉,使得剩下的玉米的高度构成一个单调不下降序列。
方伯伯可以选择一个区间,把这个区间的玉米全部拔高1单位高度,他可以进行最多K次这样的操作。拔玉米则可以随意选择一个集合的玉米拔掉。
问能最多剩多少株玉米,来构成一排美丽的玉米。
第1行包含2个整数n,K,分别表示这排玉米的数目以及最多可进行多少次操作。
第2行包含n个整数,第i个数表示这排玉米,从左到右第i株玉米的高度ai。
输出1个整数,最多剩下的玉米数。
3 1
2 1 3
3
1 < N < 10000,1 < K ≤ 500,1 ≤ ai ≤5000
数组可以选择k个区间+1,让这个最长不下降序列最长
f[i][j]表示前i个数,用j次区间+1的LIS长度
那我们可以考虑你最优的操作是什么,就是包含最后一个数字,所以可以倒着就行求解
#include
#include
using namespace std;
int c[][];
int a[];
int query(int x,int y)
{
int ans=;
for(int i=x; i; i-=(i&(-i)))
for(int j=y; j; j-=(j&(-j)))
ans=max(ans,c[i][j]);
return ans;
}
inline void add(int x,int y,int val)
{
for(int i=x; i<; i+=(i&(-i)))
for(int j=y; j<; j+=(j&(-j)))
c[i][j]=max(c[i][j],val);
}
int main()
{
int n,m;
scanf("%d%d",&n,&m),m++;
for(int i=; i<=n; i++)scanf("%d",&a[i]);
int ans=;
for(int i=; i<=n; i++)
for(int j=m,t; j; j--)
{
t=query(a[i]+j,j)+,ans=max(ans,t);
add(a[i]+j,j,t);
}
printf("%d\n",ans);
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章