【Pair】#15 A. Cottage Village

A. Cottage Village
time limit per test
2 seconds
memory limit per test
64 megabytes
input
standard input
output
standard output

A new cottage village called «Flatville» is being built in Flatland. By now they have already built in «Flatville» n square houses with the centres on the Оx-axis. The houses' sides are parallel to the coordinate axes. It's known that no two houses overlap, but they can touch each other.

The architect bureau, where Peter works, was commissioned to build a new house in «Flatville». The customer wants his future house to be on the Оx-axis, to be square in shape, have a side t, and touch at least one of the already built houses. For sure, its sides should be parallel to the coordinate axes, its centre should be on the Ox-axis and it shouldn't overlap any of the houses in the village.

Peter was given a list of all the houses in «Flatville». Would you help him find the amount of possible positions of the new house?

Input

The first line of the input data contains numbers n and t (1 ≤ n, t ≤ 1000). Then there follow n lines, each of them contains two space-separated integer numbers: xi ai, where xi — x-coordinate of the centre of the i-th house, and ai — length of its side ( - 1000 ≤ xi ≤ 10001 ≤ ai ≤ 1000).

Output

Output the amount of possible positions of the new house.

Sample test(s)
input
2 2
0 4
6 2
output
4
input
2 2
0 4
5 2
output
3
input
2 3
0 4
5 2
output
2
Note

It is possible for the x-coordinate of the new house to have non-integer value.


题意:有个x轴,轴上有n座房子,每座房子的位置是xi,边长是ai,有个人想要贴着其中一座房子建造他自己的一座边长为t的房子,有多少种建法

终于出现一个麻烦些的A题了,全网的C++解法也只有一个,倒是看到不少Python解这道题的……我想试试看用map试试看。

个人的想法是通过定义pair类型,扔进list来存储,然后使用list自带的sort排序后遍历一遍的方法……(我知道这个用数组容易而且是一个意思,但是我只是想学新东西而已……)


先把CE代码贴一下,待日后有人教我怎么用……(为什么DevC++都不认识first和second……)

后记:居然不能用'.',要用'->'......

List-Pair 终于AC:

#include<iostream>  
#include<algorithm>  
#include<list>  
#include<cstdio>  
#include<cstdlib>  
using namespace std;  
  
typedef pair<int,int> house;  
typedef list<house> infov;  
  
bool cmp(const house& p1, const house& p2) {   return p1.first < p2.first;   }  
/* 输出操作符重载:  
inline ostream& operator << (ostream& stream,house& val) 
{ 
    stream <<val.first << val.second ; 
    return stream; 
} 
*/  
int main()  
{  
    int n,cnt=0;  
    int t;
    scanf("%d%d", &n, &t);  
    infov info;  
    for(int i=0;i<n;i++)  
    {  
        int x_tmp, a_tmp;  
        scanf("%d%d", &x_tmp, &a_tmp);  
        info.push_back(make_pair(x_tmp,a_tmp));  
    }  
    info.sort();  
    //info.unique();  如果需要去重的话  
    //int testcnt=0;
    infov::iterator it,itemp;  
    for(it=info.begin();it!=info.end();++it)  
    {  
    	//cout<<it->first<<"\t"<<it->second<<endl;
        if(it!=info.begin())  
            if( (it->first - itemp->first)*2
				< (it->second + itemp->second) +t*2 );//cout<<"n1:"<<testcnt<<endl;  
            else if( (it->first - itemp->first)*2
					==(it->second + itemp->second +t*2 ) )
					{
						//cout<<"n2:"<<testcnt<<endl;  						
						cnt++;
					}
            else if( (it->first - itemp->first)*2
					>(it->second + itemp->second) +t*2 ) 
					{
						//cout<<"n3:"<<testcnt<<endl; 
						cnt+=2; 
					}
        itemp=it;  
        //testcnt++;
    }   
    cout<< cnt+2;  
    return 0;  
}   


Map-Pair Failed:

#include <cstdio>  
#include <iostream>  
#include <algorithm>  
#include <string.h>  
#include <cstdlib>  
#include <string>  
#include <map>  
using namespace std;  

/***********************************************
template <class T1, class T2> struct pair
{
  typedef T1 first_type;
  typedef T2 second_type;

  T1 first;
  T2 second;
  pair() : first(T1()), second(T2()) {}
  pair(const T1& x, const T2& y) : first(x), second(y) {}
  template <class U, class V>
    pair (const pair<U,V> &p) : first(p.first), second(p.second) { }
}
************************************************/

int cmp(const pair<int,int>& a,const pair<int,int>& b)
{
	return a.first > b.first;
}
int main()  
{
	map<int, int, greater<int> > house;
	int n,t,cnt=0;
	scanf("%d%d",&n,&t);
	int tmp_x,tmp_a;
	for(int i=0;i<n;i++)
	{
		scanf("%d%d",&tmp_x,&tmp_a);
		house.insert(pair<int,int>(tmp_x,tmp_a));
	} 
	sort(house.begin(),house.end(),cmp);
	map<int,int>::iterator it,itemp; 
	for(it=house.begin();it!=house.end();++it)
	{ 
		if(it!=house.begin())  
			if( it->first 
					<(it->second+itemp->second)/2 +itemp->first );
		  		else if( it->first 
				  			==(it->second + itemp->second)/2 + itemp->first) cnt++;  
            		else if( it->first
								>(it->second + itemp->second)/2 +itemp->first)cnt+=2; 
    	itemp=it;
	}
	cout<<cnt+2;
	return 0;	
} 


以下是ymrfzr(CSDN)的解法……大意是把房子的左点和右点在x轴上显示出来,然后每次比较一个房子的左点和左房子的右点。

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <algorithm>
using namespace std;

int n,t,cn,a[2005]; 
int main()
{
    int T,i,j,k;
    while(~scanf("%d %d",&n,&t))
	{
		for(i=0;i<n;i++)
		{
			scanf("%d %d",&j,&k);
			a[i*2]=j*2+k;
			a[i*2+1]=j*2-k;
		}
	sort(a,a+2*n);
	n*=2;
	t*=2;
	cn=2;
	for(i=1;i<n;i+=2)
	{
		if(a[i+1]-a[i]>t)cn+=2;
		else if(a[i+1]-a[i]==t)cn++;
	}
	printf("%d\n",cn);
	}
    return 0;
}


Python:

# input
n,t = map(int , raw_input().split())
dict = {}
for i in range(n):
    x,a = map(int , raw_input().split())
    dict[x] = a

# getAns
ans = 2
list = dict.keys()
list.sort()
pre = -(1<<30)

for key in list:
    a = float(dict[key])/2
    if pre != -(1<<30):
       dis = abs((key-a)-pre)
       if dis > t:
          ans += 2
       elif dis == t:
          ans += 1
    pre = key+a

# output
print ans



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java 2实用教程(第三版)实验指导与习题解答 清华大学出版社 (编著 耿祥义 张跃平) 实验模版代码 建议使用文档结构图 (选择Word菜单→视图→文档结构图) 上机实践1 初识Java 4 实验1 一个简单的应用程序 4 实验2 一个简单的Java Applet程序 4 实验3 联合编译 5 上机实践2 基本数据类型与控制语句 6 实验1 输出希腊字母表 6 实验2 回文数 6 实验3 猜数字游戏 8 上机实践3 类与对象 9 实验1 三角形、梯形和圆形的类封装 9 实验2 实例成员与类成员 12 实验3 使用package语句与import语句 13 上机实践4 继承与接口 15 实验1 继承 15 实验2 上转型对象 17 实验3 接口回调 18 上机实践5 字符串、时间与数字 19 实验1 String类的常用方法 19 实验2 比较日期的大小 21 实验3 处理大整数 22 上机实践6 组件及事件处理 23 实验1 算术测试 23 实验2 信号灯 25 实验3 布局与日历 28 上机实践7 组件及事件处理2 31 实验1 方程求根 31 实验2 字体对话框 34 实验3 英语单词拼写训练 37 上机实践8 多线程 41 实验1 汉字打字练习 41 实验2 旋转的行星 43 实验3 双线程接力 47 上机实践9 输入输出流 50 实验1 学读汉字 50 实验2 统计英文单词字 53 实验2 读取Zip文件 56 上机实践10 Java 中的网络编程 57 实验1 读取服务器端文件 57 实验2 使用套接字读取服务器端对象 59 实验3 基于UDP的图像传输 62 上机实践11 数据结构 66 实验1 扫雷小游戏 66 实验2 排序与查找 70 实验3 使用TreeSet排序 72 上机实践12 java Swing 74 实验1 JLayeredPane分层窗格 74 实验2 使用表格显示日历 75 实验3 多文档界面(MDI) 78 上机实践1 初识Java 实验1 一个简单的应用程序 2.模板代码 Hello.java package 实验一; public class Hello { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("你好,很高兴学习Java"); //命令行窗口输出"你好,很高兴学习Java" A a=new A(); a.fA(); } } class A { void fA() {System.out.println("we are student"); } } 实验2 一个简单的Java Applet程序 2.模板代码 FirstApplet.java import java.applet.*; import java.awt.*; public class FirstApplet extends Applet { public void paint(Graphics g) { g.setColor(Color.blue); g.drawString("这是一个Java Applet 程序",10,30);//在Java Applet中绘制一行文字:“这是一个Java Applet 程序” g.setColor(Color.red); g.setFont(new Font("宋体",Font.BOLD,36)); g.drawString("我改变了字体",20,50);//在Java Applet中绘制一行文字:“我改变了字体” } }实验3 联合编译 2.模板代码 public class MainClass { public static void main (String args[ ]) { System.out.println("你好,只需编译我") ; //命令行窗口输出"你好,只需编译我" A a=new A(); a.fA(); B b=new B(); b.fB(); } } public class A { void fA() {
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

糖果天王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值