zoukankan      html  css  js  c++  java
  • HDU 6447

    Problem Description
    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) on the rectangle map and B (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) now (0≤x≤109,0≤y≤109), he will only forward to (x+1,y), (x,y+1) or (x+1,y+1).
    On the rectangle map from (0,0) to (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 k on (xk,yk) (1≤xk≤109,1≤yk≤109), only the one who was from (xk−1,yk−1) to (xk,yk) will be able to earn vk 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.

    Input
    The first line of the input contains an integer T (1≤T≤10),which is the number of test cases.
    In each case, the first line of the input contains an integer N (1≤N≤105).The following N lines, the k-th line contains 3 integers, xk,yk,vk (0≤vk≤103), which indicate that there is a village on (xk,yk) and he can get vk dollars in that village.
    The positions of each village is distinct.

    Output
    The maximum of dollars YJJ can get.

    Sample Input
    1
    3
    1 1 1
    1 2 2
    3 3 1

    Sample Output
    3

    Source
    2018中国大学生程序设计竞赛 - 网络选拔赛

    题意:

    从 (0,0)(0,0) 往 (109,109)(109,109) 走,每次只能从 (x,y)(x,y) 走到 (x+1,y)(x+1,y) 或者 (x,y+1)(x,y+1) 或者 (x+1,y+1)(x+1,y+1),不能折返,不能走重复的路,

    在地图上分布着一些村庄,给出村庄的坐标 (x,y)(x,y) 和 vv 值,当且仅当你从 (x1,y1)(x−1,y−1) 走到村庄时,你才可以获得 vv,求最大能获得多少 vv。

    题解:

    显然,假设我走到了某一个村庄 (x0,y0)(x0,y0),那么只有在 xx0+1x≥x0+1 且 yy0+1y≥y0+1 范围内的村庄,能使得我的 vv 值进一步增加,

    换句话说,我走到了某一个村庄 (x0,y0)(x0,y0),我上一个走到的“有意义的”村庄必然是在 xx01x≤x0−1且 yy01y≤y0−1 的范围内的,

    那么我就要在 xx01x≤x0−1 且 yy01y≤y0−1 的范围内找到某个村庄,我走到该村庄时,我获得的 vv值时最大的,

    故,我们假设 dp[i]dp[i] 为走到村庄 ii 时能获得的最大的 vv,则状态转移方程为:

    dp[i]=max(dp[j]+v[i])dp[i]=max(dp[j]+v[i]),其中村庄 jj 的坐标 (x,y)(x,y) 满足 xx01x≤x0−1 且 yy01y≤y0−1

    那么,简单地说,对于每个村庄,要能 O(logn)O(log⁡n) 获得某区域内的最大值,同时也要能 O(logn)O(log⁡n)的某区域内的最大值,自然而然想到树状数组……

    我们离散化纵坐标,并且从小到大枚举横坐标,用树状数组维护纵坐标为 [1,y][1,y] 区域内最大的dp[i],

    1、计算到某个横坐标值上某一村庄 dp[i]dp[i],假设其纵坐标为 yy,查询 [1,y1][1,y−1] 区域内最大值;

    2、每次计算完某个横坐标值的一竖条上的所有村庄的 dp[i]dp[i],将这一竖条上所有的 dp[i]dp[i] 全部拿去更新树状数组。

    附上bin巨代码

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 
     4 struct Node {
     5     int x, y, v;
     6     void input() {
     7         scanf("%d%d%d", &x ,&y, &v);
     8     }
     9 }node[100010];
    10 bool cmp(Node a, Node b) {
    11     return a.x < b.x;
    12 }
    13 
    14 int a[100010];
    15 int tot;
    16 
    17 int c[100010];
    18 int lowbit(int x) {
    19     return x&(-x);
    20 }
    21 
    22 void update(int i, int val) {
    23     while (i <= tot) {
    24         c[i] = max(c[i], val);
    25         i += lowbit(i);
    26     }
    27 }
    28 int query(int i) {
    29     int res = 0;
    30     while (i > 0) {
    31         res = max(res, c[i]);
    32         i -= lowbit(i);
    33     }
    34     return res;
    35 }
    36 
    37 int dp[100010];
    38 
    39 
    40 int main() {
    41     int T;
    42     int n;
    43     scanf("%d", &T);
    44     while (T--) {
    45         scanf("%d", &n);
    46         for (int i = 0; i < n; i++)
    47             node[i].input();
    48         tot = 0;
    49         for (int i = 0; i < n; i++) {
    50             a[tot++] = node[i].y;
    51         }
    52         sort(a, a+tot);
    53         tot = unique(a, a+tot) - a;
    54         for (int i = 0; i < n; i++) {
    55             node[i].y = lower_bound(a, a+tot, node[i].y) - a + 1;
    56         }
    57         sort(node, node+n, cmp);
    58         for (int i = 0; i < n; i++) dp[i] = node[i].v;
    59         for (int i = 1; i <= tot; i++) c[i] = 0;
    60         int pos = 0;
    61         int ans = 0;
    62         for (int i = 0; i < n; i++) {
    63             while (pos < i && node[pos].x < node[i].x) {
    64                 update(node[pos].y, dp[pos]);
    65                 pos++;
    66             }
    67             dp[i] = query(node[i].y - 1) + node[i].v;
    68             ans = max(ans, dp[i]);
    69         }
    70         printf("%d
    ",ans);
    71     }
    72     return 0;
    73 }
    View Code
  • 相关阅读:
    Ruby
    WebGL的第二个小程序
    wegGL的第一个小程序
    Node.js介绍
    接口隔离原则(Interface Sepreation Principle)
    参数
    字段/属性
    接口和抽象类
    javascript中的事件
    线性回归算法-4.多元线性回归算法
  • 原文地址:https://www.cnblogs.com/klaycf/p/9577658.html
Copyright © 2011-2022 走看看