Gym - 100513E Election of a Mayor 贪心

在即将到来的市长选举中,现任市长面临严峻挑战。为确保胜选,竞选团队制定了合并投票站的策略,通过合并特定的投票站来增加胜算。本文详细解析了这一策略的实施方法及其背后的数学逻辑。

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

The Berland capital is preparing for mayoral election. There are two candidates for the position: the current mayor and his rival. The rival is a serious competitor, and it's not easy for current mayor to win the election.

The candidate will be declared the winner if he wins at more than a half of all polling stations. The results of the polling stations are supplied independently. The station winner is the candidate who gets more than a half of the votes of this station. No candidate is declared a winner of a polling station if both candidates have got the same number of votes at this station. Similarly, no candidate is declared a winner of the election if both candidates won at the same number of polling stations.

All eligible voters are going to take part in the election and have already decided whom to give their vote to. The campaign headquarters of the current mayor has collected data from all n stations of the city, and now for every station it is known how many people will vote for the current mayor and how many people will vote for his opponent.

The results have been disappointing for the current mayor, but his staff came up with a way to win the election. It was suggested to merge some pairs of polling stations in such a way that the current mayor will become the election winner. However, (for the sake of credibility), the proposed plan must comply with two conditions. Firstly, it is possible to merge only the pairs of stations with adjacent numbers (i.e., station j may be merged with either station j - 1, or with station j + 1). The resulting station cannot be merged again. Secondly, the number of such mergers for obvious reasons must be as few as possible.

Your task is to help the current mayor's campaign headquarters and produce such plan.

Input

The first line contains single integer n (2 ≤ n ≤ 2·105) — the number of the polling stations.

Each of the following n lines contains two integers mj and rj (0 ≤ mj, rj ≤ 105) — the number of voters at station j who plan to vote for the current mayor and his rival correspondingly.

Output

If current mayor cannot win the election at any condition, print a single number  - 1 to the first line.

Otherwise, to the first line print an integer u — the minimum number of polling station mergers the current mayor needs to perform in order to win. To each of the next u lines print a pair of integers — the numbers of the merged stations. The pairs as well as the numbers within each pair can be printed in any order. If there are multiple solutions, print any of them.

Examples

Input

7
15 8
8 10
14 14
12 13
13 12
21 10
20 30

Output

2
1 2
6 7

Input

2
1 5
5 1

Output

-1

Input

2
10 9
15 7

Output

0

题解:连续两个都平或者输可以合并,连续两个一个赢一个输或者平 合并后是赢的 可以合并  对总场数的贡献都是场数减1,赢的场数不变

#include<iostream>
#include<cstdio>
using namespace std;
const int N=2e5+100;
int a[N],b[N],y[N];
int ans,cnt;
struct node
{
	int a,b;
}ac[N];
int main()
{
	int n;
	while(~scanf("%d",&n))
	{
		cnt=0;
		for(int i=0;i<n;i++)
		{
			scanf("%d%d",&a[i],&b[i]);
			if(a[i]>b[i])
			{
				cnt++;
				y[i]=1;
			}
			else
				y[i]=0;
		}
		ans=max(0,n-(cnt*2-1));
		cnt=0;
		if(ans)
			for(int i=0;i<n-1;)
			{
				if((y[i]+y[i+1]==0)||(y[i]+y[i+1]==1&&a[i]+a[i+1]>b[i]+b[i+1]))
				{
					ac[cnt].a=i+1;
					ac[cnt].b=i+2;
					cnt++;
					if(cnt>=ans)
						break;
					i+=2;
				}
				else
					i++;
			}
		if(cnt<ans)
			printf("-1\n");
		else
		{
			printf("%d\n",ans);
			for(int i=0;i<ans;i++)
				printf("%d %d\n",ac[i].a,ac[i].b);
		}
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值