HDU 4022 Bombing c++解法

It’s a cruel war which killed millions of people and ruined series of cities. In order to stop it, let’s bomb the opponent’s base.
It seems not to be a hard work in circumstances of street battles, however, you’ll be encountered a much more difficult instance: recounting exploits of the military. In the bombing action, the commander will dispatch a group of bombers with weapons having the huge destructive power to destroy all the targets in a line. Thanks to the outstanding work of our spy, the positions of all opponents’ bases had been detected and marked on the map, consequently, the bombing plan will be sent to you.
Specifically, the map is expressed as a 2D-plane with some positions of enemy’s bases marked on. The bombers are dispatched orderly and each of them will bomb a vertical or horizontal line on the map. Then your commanded wants you to report that how many bases will be destroyed by each bomber. Notice that a ruined base will not be taken into account when calculating the exploits of later bombers.

Input
Multiple test cases and each test cases starts with two non-negative integer N (N<=100,000) and M (M<=100,000) denoting the number of target bases and the number of scheduled bombers respectively. In the following N line, there is a pair of integers x and y separated by single space indicating the coordinate of position of each opponent’s base. The following M lines describe the bombers, each of them contains two integers c and d where c is 0 or 1 and d is an integer with absolute value no more than 10 9, if c = 0, then this bomber will bomb the line x = d, otherwise y = d. The input will end when N = M = 0 and the number of test cases is no more than 50.

Output
For each test case, output M lines, the ith line contains a single integer denoting the number of bases that were destroyed by the corresponding bomber in the input. Output a blank line after each test case.

写一下本人的解法,一开始想到用结构体去写,写完一个O(N)的,oj上超时。后面用map写O(logn)的,也是超时。最后把cin改成scanf才能ac。说明这道题的测试样例点很多。而scanf读入速度约为cin的10倍,这题时间要求挺严格的。

下面粘贴代码:

#include <map>
#include <iostream>
#include <set>
#include <cstdio>
using namespace std;

int main() {
	int n, m;
	while (cin >> n >> m) {
		map<long long int, multiset<long long int > > mapx;
		map<long long int, multiset<long long int > > mapy;
		if (n == 0 && m == 0)
			break;
		long long int x, y;
		for (int i = 0; i < n; i++) {
			scanf("%lld %lld", &x, &y);
			mapx[y].insert(x);
			mapy[x].insert(y);
		}
		multiset<long long int >::iterator it;
		long long int choose, line;
		for (int i = 0; i < m; i++) {
			cin >> choose >> line;
			if (choose == 1) {
				cout << mapx[line].size() << endl;//用y找有几个x,输出对应数量
				for (it = mapx[line].begin(); it != mapx[line].end(); it++) {//输出完之后,把另一个map中的对应点删掉
					if (mapy[*it].empty())//不判断empty可能会报错,我的编译器会报错。
						continue;
					else
						mapy[*it].erase(mapy[*it].find(line));
				}
				mapx[line].clear();//删除y=line时所有的x
			}
			if (choose == 0) {//同理用x找有几个y时
				cout << mapy[line].size() << endl;
				for (it = mapy[line].begin(); it != mapy[line].end(); it++) {
					if (mapx[*it].empty())
						continue;
					else
						mapx[*it].erase(mapx[*it].find(line));
				}
				mapy[line].clear();
			}
		}
		cout << endl;
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于HDU4546问题,还可以使用优先队列(Priority Queue)来解决。以下是使用优先队列的解法思路: 1. 首先,将数组a进行排序,以便后续处理。 2. 创建一个优先队列(最小堆),用于存储组合之和的候选值。 3. 初始化优先队列,将初始情况(即前0个数的组合之和)加入队列。 4. 开始从1到n遍历数组a的元素,对于每个元素a[i],将当前队列中的所有候选值取出,分别加上a[i],然后再将加和的结果作为新的候选值加入队列。 5. 重复步骤4直到遍历完所有元素。 6. 当队列的大小超过k时,将队列中的最小值弹出。 7. 最后,队列中的所有候选值之和即为前k小的组合之和。 以下是使用优先队列解决HDU4546问题的代码示例: ```cpp #include <iostream> #include <vector> #include <queue> #include <functional> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a.begin(), a.end()); // 对数组a进行排序 priority_queue<long long, vector<long long>, greater<long long>> pq; // 最小堆 pq.push(0); // 初始情况,前0个数的组合之和为0 for (int i = 0; i < n; i++) { long long num = pq.top(); // 取出当前队列中的最小值 pq.pop(); for (int j = i + 1; j <= n; j++) { pq.push(num + a[i]); // 将所有加和结果作为新的候选值加入队列 num += a[i]; } if (pq.size() > k) { pq.pop(); // 当队列大小超过k时,弹出最小值 } } long long sum = 0; while (!pq.empty()) { sum += pq.top(); // 求队列中所有候选值之和 pq.pop(); } cout << sum << endl; return 0; } ``` 使用优先队列的方法可以有效地找到前k小的组合之和,时间复杂度为O(nklog(k))。希望这个解法对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值