zoukankan      html  css  js  c++  java
  • Codeforces-gym-101020 problem C. Rectangles

    题目链接:http://codeforces.com/gym/101020/problem/C

    C. Rectangles
    time limit per test
    2.0 s
    memory limit per test
    64 MB
    input
    standard input
    output
    standard output

    You have n rectangles, each of which is described by three integers i, j and k. This indicates that the lower-left corner of the rectangle will be located at the point (i, 0) and the upper-right corner of the rectangle will be located at the point (j, k). For example, if you have a description of four rectangles like this: 0 2 3 0 3 1 1 2 2 2 4 4 The area occupied by these rectangles will be as follows:

    The total area in this case will be 14. You are given n and n triples (i, j, k), your task is to compute the total area occupied by the rectangles which are described by the n triples.

    Input

    The first line will contain a single integer T indicates the number of test cases. Each test case will consist of n + 1 lines, the first one will contain the number of rectangles n and the following n lines will be the description of the rectangles. Each description will be a triple (i, j, k) as mentioned above. The Constraints: 10 <= T <= 20 1 <= n <= 100 0 <= i < 100 1 <= j < 100 i != j 1 <= k <= 100

    Output

    For each test case, print a single integer in a separated line indicates the total area occupied by the rectangles.

    Examples
    Input
    Copy
    1
    4
    0 2 3
    0 3 1
    1 2 2
    2 4 4
    Output
    Copy
    14
    题意概括:你有n个矩形,每个矩形由三个整数i,j和k描述。这表示矩形的左下角将位于点(i,0),矩形的右上角将位于点(j,k)。例如,如果您有四个这样的矩形的描述:0 2 3 0 3 1 1 2 2 2 4 4这些矩形占用的区域如下:
    在这种情况下,总面积为14.您将获得n和n三元组(i,j,k),您的任务是计算由n个三元组描述的矩形占据的总面积。
    输入
    第一行将包含单个整数T表示测试用例的数量。每个测试用例由n + 1行组成,第一行包含矩形n的数量,后面的n行将是矩形的描述。如上所述,每个描述将是三(i,j,k)。约束:10 <= T <= 20 1 <= n <= 100 0 <= i <100 1 <= j <100 i!= j 1 <= k <= 100
    输出
    对于每个测试用例,在单独的行中打印单个整数表示矩形占用的总面积。
    解题思路:看似很复杂,会有很多重复的不只如何下手,其实我们只需要简单定义一个数组,记录每一竖的小矩形的面积,每次输入新的矩形时,只需要比较新的矩形在每个小矩形与原来的是否更高了,如果更高了,就该成新的高度,否则不变。具体详见代码:
    #include<iostream>
    #include<string.h>
    typedef long long ll;
    const int maxn=1e6+60;
    int a[105];
    using namespace std;
    int main()
    {
        int t;
        cin>>t;
        while(t--)
        {
            int n;
            memset(a,0,sizeof(a));
            cin>>n;
            int sum=0;
            while(n--)
            {
                int x,y,z;
                cin>>x>>y>>z;
                for(int i=x;i<y;i++)
                {
                    if(a[i]<z) a[i]=z; //如果横坐标为i的小矩形的高度小于新矩形的高度,则更新 
                }    
            }
            for(int i=0;i<=100;i++)
                sum+=a[i];
            cout<<sum<<endl;
        }
        return 0;
     } 
  • 相关阅读:
    简易的sniffer程序
    ubuntu 13.10 Rhythmbox不能播放mp3 和中文乱码的问题
    5.单行函数,多行函数,字符函数,数字函数,日期函数,数据类型转换,数字和字符串转换,通用函数(case和decode)
    Java设计模式-单例模式
    WordPress的用户系统总结
    STM8S EEPROM 操作
    简化ui文件转换写法
    大型网站架构之应用服务器集群化
    大型网站架构之分布式缓存
    大型网站架构之JAVA中间件
  • 原文地址:https://www.cnblogs.com/zjl192628928/p/9280159.html
Copyright © 2011-2022 走看看