杭电 1896 Stones(题解+代码)

题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1896
题目:

Stones
Problem Description

Because of the wrong status of the bicycle, Sempr begin to walk east to west every morning and walk back every evening. Walking may cause a little tired, so Sempr always play some games this time.
There are many stones on the road, when he meet a stone, he will throw it ahead as far as possible if it is the odd stone he meet, or leave it where it was if it is the even stone. Now give you some informations about the stones on the road, you are to tell me the distance from the start point to the farthest stone after Sempr walk by. Please pay attention that if two or more stones stay at the same position, you will meet the larger one(the one with the smallest Di, as described in the Input) first.

Input
In the first line, there is an Integer T(1<=T<=10), which means the test cases in the input file. Then followed by T test cases.
For each test case, I will give you an Integer N(0<N<=100,000) in the first line, which means the number of stones on the road. Then followed by N lines and there are two integers Pi(0<=Pi<=100,000) and Di(0<=Di<=1,000) in the line, which means the position of the i-th stone and how far Sempr can throw it.

Output
Just output one line for one test case, as described in the Description.

Sample Input

2
2
1 5
2 4
2
1 5
6 6

Sample Output

11
12

题意:Sempr在走路的时候喜欢玩扔石头的游戏来打发时间。当他碰到 奇数次 的石头,他会把石头扔到Di远处:当他碰到第 偶数次 的石头,他不会去扔石头。当一个位置有两块石头的时候,他会扔 较轻 的那个石头,也就是说 可以扔的较远的那块石头(轻的石头才可以扔的远)。输出扔的最远的那块石头到起点的距离

题解:优先队列问题,建立结构体保存石头当前位置以及石头可以扔出去的距离,重载运算符。判断队列是否为空,以及遇到的石头是否为奇数次即可

需要注意的是:扔出去的石头在下一次碰到的时候是可以再扔的

代码及注释如下:

#include<iostream>
#include<queue>
using namespace std;
struct node{
	int pos;//石块当前位置 
	int dis;//石块可以扔出去的距离 
	friend bool operator < (node a,node b) {//运算符重载 
		if(a.pos==b.pos) return a.dis>b.dis;
		return a.pos>b.pos;
	}
};
int main() {
	int t;
	cin>>t;
	while(t--) {
		priority_queue<node> q;
		int n,num=1;//遇见石块的次数 
		node temp;
		cin>>n;//石块数量 
		for(int i=1;i<=n;i++) {
			int a,b;
			cin>>a>>b;
			temp.pos=a;//石块当前的位置 
			temp.dis=b;//石块可以扔出去的距离 
			q.push(temp);
		}
		while(!q.empty()) {//判断是否还有石块
			temp=q.top();
			q.pop();
			if(num%2) {//判断是奇数次还是偶数次 
				temp.pos+=temp.dis;//修改石块位置 
				q.push(temp);
			}
			num++;//遇见石块的次数++ 
		}
		cout<<temp.pos<<endl;//temp保存的就是最后一次扔的石块
	}
	return 0;
} 

ac图片

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
这段代码中有几个问题: 1. while 循环的判断条件应该是 `stones.length > 1`,因为只有至少有两个石头时才需要进行破碎的操作。所以应该将 `while( stones.length==1 || stones.length==0 )` 修改为 `while( stones.length > 1 )`。 2. 在交换石头的位置时,应该使用临时变量来保存一个石头的值,然后再进行交换。所以应该将 `a=stones[j];` 修改为 `int temp = stones[i];`,将 `stones[i]=stones[j];` 修改为 `stones[i]=stones[j];`,将 `stones[j]=a;` 修改为 `stones[j]=temp;`。 3. 在判断最后剩下的石头重量时,应该使用索引 `stones.length-1` 和 `stones.length-2` 来获取最后两个石头的重量。所以应该将 `if(stones[stones.length-1]<=stones[stones.length])` 修改为 `if(stones[stones.length-1]<=stones[stones.length-2])`,将 `stones.length=stones.length-2;` 修改为 `stones.length -= 2;`。 4. 最后返回的应该是 `stones[0]`,而不是 `stones[stones.length]`。所以应该将 `return stones[stones.length];` 修改为 `return stones[0];`。 修正后的代码如下: ```java class Solution { public int lastStoneWeight(int[] stones) { while (stones.length > 1) { for (int i = 0; i < stones.length; i++) { for (int j = 1; j < stones.length; j++) { if (stones[i] > stones[j]) { int temp = stones[i]; stones[i] = stones[j]; stones[j] = temp; } } } if (stones[stones.length - 1] <= stones[stones.length - 2]) { stones[stones.length - 2] -= stones[stones.length - 1]; stones = Arrays.copyOf(stones, stones.length - 1); } else { stones = Arrays.copyOf(stones, stones.length - 2); } } return stones[0]; } } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值