Election of a Mayor (模拟)

Election of a Mayor

 Gym - 100513E 

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 allpolling 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  - 1to 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
题意:给你n个站点,每个站点包含着市长和候选人的得票,可以对某些相邻的站点的得票数进行合并,市长得票数超过候选人得票数的站点超过总站点数目的一半,那么市长胜,如果市长可以经过某些操作获胜,那么输出最小的操作数,否则输出-1.
题解:
  这个题是我考虑的太不周到了,以为只有相邻的站点一个市长胜,一个市长败或者市长平才合并,其实如果相邻两个站点市长同时败或者同时平或者一平一败都可以合并,使得总站点数减一。
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 const int maxn=2e5+10;
 7 int n;
 8 int vis[maxn];
 9 struct node{
10     int a;
11     int b;
12 }e[maxn],ans[maxn];
13 int mp[maxn];
14 int res[maxn];
15 int main()
16 {
17     int wnum=0;
18     cin>>n;
19     int cnt=0;
20     int tot=n;
21     for(int i=1;i<=n;i++)
22     {
23         scanf("%d%d",&e[i].a,&e[i].b);
24         if(e[i].a>e[i].b)
25         {
26             wnum++;
27         }
28     }
29     int t=0;
30     int sum=0;
31     for(int i=1;wnum<=tot/2&&i<n;i++)
32     {
33         int flag=0;
34         if(e[i].a<=e[i].b)
35         {
36             if(e[i+1].a<=e[i+1].b)
37             {
38                 flag=1;
39                 tot--;
40                 ++i;
41                 sum++;
42                 res[cnt++]=i;
43                 ans[t].a=i;
44                 ans[t++].b=i+1;
45             }
46             else
47             {
48                 if(e[i].a+e[i+1].a>e[i].b+e[i+1].b)
49                 {
50                     flag=1;
51                     tot--;
52                     ++i;
53                     sum++;
54                     res[cnt++]=i;
55                     ans[t].a=i;
56                     ans[t++].b=i+1;
57                 }
58             }
59         }
60         else
61         {
62             if(e[i+1].a<=e[i+1].b)
63             {
64                 if(e[i].a+e[i+1].a>e[i].b+e[i+1].b)
65                 {
66                     flag=1;
67                     tot--;
68                     ++i;
69                     sum++;
70                     ans[t].a=i;
71                     res[cnt++]=i;
72                     ans[t++].b=i+1;
73                 }
74             }
75         }
76     }
77 
78     if(wnum<=tot/2)
79         puts("-1");
80     else
81     {
82         printf("%d\n",sum);
83         /*for(int i=0;i<t;i++)
84         {
85             printf("%d %d\n",ans[i].a,ans[i].b);
86         }*/
87         for(int i=0;i<cnt;i++)
88         {
89             printf("%d %d\n",res[i]-1,res[i]);
90         }
91     }
92 }

 

posted @ 2018-12-05 11:30  *starry*  阅读(263)  评论(0编辑  收藏  举报