hdu 1050 Moving Table

本文提供了一种解决HDU 1050问题的有效算法,通过预处理输入数据并进行排序来判断家具是否可以一次性移动到位。代码使用C++实现,并详细解释了解决方案的关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1050

很容猜到是用信心,但老是做不对,,抓狂,,郁闷!!!

 首先要注意几点:

1。对数据的预先处理。

对于 (room 2 to room 3 ) and (room 4 to room 6), It's impossible move in once.

when room 2 to roo 3 ,it ocupy space 1 to 4.

So while room 4 to room 6. space 4 will be used by both.It's impossible.安静

 

2。下面就是排序了,按开始位置排序。

 

Game Over!

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

struct Node {
	int s, e;
	bool flag;
	bool operator <(const Node &elem) const {
		if(s!=elem.s)
			return s < elem.s;
		else
			return e < elem.e;
	}
} move[209];

int main(int argc, char **argv) {
//	freopen("input.txt","r",stdin);
	int i, n, times, finished, T;
	cin >> T;
	while (T--) {
		cin >> n;
		for (i = 0; i < n; ++i) {
			cin >> move[i].s >> move[i].e;
			if (move[i].s > move[i].e)
				swap(move[i].s, move[i].e);

			if (move[i].s % 2 == 0)
				move[i].s--;
			if (move[i].e % 2 != 0)
				move[i].e++;

			move[i].flag = true;
		}
		sort(move, move + n);

		times = 0;
		while (true) {
			i = 0;
			while (!(move[i].flag) && i < n)
				++i;

			if (i < n) {
				move[i].flag = false;
				finished = move[i].e;
				++times;
				++i;
			} else
				break;

			while(i<n){
				if (move[i].flag && finished < move[i].s){
					move[i].flag = false;
					finished=move[i].e;
				}
				++i;
			}
		}
		cout << 10 * times << endl;
	}
	return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值