24h購物| | PChome| 登入
2013-12-10 08:32:55| 人氣2,781| 回應0 | 上一篇 | 下一篇

[UVA][dp] 10421 - Critical Wave

推薦 0 收藏 0 轉貼0 訂閱站台

Problem C
Critical Wave

Input: standard input
Output: standard output
Time Limit: 10 seconds

 

The task is simple. Through some critical points in 2D, you are to draw a wave like curve. Your goal is to include as many points as possible.

  • There will be an imaginary line y = a, which we call the major axis for the curve.
  • All the points on the curve should have different x coordinates. Their y coordinates should be of form a-1 or a+1.

Two consecutive points on the curve should have a difference of 2 in their y coordinate

 

Input

There will be no more than 222 test cases. Each test case starts with an integer N, the number of points in the test case. In the next N lines, there will be N pair of integers giving the x and y coordinate of the points. There will be no more than 1000 points in each test case. All coordinates are integers -- they'd fit in an signed 2 byte integer data type.

 

Output

For each test case print a number -- the maximum number of critical points that can be included in a curve drawn from the given points.

 

Sample Input

10
0 1
1 0
1 -1
2 -2
3 1
3 -1
3 -2
4 1
4 -1

5 –1

10
0 1
1 0
1 -1
2 -2
3 1
3 -1
3 -2
4 1
4 -1

5 –1

 

Sample Output

4

4


Problem-setter: Monirul Hasan Tomal

 

 

 

“If you don’t consider your life as a journey you will advance nowhere and life will appear to you as an endless and hopeless track.”


這裡用最樸素的 O(n^2) 計算 LIS。

題目描述很簡單,找一條 y = a,並且找到一組數列符合由左而右 x 座標都不同。
且上下上下 y 值差 2 的方式擺動。

由於沒有給定 a 值,因此使用 dp 去計算。狀態分成兩種,

以它為結尾時,它前一次是上、它前一次是下。

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
struct Pt {
    int x, y;
    Pt(int a=0, int b=0) :
        x(a), y(b) {}
    bool operator<(const Pt &a) const {
        if(a.x != x)
            return x < a.x;
        return y < a.y;        
    }
};
int main() {
    int n;
    int i, j, k;
    Pt D[1005];
    while(scanf("%d", &n) == 1) {
        for(i = 0; i < n; i++)
            scanf("%d %d", &D[i].x, &D[i].y);
        sort(D, D+n);
        int ret = 0;
        int dp[1005][2];
        for(i = 0; i < n; i++) {
            dp[i][0] = dp[i][1] = 1;
            for(j = i-1; j >= 0; j--) {
                if(D[i].x == D[j].x)
                    continue;
                if(D[i].y == D[j].y+2)
                    dp[i][0] = max(dp[i][0], dp[j][1]+1);
                if(D[i].y == D[j].y-2)
                    dp[i][1] = max(dp[i][1], dp[j][0]+1);
            }
            ret = max(ret, dp[i][0]);
            ret = max(ret, dp[i][1]);
        }
        printf("%d\n", ret);
    }
    return 0;
}

台長: Morris
人氣(2,781) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 教育學習(進修、留學、學術研究、教育概況) | 個人分類: UVA |
此分類下一篇:[UVA][floyd] 10436 - Cheapest way
此分類上一篇:[UVA][空間幾何] 10495 - Conic Distance

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文