[Codeforces 35E] Parade

Link:

Codeforces 35E 传送门

Brief Intro:

给定$n$个矩形,求出轮廓线的所有顶点

Solution:

对于此类可拆分成多个事件点的题目,使用扫描线的方式

每个矩形分为起始边和终止边两个事件,排序,按顺序扫描

分类讨论:

维护当前最高的高度,

(1)如果是起始边,判断其高度是否高于当前高度,如果是,判断添加一个点还是两个点

(2)如果是终止边,判断其是否为当前唯一的最高高度,考虑是否要添加2个节点

TIP:

(1)注意,此题要能维护出每个高度和其出现的次数

因此由$set$改为$multiset$进行处理

(2)同时对于相同点,要先处理“起始事件”,再处理“终止事件”

在$cmp$中要另行判断,否则可能多输出点

Code:

#include <bits/stdc++.h>

using namespace std;
const int MAXN=1e5+10,INF=1e9+10;
#define X first
#define Y second
typedef pair<int,int> P;
typedef pair<P,int> PP;
int n,cnt=0;
PP dat[2*MAXN];
P res[MAXN];
multiset<int> s;

bool cmp(PP a,PP b)
{
    if(a.X==b.X) return a.Y>b.Y;
    return a.X<b.X;
}

int main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        dat[i].X.X=y;dat[i].X.Y=x;dat[i].Y=1;
        dat[n+i].X.X=z;dat[n+i].X.Y=x;dat[n+i].Y=-1;
    }
    sort(dat+1,dat+2*n+1,cmp);

    s.insert(0);
    for(int i=1;i<=2*n;i++)
    {
        if(dat[i].Y==1)
        {
            if(dat[i].X.Y>*(--s.end()))
            {
                if(cnt>1 && dat[i].X.X==res[cnt].X && res[cnt].X==res[cnt-1].X) cnt--;
                else res[++cnt]=P(dat[i].X.X,*(--s.end()));
                res[++cnt]=P(dat[i].X.X,dat[i].X.Y);
            }
            s.insert(dat[i].X.Y);
        }
        else if(dat[i].Y==-1)
        {
            if(dat[i].X.Y==*(--s.end()) && s.count(dat[i].X.Y)==1)
            {
                s.erase(s.find(dat[i].X.Y));
                res[++cnt]=P(dat[i].X.X,dat[i].X.Y);
                res[++cnt]=P(dat[i].X.X,*(--s.end()));
            }
            else s.erase(s.find(dat[i].X.Y));
        }
    }
    printf("%d\n",cnt);
    for(int i=1;i<=cnt;i++) printf("%d %d\n",res[i].X,res[i].Y);
    return 0;
}

原文地址:https://www.cnblogs.com/newera/p/9127415.html

时间: 2024-08-02 12:51:08

[Codeforces 35E] Parade的相关文章

Codeforces 35E Parade 扫描线 + list

主题链接:点击打开链接 意甲冠军:特定n矩阵(总是接近底部x轴) 然后找到由上面的矩阵所包围的路径,的点 给定n 以下n行给定 y [x1, x2] 表示矩阵的高度和2个x轴坐标 思路: 扫描线维护每段区间的线段 最大的y值 则我们訪问每一个x轴坐标,就相当于訪问x轴坐标向右最短的那个小区间上的最大y值. 则能够得到[x,y] 和 [x+1, y] 这样2个点 当我们发现存在高度差时(且x轴不同样) 那么则手动加入一个点(这个点一定是与y大的点的x同样,y小的点的y同样) 再把3个相邻点且有同样

CodeForces 733B Parade

枚举.枚举交换哪一个即可. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include<map> #include<set> #include<queue

Codeforces 35E(区间更新)

题意:要建n个高楼,给出了每个高楼的左右区间和高度,问最后所有的高楼的轮廓包括了哪些点. 题解:这题好坑,用了n种姿势了还是一直wa,后来才直到必须加输入输出文件那句话才能过...用线段树存维护区间内最大值也就是高度,左右区间到1e9所以要离散化.因为维护的是每一段的最大值而不是点,所以划分左右子区间那里要把mid到mid+1也归到右子区间里. #include <cstdio> #include <cstring> #include <algorithm> #incl

线段树详解 (原理,实现与应用)

线段树详解 By 岩之痕 目录: 一:综述 二:原理 三:递归实现 四:非递归原理 五:非递归实现 六:线段树解题模型 七:扫描线 八:可持久化 (主席树) 九:练习题 一:综述 假设有编号从1到n的n个点,每个点都存了一些信息,用[L,R]表示下标从L到R的这些点. 线段树的用处就是,对编号连续的一些点进行修改或者统计操作,修改和统计的复杂度都是O(log2(n)). 线段树的原理,就是,将[1,n]分解成若干特定的子区间(数量不超过4*n),然后,将每个区间[L,R]都分解为 少量特定的子区

codeforces Arrival of the General 题解

A Ministry for Defense sent a general to inspect the Super Secret Military Squad under the command of the Colonel SuperDuper. Having learned the news, the colonel ordered to all n squad soldiers to line up on the parade ground. By the military charte

【codeforces 718E】E. Matvey&#39;s Birthday

题目大意&链接: http://codeforces.com/problemset/problem/718/E 给一个长为n(n<=100 000)的只包含‘a’~‘h’8个字符的字符串s.两个位置i,j(i!=j)存在一条边,当且仅当|i-j|==1或s[i]==s[j].求这个无向图的直径,以及直径数量. 题解:  命题1:任意位置之间距离不会大于15. 证明:对于任意两个位置i,j之间,其所经过每种字符不会超过2个(因为相同字符会连边),所以i,j经过节点至多为16,也就意味着边数至多

Codeforces 124A - The number of positions

题目链接:http://codeforces.com/problemset/problem/124/A Petr stands in line of n people, but he doesn't know exactly which position he occupies. He can say that there are no less than a people standing in front of him and no more than b people standing b

Codeforces 841D Leha and another game about graph - 差分

Leha plays a computer game, where is on each level is given a connected graph with n vertices and m edges. Graph can contain multiple edges, but can not contain self loops. Each vertex has an integer di, which can be equal to 0, 1 or  - 1. To pass th

Codeforces Round #286 (Div. 1) A. Mr. Kitayuta, the Treasure Hunter DP

链接: http://codeforces.com/problemset/problem/506/A 题意: 给出30000个岛,有n个宝石分布在上面,第一步到d位置,每次走的距离与上一步的差距不大于1,问走完一路最多捡到多少块宝石. 题解: 容易想到DP,dp[i][j]表示到达 i 处,现在步长为 j 时最多收集到的财富,转移也不难,cnt[i]表示 i 处的财富. dp[i+step-1] = max(dp[i+step-1],dp[i][j]+cnt[i+step+1]) dp[i+st