poj1054--The Troublesome Frog(枚举+二分)

题目链接:点击打开链接

题目大意:青蛙经过一块农田,每一次跳相同的距离,经过的点的植物被踩坏,给出n个被踩坏的坐标,问危害最大的一个青蛙踩坏了几块植物。(最少要有三个,否则是0)

每一次跳的距离相同,枚举最先开始的两个点p[i],p[j],得到距离差(x,y)来计算之后的点的坐标,用二分查找该点是否被踩坏,找出最大值。

优化:1、枚举的是最先开始的两个点,所以p[i].x-x,p[i].y-y应该在农田外,

2、判断p[j].x+max1*x,p[j].y+max1*y如果在农田外,那么他不可能被踩坏更多的点,也就不用去计算了。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;
struct node{
    int x , y ;
}p[5100];
int n , r , c , max1 ;
int x , y ;
int cmp(node a,node b) {
    return a.x < b.x || (a.x == b.x && a.y < b.y) ;
}
int f(int x,int y) {
    if( x > 0 && x <= r && y > 0 && y <= c ) return 1 ;
    return 0 ;
}
int search1(int x,int y) {
    int low = 0 , mid , high = n-1 ;
    while( low <= high ) {
        mid = (low+high)/2 ;
        if( p[mid].x == x && p[mid].y == y ) return 1 ;
        if( p[mid].x < x || (p[mid].x == x && p[mid].y < y) ) low = mid+1 ;
        else high = mid-1 ;
    }
    return 0 ;
}
int main() {
    int i , j , k , num ;
    while( scanf("%d %d", &r, &c) != EOF ) {
        scanf("%d", &n) ;
        max1 = 0 ;
        for(i = 0 ; i < n ; i++)
            scanf("%d %d", &p[i].x, &p[i].y) ;
        sort(p,p+n,cmp) ;
        for(i = 0 ; i < n ; i++) {
            for(j = i+1 ; j < n ; j++) {
                if( i == j ) continue ;
                x = p[j].x - p[i].x ;
                y = p[j].y - p[i].y ;
                if( f(p[i].x-x,p[i].y-y) || !f(p[j].x+max1*x,p[j].y+max1*y)  ) continue ;
                num = 0 ;
                for(k = 1 ; f(p[j].x+k*x,p[j].y+k*y) ; k++) {
                    if( search1(p[j].x+k*x,p[j].y+k*y) ) num++ ;
                    else {
                        num = 0 ; break ;
                    }
                }
                max1 = max(max1,num) ;
            }
        }
        if(max1) max1 += 2 ;
        printf("%d\n", max1) ;
    }
    return 0 ;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-16 08:58:01

poj1054--The Troublesome Frog(枚举+二分)的相关文章

POJ1054 The Troublesome Frog [dp]

艰难的写上一篇,小学期太累了,,,很难坚持刷 #include <iostream> #include <cmath> #include <cstring> #include <cstdio> #include <cstdlib> #include <algorithm> using namespace std; int m,n; int num; struct Grid { int x,y; }grid[5555]; int cmp

[poj1054] The Troublesome Frog 暴力枚举+剪支

The Troublesome Frog Time Limit: 5000MS Memory Limit: 100000K Total Submissions: 11339 Accepted: 3383 Case Time Limit: 500MS Description In Korea, the naughtiness of the cheonggaeguri, a small frog, is legendary. This is a well-deserved reputation, b

IOI2002 POJ1054 The Troublesome Frog 讨厌的青蛙 (离散化+剪枝)

Description In Korea, the naughtiness of the cheonggaeguri, a small frog, is legendary. This is a well-deserved reputation, because the frogs jump through your rice paddy at night, flattening rice plants. In the morning, after noting which plants hav

POJ 1054 The Troublesome Frog(枚举+剪枝)

题目链接 题意 :给你r*c的一块稻田,每个点都种有水稻,青蛙们晚上会从水稻地里穿过并踩倒,确保青蛙的每次跳跃的长度相同,且路线是直线,给出n个青蛙的脚印点问存在大于等于3的最大青蛙走的连续的脚印个数. 思路 : 暴力了一下,顺便剪剪枝就可以过.... 1 //POJ1054 2 #include <stdio.h> 3 #include <string.h> 4 #include <iostream> 5 #include <algorithm> 6 7

poj 1054 The Troublesome Frog (暴力搜索 + 剪枝优化)

题目链接 看到分类里是dp,结果想了半天,也没想出来,搜了一下题解,全是暴力! 不过剪枝很重要,下面我的代码 266ms. 题意: 在一个矩阵方格里面,青蛙在里面跳,但是青蛙每一步都是等长的跳, 从一个边界外,跳到了另一边的边界外,每跳一次对那个点进行标记. 现在给你很多青蛙跳过后的所标记的所有点,那请你从这些点里面找出 一条可能的路径里面出现过的标记点最多. 分析:先排序(目的是方便剪枝,break),然后枚举两个点,这两个 点代表这条路径的起始的两个点.然后是三个剪枝,下面有. 开始遍历时,

POJ - 1054 The Troublesome Frog

题意:给你个矩阵,里面有n个标记的点,许多只青蛙在上面跳,每次跳的距离都是一样的且轨迹是直线,目标是从一边跳到另一边,求最多步数的青蛙 思路:排序后,枚举判断 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAXN = 5050; struct point{ int x,y; void i

hdu4430之枚举+二分

Yukari's Birthday Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2549    Accepted Submission(s): 522 Problem Description Today is Yukari's n-th birthday. Ran and Chen hold a celebration party

CSU OJ PID=1514: Packs 超大背包问题,折半枚举+二分查找。

1514: Packs Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 61  Solved: 4[Submit][Status][Web Board] Description Give you n packs, each of it has a value v and a weight w. Now you should find some packs, and the total of these value is max, total of

hdu 4430 Yukari&#39;s Birthday 枚举+二分

注意会超long long 开i次根号方法,te=(ll)pow(n,1.0/i); Yukari's Birthday Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3262    Accepted Submission(s): 695 Problem Description Today is Yukari's n-th birt

Eqs 折半枚举+二分查找 大水题

Eqs 题目抽象:a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 (*),给出a1,a2,a3,a4,a5.    ai属于[-50,50]. 求有多少序列   x1,x2,x3,x4,x5 ,xi属于 [-50,50]-{0}. 思路:折半枚举+二分查找 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #inclu