[HDU] [1896] Stones

Problem Description

Loading...

Problem Analysis

Problems like this are always harder to understand than to solve
Once you’ve sorted out the idea, writing it up might be very simple and it’s AC…

Using a Priority Queue makes this fairly convenient

The gist of the problem is
On encountering a stone for an odd-numbered time, throw it forward di meters
On encountering a stone for an even-numbered time, ignore it
Repeat this until no more stones are encountered
Ask how far the farthest stone is from the starting point
(always starting from 1)

This is a process of continuously adding stones to the queue
until the queue is empty (no stones ahead)
Using a Priority Queue lets you conveniently pick stones by
prioritizing Pi first and then Di

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<stdio.h>
#include<queue>
using namespace std;
struct node {
	int p,d;
	bool friend operator<(node A,node B) {
		if(A.p==B.p)
			return A.d>B.d;
		else
			return A.p>B.p;
	}
} a;
int main() {
	int T;
	scanf("%d",&T);
	while(T--) {
		priority_queue<node>q;
		int n;
		scanf("%d",&n);
		while(n--) {
			scanf("%d %d",&a.p,&a.d);
			q.push(a);
		}
		int time=0,flag=0;
		while(!q.empty()) {
			time++;
			if(time&1) {
				a=q.top();
				a.p+=a.d;
				if(a.p>flag)
					flag=a.p;
				q.push(a);
				q.pop();
			} else {
				a=q.top();
				if(a.p>flag)
					flag=a.p;
				q.pop();
			}
		}
		printf("%d\n",flag);
	}
	return 0;
}

Problem link: 【杭电】[1896]Stones

This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...