Codeforces Round #323 (Div. 2) (ABCD题解)

54 篇文章 0 订阅
53 篇文章 0 订阅

比赛链接:http://codeforces.com/contest/583


A. Asphalting Roads
time limit per test
1 second
memory limit per test
256 megabytes

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 hi, vi (1 ≤ hi, vi ≤ n), separated by a space, and meaning that the intersection that goes i-th in the timetable is at the intersection of the hi-th horizontal and vi-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.

Sample test(s)
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.


题目大意:如果一个点的水平和竖直方向的直线都没被访问,则输出点的ID

题目分析:模拟

#include <cstdio>
int const MAX = 1e4 + 5;
bool vis1[MAX], vis2[MAX];
int ans[MAX];

int main()
{
    int n, cnt = 0;
    scanf("%d", &n);
    for(int i = 0; i < n * n; i++)
    {
        int u, v;
        scanf("%d %d", &u, &v);
        if(!vis1[u] && !vis2[v])
        {
            vis1[u] = true;
            vis2[v] = true;
            ans[cnt ++] = i + 1;
        } 
    }
    for(int i = 0; i < cnt; i++)
        printf("%d ", ans[i]);
}


B. Robot's Task
time limit per test
1 second
memory limit per test
256 megabytes

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 ai 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, ..., an (0 ≤ ai < 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.

Sample test(s)
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.

题目大意:一个机器人在一条线段上收集物品,每个点有个值,只有机器人拿到的物品数大于等于点值才能拿当前点的物品,机器人从点1开始,问收集完所有的物品最少需要掉头几次。保证有解

题目分析:还是模拟,既然只问掉头次数,显然每次都走整个线段的长度是最优的,保证有解,根据题意模拟即可

#include <cstdio>
bool vis[1005];
int  a[1005];

int main()
{
    int n;
    scanf("%d", &n);
    for(int i = 1; i <= n; i++)
        scanf("%d", &a[i]);
    int turn = 0;
    a[n + 1] = a[0] = 1005;
    for(int num = 0, dir = 1, i = 1; num < n; i += dir)
    {
        if(dir == 1 && i == n + 1)
        {
            turn ++;
            dir *= -1;
        }
        else if(dir == -1 && i == 0)
        {
            turn ++;
            dir *= -1;
        }
        if(!vis[i] && num >= a[i])
        {
            num ++;
            vis[i] = true;
        }
    }
    printf("%d\n", turn);
}

C. GCD Table
time limit per test
2 seconds
memory limit per test
256 megabytes

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.

Sample test(s)
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 

题目大意:给n*n个GCD表里的数字,要求按规则恢复原数组

题目分析:显然数字大的gcd要优先算且最大的肯定是原数组的元素,先记录所有数字的频数mp[a]表示a出现了mp[a]次,也可以理解为a与别的数(包括自己)的最大公约数为a的对数有mp[a]个,然后对原数组排序,从大数字开始记录答案,注意记录完要更新mp的值,比如gcd(6,2) == 2,最大公约数为2的对数要减2,因为GCD表是对称的

#include <cstdio>
#include <algorithm>
#include <map>
int const MAX = 500 * 500 + 5;
using namespace std;
int a[MAX], ans[MAX];
map <int, int> mp;

int gcd(int a, int b)
{
    return b ? gcd(b, a % b) : a;  
}

int main()
{
    int n, cnt = 0;
    scanf("%d", &n);
    for(int i = 0; i < n * n; i++)
    {
        scanf("%d", &a[i]);
        mp[a[i]] ++;
    }
    sort(a, a + n * n);
    for(int i = n * n - 1; i >= 0; i--)
    {
        if(mp[a[i]] > 0)
        {
            ans[cnt ++] = a[i];
            for(int j = 0; j < cnt; j++)
                mp[gcd(a[i], ans[j])] -= 2;
        }
    }
    for(int i = 0; i < cnt - 1; i++)
        printf("%d ", ans[i]);
    printf("%d\n", ans[cnt - 1]);
}

D. Once Again...
time limit per test
1 second
memory limit per test
256 megabytes

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 contains two space-separated integers: nT (1 ≤ n ≤ 1001 ≤ T ≤ 107). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 300).

Output

Print a single number — the length of a sought sequence.

Sample test(s)
input
4 3
3 1 4 2
output
5
Note

The array given in the sample looks like that: 3, 1, 4, 23, 1, 4, 2, 3, 1, 4, 2. The elements in bold form the largest non-decreasing subsequence.


题目大意:把一个数组循环T次,求LIS的长度

题目分析:n为100,其实对于LIS,我们只需要求n*n的即可,这里求的是理论上长度最大的最小范围,很好理解,一个数组n个元素,假设我每次都只选一个来保证得到最长不减序列,那么最多只需要选择n*n次,最多其实就是倒序的情况,比如 5 4 3 2 1,循环5次的话,第一次选1,第二次选2,以此类推。求完以后剩余的其实只用算原数组中频数最大的那个即可,因为求的是最长不减序列,重复的数字可以直接插进去

#include <cstdio>
#include <algorithm>
using namespace std;
int n, T;
int a[30005], num[305];
int stk[30005];

int main() 
{
    scanf("%d %d", &n, &T);
    int ma = 0;
    for(int i = 0; i < n; i++) 
    {
        scanf("%d", &a[i]);
        num[a[i]] ++;
        ma = max(ma, num[a[i]]);
    }
    int ans = 0, len = min(100, n), top = 0;
    for(int i = n; i < n * len; i++)
        a[i] = a[i - n];
    for(int i = 0; i < n * len; i++) 
    {
        if(a[i] >= stk[top])
            stk[++ top] = a[i];
        else
        {
            int pos = upper_bound(stk, stk + top + 1, a[i]) - stk;
            stk[pos] = a[i];
        }
        ans = max(ans, top);
    }
    printf("%d\n", ans + (T - len) * ma);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值