zoj 3041 City Selection(数学啊)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=2039

The government decided to build some cities in a newly developping area. Now they had
N different locations to select from, while there were M factories in this newly developping area.

The area was blowing northwest wind, so if we choose a location that located on the southeast quadrant of one of the factory (including the boundary), the fog from the factory would pollute the city heavily.

Now it‘s your job to choose all the city locations that will not get pollution by the factories.

Input

The first line of a input block contains the numbers N, M (1 <=
N, M <= 200000). Line 2 ~ N + 1 will each contain two integers(x,
y) the location of a city. Line N + 2 ~ M + N + 1 will each contains two integers(x,
y) the location of a factory. The x and y co-ordinates of the locations will be between -1000000000 and 1000000000, inclusive. There‘re no more than 10 test cases in the input data.

Output

The first line of your output block should be an integer K represent the number of city locations which the government can choose from. The next
K lines should contain the co-ordinates of the cities. The co-ordinates should be sorted in ascending order of x co-ordinates, and in case of tie, ascending order of y co-ordinates.

Sample Input

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

Sample Output

1
1 3

题意:

给出一些城市和工厂的位置,城市不能在工厂的东南方向,求能安放的城市的个数个位置!

ps:

首先按照城市和工厂的x坐标从小达到排序,再寻找每个城市前面是否有y坐标比当前城市大的,如果有则此处不能安放城市

注意:案例有城市和工厂在同一位置的情况!

代码如下:

#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
#define maxn 500000
#define INF 0x3f3f3f3f
struct node
{
    int x, y;
    int flag;
} a[maxn];
bool cmp(node a, node b)//排序
{
    if(a.x == b.x)
    {
        return a.y < b.y;
    }
    return a.x < b.x;
}
int main()
{
    int n, m;
    int x1[maxn], y1[maxn];
    int b[maxn];
    while(~scanf("%d%d",&n,&m))
    {
        int num = 0;
        for(int i = 0; i < n; i++)
        {
            scanf("%d%d",&a[i].x,&a[i].y);
            a[i].flag = 1;//城市
        }
        for(int i = n; i < n + m; i++)
        {
            scanf("%d%d",&a[i].x,&a[i].y);
            a[i].flag =  2;//工厂
        }
        sort(a, a+n+m, cmp);
        int maxx = -INF;//最大值
        for(int i = 0; i < n + m; i++)
        {
            if(a[i].flag == 2 && maxx < a[i].y)
            {
                maxx = a[i].y;//找到最大的y坐标
            }
            if(a[i].flag == 1 && a[i].y > maxx)
            {
                if(a[i].x==a[i+1].x && a[i].y==a[i+1].y)//坐标相同
                    continue;
                if(a[i].x==a[i-1].x && a[i].y==a[i-1].y)//坐标相同
                    continue;
                b[num] = i;
                num++;
            }
        }
        printf("%d\n",num);
        //printf("%d %d",a[b[0]].x,a[b[0]].y);
        for(int i = 0; i < num; i++)
        {
            printf("%d %d\n",a[b[i]].x,a[b[i]].y);
        }
    }
    return 0;
}
时间: 2024-10-05 01:35:03

zoj 3041 City Selection(数学啊)的相关文章

ZOJ 3041 City Selection(好排序)

题目链接:ZOJ 3041 City Selection 题意:有N个城市坐标和M个工厂坐标,在以工厂为原点的第四象限的点都会受到污染.即图中画线区域 思路:把工厂和城市的坐标一起排序,再比较y坐标. AC代码: #include <stdio.h> #include <algorithm> #include <set> using namespace std; const int maxn=200010; struct node{ int x,y; int flag;

ZOJ 3042 City Selection II 【序】【离散化】【数学】

题意: 输入数据n,m.n代表工厂的数量,m代表城市的数量. 接下来n+m行为工厂和城市的坐标. 规定如图所示方向刮风,工厂的air会污染风向地区的air. 注意,工厂和城市的坐标表示的是从x到x+1从y到y+1之间小正方形都是工厂区域,规定如果只有一个coner的air被污染那么该地区视为无污染. 要求输出有多少不被污染的城市. 坑: 思考了很多问题.... 1.加入某城市的正下方是工厂怎么办...这个是否算污染. 2.假如有两个角被污染了怎么办...算污染吗. 坑了一整天.没办法找大神的代码

poj 1543 &amp; HDU 1334 &amp; ZOJ 1331 Perfect Cubes(数学 暴力大法好)

题目链接:http://poj.org/problem?id=1543 Description For hundreds of years Fermat's Last Theorem, which stated simply that for n > 2 there exist no integers a, b, c > 1 such that a^n = b^n + c^n, has remained elusively unproven. (A recent proof is believ

ZOJ 3956 Course Selection System 背包DP

ZOJ3956 观察数据范围, c的值非常小 只有100 所以c的和也很有限 只有50000 是否可以从这里下手? 对于某一个c的和 我们一定希望h的和最大 才有可能是最终答案. 于是有了类似背包的dp方程. 代码很简单,就不给出方程了. //比赛的时候想得太多,都想到斜率优化上了,完全忽略了c的范围这么小!!!毕竟图样. //一个人的面命运,当然要靠自我奋斗,但是也要考虑到历史的行程. #include<iostream> #include<cstdio> #include<

zoj 2180 - City Game

题目:给你一个1000*1000的01矩阵,求里面全是1的最大矩形面积. 分析:dp,单调队列.zju1985升级版 ^_^ 继上次那道题想了一天. 本来想用O(N^2)的最大正方形求解,想错了今天仔细一看,其实这道题目就是二维的最大矩形: 我们将问题分解成最大矩形,即求解以k行为底边的图形中的最大矩形,然后合并,求最大的矩形: 预处理: 求出以每行为底边的每一列从底边开始向上的最大连续1的高度MaxH. O(N^2) : DP:对于每一层底边,我们利用单调队列求解出本行的最大矩形. O(N):

ZOJ 3903 Ant(数学,推公示+乘法逆元)

Ant Time Limit: 1 Second      Memory Limit: 32768 KB There is an ant named Alice. Alice likes going hiking very much. Today, she wants to climb a cuboid. The length of cuboid's longest edge is n, and the other edges are all positive integers. Alice's

zoj.3868.GCD Expectation(数学推导&gt;&gt;容斥原理)

GCD Expectation Time Limit: 4 Seconds                                     Memory Limit: 262144 KB Edward has a set of n integers {a1, a2,...,an}. He randomly picks a nonempty subset {x1, x2,…,xm} (each nonempty subset has equal probability to be pick

ZOJ 3710 Friends(数学啊 )

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3710 Alice lives in the country where people like to make friends. The friendship is bidirectional and if any two person have no less than k friends in common, they will become friends

ZOJ 2988 Conversions(数学啊)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1987 Conversion between the metric and English measurement systems is relatively simple. Often, it involves either multiplying or dividing by a constant. You must write a program that co