water~7

Advertisement

poj1328http://poj.org/problem?id=1328
比较简单的贪心,看代码,不细说了~

#include <iostream>
#include <fstream>
#include <algorithm>
#include <cmath>
using namespace std;

struct Space
{
    double left;
    double right;
    friend bool operator<(Space a,Space b)
    {
        return a.left<b.left;
    }

};

Space space[1007];
int n,d;
int ans;
int caseCount=1;

void waterit()
{
    ans=0;
    int i;

    double adjust=space[0].right;
    ans++;

    for(i=0;i<n-1;i++)
    {
        if(adjust<space[i+1].left)
        {
            ans++;
            adjust=space[i+1].right;
        }
        else
        {
            if(adjust>space[i+1].right)
                adjust=space[i+1].right;
        }
    }
    cout<<"Case "<<caseCount<<": "<<ans<<endl;
    caseCount++;

}

int main()
{
    //ifstream cin("1.txt");
    int i;
    bool flag=true;
    while(1)
    {
        cin>>n>>d;
        if(n==0 && d==0)
            break;
        for(i=0;i<n;i++)
        {
            int a,b;
            cin>>a>>b;
            if(b>d && flag)
            {
                    cout<<"Case "<<caseCount<<": "<<"-1"<<endl;
                    caseCount++;
                    flag=false;

            }
            space[i].left=a-sqrt(double(d*d-b*b));
            space[i].right=a+sqrt(double(d*d-b*b));
        }

        if(flag)
        {
            sort(space,space+n);
            waterit();
        }
        flag=true;

    }
    return 0;
}

Similar Posts:

  • 11. Container With Most Water

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a containe

  • leetcode 【 Container With Most Water 】python 实现

    题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a cont

  • PLSQL_性能优化系列14_Oracle High Water Level高水位分析

    2014-10-04 Created By BaoXinjian 一.摘要 PLSQL_性能优化系列14_Oracle High Water Level高水位分析 高水位线好比水库中储水的水位线,用于描述数据库中段的扩展方式.高水位线对全表扫描方式有着至关重要的影响. 当使用delete 操作表记录时,高水位线并不会下降,随之导致的是全表扫描的实际开销并没有任何减少. 本文给出高水位线的描述,如何降低高水位线,以及高水位线对全表扫描的影响. 1. 何谓高水位线 如前所述,类似于水库中储水的水位线

  • [LeetCode] Container With Most Water 简要分析

    前言 这题非要说贪心的话也算是吧,不过最主要的特征还是双指针.LC的题好像不少都是扔倆头尾指针然后遍历一遍完事儿的.这道题倒是"短板效应"的不错体现了. 题目 题目链接 Given n non-negative integers a1, a2, -, an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line

  • Trapping Rain Water

    Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. For example, Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6. 对于每一个点来说,蓄水量 = min(左边的最高点,右边的最高点)-本身高度,于是两次

  • [poj 2331] Water pipe ID A*迭代加深搜索(dfs)

    Water pipe Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 2265 Accepted: 602 Description The Eastowner city is perpetually haunted with water supply shortages, so in order to remedy this problem a new water-pipe has been built. Builders s

  • [leetcode]Trapping Rain Water @ Python

    原题地址:https://oj.leetcode.com/problems/trapping-rain-water/ 题意: Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. For example, Given [0,1,0,2,1,0,1,3,

  • Container With Most Water leetcode java

    题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a cont

  • leetcode 之trap water(8)

    这题不太好想.可以先扫描找到最高的柱子,然后分别处理两边. int trapWater(int A[], int n) { int peak = 0; int max = 0; int water = 0; for (int i = 1; i < n; i++) { if (A[i]>A[max])max = i; } for (int i = 0; i < max; i++) { if (A[i]>peak) peak = A[i]; else water += peak - A

  • 【LeetCode with Python】 Container With Most Water

    博客域名:http://www.xnerv.wang 原题页面:https://oj.leetcode.com/problems/container-with-most-water/ 题目类型:两指针问题 难度评价:★★★ 本文地址:http://blog.csdn.net/nerv3x3/article/details/37331325 Given n non-negative integers a1, a2, ...,an, where each represents a point at

  • "China real estate property enameled mineral water t shirt brand"

    In The month of january 25, through the Entire world Association of structure and ecological surroundings safeguards, The far east Asia property Holding chamber involving Business sorted out, Joined the nation in addition to around the globe signific

Tags: