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 outputYou 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.
InputThe 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
OutputFor each test case, print a single integer in a separated line indicates the total area occupied by the rectangles.
Examples Input Copy 14
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;}
?
轉載于:https://www.cnblogs.com/zjl192628928/p/9280159.html
總結
以上是生活随笔為你收集整理的Codeforces-gym-101020 problem C. Rectangles的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 吴恩达《机器学习》课程总结(19)总结
- 下一篇: 编程开发之--单例模式(6)单元测试