2017-5-2-Train:Codeforces Round #323 (Div. 2)

A. Asphalting Roads(模拟)

City X consists of n vertical and n horizontal infinite roads, forming n × n intersections. Roads (both vertical and horizontal) are numbered from 1 to n, and the intersections are indicated by the numbers of the roads that form them.

Sand roads have long been recognized out of date, so the decision was made to asphalt them. To do this, a team of workers was hired and a schedule of work was made, according to which the intersections should be asphalted.

Road repairs are planned for n2 days. On the i-th day of the team arrives at the i-th intersection in the list and if none of the two roads that form the intersection were already asphalted they asphalt both roads. Otherwise, the team leaves the intersection, without doing anything with the roads.

According to the schedule of road works tell in which days at least one road will be asphalted.

Input

The first line contains integer n (1 ≤ n ≤ 50) — the number of vertical and horizontal roads in the city.

Next n2 lines contain the order of intersections in the schedule. The i-th of them contains two numbers h**i, v**i (1 ≤ h**i, v**i ≤ n), separated by a space, and meaning that the intersection that goes i-th in the timetable is at the intersection of the h**i-th horizontal and v**i-th vertical roads. It is guaranteed that all the intersections in the timetable are distinct.

Output

In the single line print the numbers of the days when road works will be in progress in ascending order. The days are numbered starting from 1.

Examples

input

2
1 1
1 2
2 1
2 2

output

1 4 

input

1
1 1

output

1 

Note

In the sample the brigade acts like that:

  1. On the first day the brigade comes to the intersection of the 1-st horizontal and the 1-st vertical road. As none of them has been asphalted, the workers asphalt the 1-st vertical and the 1-st horizontal road;
  2. On the second day the brigade of the workers comes to the intersection of the 1-st horizontal and the 2-nd vertical road. The 2-nd vertical road hasn‘t been asphalted, but as the 1-st horizontal road has been asphalted on the first day, the workers leave and do not asphalt anything;
  3. On the third day the brigade of the workers come to the intersection of the 2-nd horizontal and the 1-st vertical road. The 2-nd horizontal road hasn‘t been asphalted but as the 1-st vertical road has been asphalted on the first day, the workers leave and do not asphalt anything;
  4. On the fourth day the brigade come to the intersection formed by the intersection of the 2-nd horizontal and 2-nd vertical road. As none of them has been asphalted, the workers asphalt the 2-nd vertical and the 2-nd horizontal road.

Code:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 static const int MAXN = 66;
 4 int n;
 5 int vis1[MAXN];
 6 int vis2[MAXN];
 7 int x , y;
 8 vector<int> ans;
 9 int main()
10 {
11     //freopen("D:\\系统优化\\Desktop\\littlepea\\in.data" , "r" , stdin);
12     scanf("%d" , &n);
13     n *= n;
14     for(int i = 1 ; i <= n ; ++i)
15     {
16         scanf("%d%d" , &x , &y);
17         if(vis1[x] || vis2[y])
18             continue;
19         ans.push_back(i);
20         vis1[x] = vis2[y] = 1;
21     }
22     for(auto i: ans)
23         printf("%d " , i);
24 }

B. Robot‘s Task(贪心 + 模拟)

Robot Doc is located in the hall, with n computers stand in a line, numbered from left to right from 1 to n. Each computer containsexactly one piece of information, each of which Doc wants to get eventually. The computers are equipped with a security system, so to crack the i-th of them, the robot needs to collect at least a**i any pieces of information from the other computers. Doc can hack the computer only if he is right next to it.

The robot is assembled using modern technologies and can move along the line of computers in either of the two possible directions, but the change of direction requires a large amount of resources from Doc. Tell the minimum number of changes of direction, which the robot will have to make to collect all n parts of information if initially it is next to computer with number 1.

It is guaranteed that there exists at least one sequence of the robot‘s actions, which leads to the collection of all information. Initially Doc doesn‘t have any pieces of information.

Input

The first line contains number n (1 ≤ n ≤ 1000). The second line contains n non-negative integers a1, a2, ..., a**n (0 ≤ a**i < n), separated by a space. It is guaranteed that there exists a way for robot to collect all pieces of the information.

Output

Print a single number — the minimum number of changes in direction that the robot will have to make in order to collect all n parts of information.

Examples

input

3
0 2 0

output

1

input

5
4 2 3 0 1

output

3

input

7
0 3 1 0 5 2 6

output

2

Note

In the first sample you can assemble all the pieces of information in the optimal manner by assembling first the piece of information in the first computer, then in the third one, then change direction and move to the second one, and then, having 2 pieces of information, collect the last piece.

In the second sample to collect all the pieces of information in the optimal manner, Doc can go to the fourth computer and get the piece of information, then go to the fifth computer with one piece and get another one, then go to the second computer in the same manner, then to the third one and finally, to the first one. Changes of direction will take place before moving from the fifth to the second computer, then from the second to the third computer, then from the third to the first computer.

In the third sample the optimal order of collecting parts from computers can look like that: 1->3->4->6->2->5->7.

Code:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 deque<int> dq[2];
 4 bool flag;
 5 int n;
 6 int eat;
 7 int change;
 8 int x;
 9 int main()
10 {
11     scanf("%d" , &n);
12     for(int i = 1 ; i <= n ; ++i)
13     {
14         scanf("%d" , &x);
15         dq[0].push_back(x);
16     }
17     while(!dq[flag].empty())
18     {
19         x = dq[flag].front();
20         dq[flag].pop_front();
21         if(x <= eat)
22         {
23             ++eat;
24         }
25         else
26         {
27             dq[!flag].push_front(x);
28         }
29         if(dq[flag].empty())
30         {
31             flag = !flag;
32             if(!dq[flag].empty())
33                 ++change;
34         }
35     }
36     printf("%d" , change);
37 }

C. GCD Table(思维 + 暴力)

The GCD table G of size n × n for an array of positive integers a of length n is defined by formula

Let us remind you that the greatest common divisor (GCD) of two positive integers x and y is the greatest integer that is divisor of both xand y, it is denoted as . For example, for array a = {4, 3, 6, 2} of length 4 the GCD table will look as follows:

Given all the numbers of the GCD table G, restore array a.

Input

The first line contains number n (1 ≤ n ≤ 500) — the length of array a. The second line contains n2 space-separated numbers — the elements of the GCD table of G for array a.

All the numbers in the table are positive integers, not exceeding 109. Note that the elements are given in an arbitrary order. It is guaranteed that the set of the input data corresponds to some array a.

Output

In the single line print n positive integers — the elements of array a. If there are multiple possible solutions, you are allowed to print any of them.

Examples

input

4
2 1 2 3 4 3 2 6 1 1 2 2 1 2 3 2

output

4 3 6 2

input

1
42

output

42 

input

2
1 1 1 1

output

1 1 

Code:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 static const int MAXN = 510 * 510;
 5 map<int , int> mp;
 6 LL data[MAXN];
 7 vector<LL> ans;
 8 int n;
 9 int main()
10 {
11     scanf("%I64d" , &n);
12     int t = n * n;
13     for(int i = 1 ; i <= t ; ++i)
14     {
15         scanf("%I64d" , &data[i]);
16         ++mp[data[i]];
17     }
18     sort(data + 1 , data + 1 + t , [](LL a , LL b){return a > b;});
19     for(int i = 1 ; i <= t ; ++i)
20     {
21         if(!mp[data[i]])
22             continue;
23         --mp[data[i]];
24         for(auto p: ans)
25             mp[__gcd(data[i] , p)] -= 2;
26         ans.push_back(data[i]);
27     }
28     for(auto x: ans)
29         printf("%I64d " , x);
30 }

时间: 2024-08-04 01:56:06

2017-5-2-Train:Codeforces Round #323 (Div. 2)的相关文章

Codeforces Round #323 (Div. 2)

被进爷坑了,第二天的比赛改到了12点 水 A - Asphalting Roads /************************************************ * Author :Running_Time * Created Time :2015/10/3 星期六 21:53:09 * File Name :A.cpp ************************************************/ #include <cstdio> #include

Codeforces Round #323 (Div. 2) D.Once Again...

D. Once Again... You are given an array of positive integers a1, a2, ..., an × T of length n × T. We know that for any i > n it is true that ai = ai - n. Find the length of the longest non-decreasing sequence of the given array. Input The first line

Codeforces Round #323 (Div. 2) D. Once Again... 暴力+最长非递减子序列

                                                                              D. Once Again... You are given an array of positive integers a1, a2, ..., an × T of length n × T. We know that for any i > n it is true that ai = ai - n. Find the length of

Codeforces Round #323 (Div. 2) C. GCD Table

C. GCD Table The GCD table G of size n × n for an array of positive integers a of length n is defined by formula Let us remind you that the greatest common divisor (GCD) of two positive integers x and y is the greatest integer that is divisor of both

Codeforces Round #323 (Div. 2) E - Superior Periodic Subarrays

E - Superior Periodic Subarrays 好难的一题啊... 这个博客讲的很好,搬运一下. https://blog.csdn.net/thy_asdf/article/details/49406133 #include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mk make_pair #define pii pair<int,int> #de

Codeforces Round #279 (Div. 2) ABCD

Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name     A Team Olympiad standard input/output 1 s, 256 MB  x2377 B Queue standard input/output 2 s, 256 MB  x1250 C Hacking Cypher standard input/output 1 s, 256 MB  x740 D Chocolate standard input/

Codeforces Round #257(Div.2) D Jzzhu and Cities --SPFA

题意:n个城市,中间有m条道路(双向),再给出k条铁路,铁路直接从点1到点v,现在要拆掉一些铁路,在保证不影响每个点的最短距离(距离1)不变的情况下,问最多能删除多少条铁路 分析:先求一次最短路,铁路的权值大于该点最短距离的显然可以删去,否则将该条边加入图中,再求最短路,记录每个点的前一个点,然后又枚举铁路,已经删去的就不用处理了,如果铁路权值大于该点最短距离又可以删去,权值相等时,该点的前一个点如果不为1,则这个点可以由其他路到达,这条铁路又可以删去. 由于本题中边比较多,最多可以有8x10^

Codeforces Round #515 (Div. 3)

Codeforces Round #515 (Div. 3) 1 #include<bits/stdc++.h> 2 #include<iostream> 3 #include<cstdio> 4 #include<cstdlib> 5 #include<cstring> 6 #include<cmath> 7 #include<algorithm> 8 #include<queue> 9 #include&l

Codeforces Round #542 (Div. 2)

Codeforces Round #542 (Div. 2) 题目做不下去的我写一写题解 A. Be Positive 水题,考虑正数,负数个数是否\(\geq \lceil \frac{n}{2} \rceil\) B. Two Cakes 给定位置大小为1...n的蛋糕,每种大小共两份,要求两个人从最左边的位置增序各取1..n的蛋糕,求最小步长 两个一起考虑,取到第i块蛋糕,两人的位置状态是确定的,无论是a,b两人怎样分布总是占据了这个位置,有点类似蚂蚁相遇后逆行的想法 所以我们只需min(