HDU1050(Moving Tables:贪心算法)
解题思路:
这种做法是基于hdu2037的做法上考虑的,找出所有可以同时搬运的桌子,然后就很方便求出最短总时间。
还有一种更简单的做法是直接遍历一遍找出与别的重复次数最多的那片区域,重复次数*10就可以得到结果。
Moving Tables
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 28033 Accepted Submission(s): 9170
Problem Description
The famous ACM (Advanced Computer Maker) Company has rented a floor of a building whose shape is in the following figure.
The floor has 200 rooms each on the north side and south side along the corridor. Recently the Company made a plan to reform its system. The reform includes moving a lot of tables between rooms. Because the corridor is narrow and all the tables are big, only one table can pass through the corridor. Some plan is needed to make the moving efficient. The manager figured out the following plan: Moving a table from a room to another room can be done within 10 minutes. When moving a table from room i to room j, the part of the corridor between the front of room i and the front of room j is used. So, during each 10 minutes, several moving between two rooms not sharing the same part of the corridor will be done simultaneously. To make it clear the manager illustrated the possible cases and impossible cases of simultaneous moving.
For each room, at most one table will be either moved in or moved out. Now, the manager seeks out a method to minimize the time to move all the tables. Your job is to write a program to solve the manager’s problem.
Input
The input consists of T test cases. The number of test cases ) (T is given in the first line of the input. Each test case begins with a line containing an integer N , 1<=N<=200 , that represents the number of tables to move. Each of the following N lines contains two positive integers s and t, representing that a table is to move from room number s to room number t (each room number appears at most once in the N lines). From the N+3-rd line, the remaining test cases are listed in the same manner as above.
Output
The output should contain the minimum time in minutes to complete the moving, one per line.
Sample Input
3
4
10 20
30 40
50 60
70 80
2
1 3
2 200
3
10 100
20 80
30 50
Sample Output
10
20
30
Source
Asia 2001, Taejon (South Korea)
Recommend
We have carefully selected several similar problems for you: 1051 1052 1789 1045 1053
Statistic | Submit | Discuss | Note
//#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm> using namespace std; int cmp(const int *a, const int *b) {
if (a[] == b[]) return a[] < b[];
else return a[] < b[];
} int main() {
int n;
cin >> n;
while (n--) {
int t;
int **room;
int time = ;
int t1 = ;
int flag[] = { };//用于判断此次搬运是否已经进行。
room = new int *[];
cin >> t;
for (int i = ; i < t; i++) {
room[i] = new int[];
cin >> room[i][] >> room[i][];
if (room[i][] > room[i][]) {
int temp = room[i][];
room[i][] = room[i][];
room[i][] = temp;
}
if (room[i][] % == ) room[i][]++;//此处是这道题的坑点,因为走廊两侧都有房间,所以
if (room[i][] % == ) room[i][]--;//3和4号房间公用一片走廊。
}
sort(room, room + t, cmp);
for (int i = ; i < t; i++) {
if (flag[i] == ) {
time += ;
int boundary = room[i][];
for (int j = i + ; j < t; j++) { //找出可以与第i次搬运同时进行的次数
if (room[j][] > boundary && flag[j] == ) {
boundary = room[j][];
flag[j] = ;
/*printf("%d %d %d\n", i, time, boundary);纠错语句*/
}
}
}
}
cout << time << endl;
/*for (int i = 0; i < t; i++) {
cout << room[i][0] << room[i][1] << endl;
}*/
}
//system("pause");
return ;
}
HDU1050(Moving Tables:贪心算法)的更多相关文章
- Moving Tables(贪心或Dp POJ1083)
Moving Tables Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28304 Accepted: 9446 De ...
- zstu.2512. Moving Tables(贪心)
Moving Tables Time Limit: 1 Sec Memory Limit: 64 MB Submit: 1182 Solved: 563 Description The famo ...
- Hdu1050 Moving Tables 2016-05-19 16:08 87人阅读 评论(0) 收藏
Moving Tables Problem Description The famous ACM (Advanced Computer Maker) Company has rented a floo ...
- hdu_1050 Moving Tables 贪心
Moving Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- 贪心算法训练(四)——(HDU1050)Moving Tables
题目描述 在一个狭窄的走廊里将桌子从一个房间移动到另一个房间,走廊的宽度只能允许一个桌子通过.给出 t,表示有 t 组测试数据,再给出 n,表示要移动 n 个桌子.n 下面有 n 行,每行两个数字,表 ...
- UVAlive 2326 Moving Tables(贪心 + 区间问题)
The famous ACM (Advanced Computer Maker) Company has rented a floor of a building whose shape is in ...
- --hdu 1050 Moving Tables(贪心)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1050 AC code: #include<stdio.h> #include<str ...
- hdu1050 Moving Tables
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1050 求区间上点的最大重叠次数. #include <stdio.h> #include &l ...
- POJ 1083 && HDU 1050 Moving Tables (贪心)
Moving Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
随机推荐
- Android手机开发者模式设置
通用设置 情景1 开发者选项已经激活,并且在设置列表中能看到 设置-->开发者选项(开发者选项已经激活) 情景2 开发者选项还没有激活,并且在设置列表中能不能看到 如果没有看到开发者选项是因办手 ...
- 点击文字可以选中相应的checkbox
<html><head><title>中国站长天空-网页特效-表单特效-点击文字选中的复选框</title><meta http-equiv=&q ...
- Missing artifact com.sun:tools:jar:1.5.0
http://java-suddy.iteye.com/blog/1821871(解决了我的问题)
- Eclipse中看java源代码
如何在Eclipse sdk中查看jar源代码如:*.jar 1.点 “window”-> "Preferences" -> "Java" -> ...
- ecshop 商店设置,新增或者修改字段
当想要新增选项到ecshop的商店设置时, 第一步:在ecs_shop_config这个表增加一条设置项记录 parent_id.code.type.value type 会有 group text ...
- html5 DeviceOrientation来实现手机网站上的摇一摇功能
原文地址:http://www.cootm.com/?p=706 从网上转载看到的,感觉不错,就转过来了,特此感谢 cnblogs 的 幸福2胖纸的码农生活,直接转载了,不要介意!呵呵 以下是转载内容 ...
- 高级停靠(Dock)技术的实现
高级停靠(Dock)技术的实现 介绍 所谓停靠就是可以用鼠标拖动窗体或者控件,并将其从一个父窗体移出或者移动到另一个父窗体上,可以按水平,垂直方向整齐排列, 并且可以停靠在分页控制组件上.下面的示意图 ...
- Session和Cookie的学习笔记2
<?php /* Cookie在计算机中保存的格式 用户名@网站地址[数字].txt Cookie在文件夹下,每个Cookie文件都是一个简单而又普通的文件件而不是程序,Cookie中的内容大多 ...
- CHROME下载地址
Chrome官方独立中文安装包下载地址 一般我们安装Google Chrome浏览器都是访问 http://www.google.com/chrome/?hl=zh-CN 然后下载运行ChromeSe ...
- 使用JAVA客户端对HDFS进行代码编写(五)
在linux中,在JAVA中编程,耗时的不是代码的编写而是环境的搭建,版本的选择...日了苍天,昨天eclipse突然抽风在linux运行不起来,耗了几个小时,试了各种办法...现在windows环境 ...