Gym - 100513E Election of a Mayor 贪心

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.

扫描二维码关注公众号,回复: 4287525 查看本文章

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;
}

猜你喜欢

转载自blog.csdn.net/mmk27_word/article/details/84556819