zoukankan      html  css  js  c++  java
  • Codeforces 527 C. Glass Carving

    一、题目概述
    C. Glass Carving
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Leonid wants to become a glass carver (the person who creates beautiful artworks by cutting the glass). He already has a rectangular w mm  ×  h mm sheet of glass, a diamond glass cutter and lots of enthusiasm. What he lacks is understanding of what to carve and how.

    In order not to waste time, he decided to practice the technique of carving. To do this, he makes vertical and horizontal cuts through the entire sheet. This process results in making smaller rectangular fragments of glass. Leonid does not move the newly made glass fragments. In particular, a cut divides each fragment of glass that it goes through into smaller fragments.

    After each cut Leonid tries to determine what area the largest of the currently available glass fragments has. Since there appear more and more fragments, this question takes him more and more time and distracts him from the fascinating process.

    Leonid offers to divide the labor — he will cut glass, and you will calculate the area of the maximum fragment after each cut. Do you agree?

    Input

    The first line contains three integers w, h, n (2 ≤ w, h ≤ 200 000, 1 ≤ n ≤ 200 000).

    Next n lines contain the descriptions of the cuts. Each description has the form H y or V x. In the first case Leonid makes the horizontal cut at the distance y millimeters (1 ≤ y ≤ h - 1) from the lower edge of the original sheet of glass. In the second case Leonid makes a vertical cut at distance x (1 ≤ x ≤ w - 1) millimeters from the left edge of the original sheet of glass. It is guaranteed that Leonid won't make two identical cuts.

    Output

    After each cut print on a single line the area of the maximum available glass fragment in mm2.

    Examples
    Input
    4 3 4
    H 2
    V 2
    V 3
    V 1
    Output
    8
    4
    4
    2
    Input
    7 6 5
    H 4
    V 3
    V 5
    H 2
    V 1
    Output
    28
    16
    12
    6
    4
    Note

    Picture for the first sample test:

    Picture for the second sample test:
    二、题目大意
    给定一块w*h的玻璃,做n次切割操作,每次操作水平或竖直的切割,求出每次切割后子块的最大面积
    三、思路分析
    最直接的思路,求每次切割完之后每个子块的面积,输出最大的那一个,但这显然是不切实际的。观察题目可以发现,因为切割可以想成是一条无限长的直线,所有每个部分都会有共享的长宽,最大的面积其长与宽也必然是最大的。可以设置四个set一个用来存储每次,一类保存x,一类保存y,x,y中又分为保存每次切割的取点与切割之后所需要维护的各子块长度,由于每次切割之后,最大长可能不是唯一的,所以这里需要采用multiset来存储边长
    四、AC代码
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <set>
     4 
     5 using namespace std;
     6 typedef long long LL;
     7 multiset<int> x,y; // 点,可以不定义multiset
     8 multiset<int> mx,my; // 边长
     9 multiset<int>::iterator iter;
    10 
    11 int main()
    12 {
    13     int w,h,n;
    14     char c; int a;
    15     scanf("%d%d%d",&w,&h,&n);
    16     x.insert(0); x.insert(w); // 初始化插入起始点与终点
    17     y.insert(0); y.insert(h);
    18     mx.insert(w); my.insert(h); // 初始化为整条边长的长度
    19     while(n--)
    20     {
    21         cin >> c >> a;
    22         if(c=='H')
    23         {
    24             y.insert(a); // 向存储y坐标的set内插入切割的点
    25             iter = y.find(a); // 找到该点
    26             iter++; int r = *iter; // 找到该点左右的点,返回其值,其左右点是必然存在的
    27             iter--;iter--; int l = *iter;
    28             iter = my.find(r-l); // 找到被该点切割的原线段,从set中移除它
    29             my.erase(iter);
    30             my.insert(a-l); my.insert(r-a); // 将被分割后的两条线段长度插入存储边长的set
    31         }
    32         else
    33         {
    34             x.insert(a);
    35             iter = x.find(a);
    36             iter++; int r = *iter;
    37             iter--;iter--; int l = *iter;
    38             iter = mx.find(r-l);
    39             mx.erase(iter);
    40             mx.insert(a-l); mx.insert(r-a);
    41         }
    42         iter = mx.end(); // 利用set自动排序的性质,找到end前的迭代指针即为最大值
    43         int mw = *(--iter);
    44         iter = my.end();
    45         int mh = *(--iter);
    46         LL ans = (LL)mw*mh; // 数据上限为2e5,相乘int会爆
    47         printf("%I64d
    ",ans);
    48     }
    49     return 0;
    50 }
  • 相关阅读:
    [程序员代码面试指南]栈和队列-单调栈结构(单调栈)
    快学Scala第一部分
    Add Digits
    Nim Game
    将分布式中多台节点的日志信息集中到一个节点上
    Eclipse调试的一些小技巧
    Maven的常用命令
    Eclipse插件本地扩展安装
    Spark应用程序的运行框架
    Spark运行各个时间段的解释
  • 原文地址:https://www.cnblogs.com/EcliWalker/p/8324997.html
Copyright © 2011-2022 走看看