5-26 Population (35分) -- 待解决
2015-11-02 21:21
501 查看
5-26 Population (35分)
It is always exciting to see people settling in a new continent. As the head of the population management office, you are supposed to know, at any time, how people are distributed in this continent.
The continent is divided into square regions, each has a center with integer coordinates (x,y).
Hence all the people coming into that region are considered to be settled at the center position. Given the positions of the corners of a rectangle region, you are supposed to count the number of people living in that region.
Each input file contains one test case. For each case, the character
a line signals the coming in of new groups of people. In the following lines, each line contains three integers:
and
are the coordinates of the region's center, and
is the number of people coming in.
The character
The following lines each contains four numbers: Xmin, Xmax, Ymin, Ymax,
where (Xmin,Ymin) and (Xmax,Ymax) are
the integer coordinates of the lower left corner and the upper right corner of the rectangle, respectively.
The character
For each
rectangle region. That is, you are supposed to count the number of people living at all the positions (x,y) such
that Xmin≤x≤Xmax,
and Ymin≤y≤Ymax.
时间限制:600ms
内存限制:64MB
代码长度限制:16kB
判题程序:系统默认
作者:陈越
单位:浙江大学
27分运行超时代码
It is always exciting to see people settling in a new continent. As the head of the population management office, you are supposed to know, at any time, how people are distributed in this continent.
The continent is divided into square regions, each has a center with integer coordinates (x,y).
Hence all the people coming into that region are considered to be settled at the center position. Given the positions of the corners of a rectangle region, you are supposed to count the number of people living in that region.
Input Specification:
Each input file contains one test case. For each case, the character Iin
a line signals the coming in of new groups of people. In the following lines, each line contains three integers:
X,
Y,
and
N, where
Xand
Y(1 ≤
X,
Y≤ 20000)
are the coordinates of the region's center, and
N(1 ≤
N≤ 10000)
is the number of people coming in.
The character
Qin a line signals the query of population.
The following lines each contains four numbers: Xmin, Xmax, Ymin, Ymax,
where (Xmin,Ymin) and (Xmax,Ymax) are
the integer coordinates of the lower left corner and the upper right corner of the rectangle, respectively.
The character
Esignals the end of the test case.
Output Specification:
For each Qcase, print in a line the population in the given
rectangle region. That is, you are supposed to count the number of people living at all the positions (x,y) such
that Xmin≤x≤Xmax,
and Ymin≤y≤Ymax.
Sample Input:
I 8 20 1 4 5 1 10 11 1 12 10 1 18 14 1 Q 8 10 5 15 8 20 10 14 I 7 6 1 10 3 2 7 2 1 2 3 2 10 3 1 Q 2 20 2 20 E
Sample Output:
1 3 12
时间限制:600ms
内存限制:64MB
代码长度限制:16kB
判题程序:系统默认
作者:陈越
单位:浙江大学
http://pta.patest.cn/pta/test/16/exam/4/question/688
27分运行超时代码#include <cstdio> #include <sstream> #include <cstring> #include <iostream> #include <string> #include <vector> #include <algorithm> #include <sstream> #include <stack> using namespace std; #define Mum 20001 int n ; int str2int(string s) { stringstream ss ; int d ; ss << s; ss >> d ; return d ; } typedef struct node { int x, y ; // 1 ≤ X, Y ≤ 20000 int pNum ; struct node *left , *right ; node(int _x , int _y , int _pNum) { x = _x ; y = _y ; pNum = _pNum ; left = NULL ; right = NULL ; } }Bnode; int cmp(int x1 ,int y1 , int x2 , int y2) { if(x1 < x2) return -1; if(x1 == x2) { if(y1 < y2) return -1 ; if(y1 == y2) return 0 ; } return 1 ; } Bnode* createTree(Bnode* root , int x , int y ,int pNum) { if(root == NULL) { root = new node(x , y , pNum); }else{ if(cmp(x , y , root->x , root->y) == -1) { root->left = createTree(root->left , x ,y , pNum); }else if(cmp(x , y , root->x , root->y) == 1){ root->right = createTree(root->right , x ,y , pNum); }else{ root->pNum = root->pNum + pNum ; } } return root ; } Bnode* root = NULL ; int cnt = 0 ; int xmin , xmax ; int ymin , ymax ; bool isOk(int x , int y) { if(x >= xmin && x <= xmax && y >= ymin && y <= ymax) return true ; return false ; } void inOrder(Bnode* root) { if(root != NULL) { inOrder(root->left); if( isOk(root->x , root->y) ) { cnt += root->pNum ; } if(root->x > xmax) return ; inOrder(root->right); } } void inOrder2(Bnode *root) //非递归中序遍历 { stack<Bnode*> s; Bnode *p = root; while(p!=NULL||!s.empty()) { while(p!=NULL) { s.push(p); p=p->left; } if(!s.empty()) { p=s.top(); if( isOk(p->x , p->y) ) { cnt += p->pNum ; } if(p->x > xmax) return ; s.pop(); p=p->right; } } } int main() { //freopen("in.txt", "r", stdin); string c; bool flag = true ; while( cin >> c) { if(c == "E") break ; if(c == "I") { flag = true ; } else if(c == "Q") { flag = false ; }else{ if(flag == true) { int x , y , pNum ; x = str2int(c); scanf("%d%d\n" , &y , &pNum) ; root = createTree(root , x , y , pNum) ; }else{ xmin = str2int(c); scanf("%d%d%d\n" , &xmax , &ymin ,&ymax) ; cnt = 0 ; //inOrder(root) ; inOrder2(root); printf("%d\n" , cnt) ; } } } return 0 ; }
相关文章推荐
- Linux内存子系统基础及常用调优参数
- 【Opencv初学者笔记】-vs2010的配置和简单例程demo
- PowerShell 在hyper-v中创建虚拟机
- Linux 的文件权限与目录配置
- Linux常用命令总结及常用快捷键
- 正则化方法:L1和L2 regularization、数据集扩增、dropout
- Lesson 8 Basic arithmetic reduction operations
- 解决xshell 中文乱码
- Linux命令大观
- Asp.net使用powershell管理hyper-v
- OpenGL一些函数详解(二)
- 使用ubuntu15.04手动搭建LNMP服务器框架
- Linux命令大观
- linux 基本操作笔记
- SHELL脚本 数学运算、数组、文本处理
- C/C++ ShellExecuteEx调用exe可执行文件
- C/C++ ShellExecuteEx调用exe可执行文件
- php的开发的apache的配置及伪静态的应用
- SHELL脚本基础
- Linux磁盘挂载与卸载