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

题意:

输入数据n,m。n代表工厂的数量,m代表城市的数量。

接下来n+m行为工厂和城市的坐标。

规定如图所示方向刮风,工厂的air会污染风向地区的air。

注意,工厂和城市的坐标表示的是从x到x+1从y到y+1之间小正方形都是工厂区域,规定如果只有一个coner的air被污染那么该地区视为无污染。

要求输出有多少不被污染的城市。

坑:

思考了很多问题....

1.加入某城市的正下方是工厂怎么办...这个是否算污染。

2.假如有两个角被污染了怎么办...算污染吗。

坑了一整天.没办法找大神的代码测试了几组样例...【奈何年代久远代码都不好找更不用说题解】

总之测试结果是以上两种都不算污染。

其实坑都是自己挖的,用正常的脑子思考下问题,就不会出问题。

不过话说回来掉坑里不可怕,要保持冷静爬出来...

思路:

1.离散化,已知直线坐标,按照截距离散化。

2.序,从坐到右从上刀下依次处理,遇到工厂更新标记数组,遇到城市检查是否被污染,无污染加入答案序列。

3.如何处理只有两个顶点被污染的问题,这点我借鉴了大牛的思路,每个点不处理右上角的点,不管是工厂还是城市。那么城市不污染的条件是除了右上角的点以外其它三个点不被污染。证明自己画个图想想。

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
const int inf=0x3f3f3f3f;
int tmp;
struct st{
    pair<int,int>me;
    bool imfac;
};
pair<int,int>city[100050];
pair<int,int>fac[100050];
int jilu[800050];
int mynum[100050];
bool vis[800050];
st all[200050];
pair<int,int>aaa[100050];
bool cmp(st a,st b){
    if(a.me.first!=b.me.first)
        return a.me.first<b.me.first;
    else if(a.me.second!=b.me.second)return a.me.second>b.me.second;
    else return a.imfac>b.imfac;
}
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF){
        memset(vis,0,sizeof(vis));
        int num=0,nnnn=0;
        for(int i=0;i<n;i++){
            scanf("%d%d",&fac[i].first,&fac[i].second);
            jilu[num++]=2*fac[i].first+fac[i].second;
            jilu[num++]=2*(fac[i].first+1)+fac[i].second;
            jilu[num++]=2*(fac[i].first)+fac[i].second+1;
        }
        for(int i=0;i<m;i++){
            scanf("%d%d",&city[i].first,&city[i].second);
            jilu[num++]=2*city[i].first+city[i].second;
            jilu[num++]=2*city[i].first+city[i].second+1;
            jilu[num++]=2*(city[i].first+1)+city[i].second;
        }
        sort(jilu,jilu+num);
        num=unique(jilu,jilu+num)-jilu;
        for(int i=0;i<n;i++){
            all[nnnn++].me=fac[i];
            all[nnnn-1].imfac=1;
        }
        for(int i=0;i<m;i++){
            all[nnnn++].me=city[i];
            all[nnnn-1].imfac=0;
        }
        int id,ansnum=0;
        sort(all,all+nnnn,cmp);
        for(int i=0;i<nnnn;i++){
            if(all[i].imfac){
            id=upper_bound(jilu,jilu+num,2*all[i].me.first+all[i].me.second)-jilu;
            vis[id]=1;
            id=upper_bound(jilu,jilu+num,2*all[i].me.first+all[i].me.second+1)-jilu;
            vis[id]=1;
            id=upper_bound(jilu,jilu+num,2*(all[i].me.first+1)+all[i].me.second)-jilu;
            vis[id]=1;
            }
            else{
                int ttt=0;
                id=upper_bound(jilu,jilu+num,2*(all[i].me.first)+all[i].me.second)-jilu;
                ttt+=vis[id];
                id=upper_bound(jilu,jilu+num,2*(all[i].me.first)+all[i].me.second+1)-jilu;
                ttt+=vis[id];
                id=upper_bound(jilu,jilu+num,2*(all[i].me.first+1)+all[i].me.second)-jilu;
                ttt+=vis[id];
                if(ttt==0){
                aaa[ansnum++]=all[i].me;
                }
            }
        }
        sort(aaa,aaa+ansnum);
        printf("%d\n",ansnum);
        for(int i=0;i<ansnum;i++){
            printf("(%d,%d)\n",aaa[i].first,aaa[i].second);
        }
    }
}
时间: 2025-01-17 04:32:37

ZOJ 3042 City Selection II 【序】【离散化】【数学】的相关文章

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 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 ar

zoj 3332 Strange Country II

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3332 Description You want to visit a strange country. There are n cities in the country. Cities are numbered from 1 to n. The unique way to trav

HDU 4791 &amp;amp; ZOJ 3726 Alice&amp;#39;s Print Service (数学 打表)

题目链接: HDU:http://acm.hdu.edu.cn/showproblem.php?pid=4791 ZJU:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5072 Problem Description Alice is providing print service, while the pricing doesn't seem to be reasonable, so people using her pr

ZOJ 3574 Under Attack II 归并排序求逆序对

Under Attack II Time Limit: 5 Seconds      Memory Limit: 65536 KB Because of the sucessfully calculation in Under Attack I, Doctor is awarded with Courage Cross and promoted to lieutenant. But the war seems to end in never, now Doctor has a new order

CCPC河南省赛B-树上逆序对| 离线处理|树状数组 + 线段树维护逆序对 + dfs序 + 离散化

B题地址:树上逆序对 有两个思路 方法一:线段树离线 + 树状数组或者线段树维护区间和 0:离散化,离线存储输入的operation操作序列. ①:先线段树在dfs序上离线处理好整一棵树:在dfs序上先查询"加入当前结点的逆序对权值和"并记录,再加入当前这个节点:dfs完毕后,就已经记录好每个结点的dfs序出入时间戳(转化成区间问题了)和每个 ②:使用树状数组或者新的线段树在dfs序上插入逆序对权值 为什么能这样呢?因为dfs序维护了每个结点遍历的顺序,每个结点的dfs序时间戳肯定比它

HDOJ5877(dfs序+离散化+树状数组)

Weak Pair Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 2081    Accepted Submission(s): 643 Problem Description You are given a rooted tree of N nodes, labeled from 1 to N. To the ith node a

zoj 3620 Escape Time II

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4744 Escape Time II Time Limit: 2 Seconds      Memory Limit: 65536 KB There is a fire in LTR ’ s home again. The fire can destroy all the things in t seconds, so LTR has to escape in t second

zoj 3620 Escape Time II dfs

Escape Time II Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3620 Description There is a fire in LTR ’ s home again. The fire can destroy all the things in t seconds, so LTR has to escape i