寒假训练20200126(补题)

B - Berstagram CodeForces - 1250A

Problem Description

Polycarp recently signed up to a new social network Berstagram. He immediately published n posts there. He assigned numbers from 1 to n to all posts and published them one by one. So, just after publishing Polycarp’s news feed contained posts from 1 to n — the highest post had number 1, the next one had number 2, …, the lowest post had number n.
After that he wrote down all likes from his friends. Likes were coming consecutively from the 1-st one till the m-th one. You are given a sequence a1,a2,…,am (1≤aj≤n), where aj is the post that received the j-th like.
News feed in Berstagram works in the following manner. Let’s assume the j-th like was given to post aj. If this post is not the highest (first) one then it changes its position with the one above. If aj is the highest post nothing changes.
For example, if n=3, m=5 and a=[3,2,1,3,3], then Polycarp’s news feed had the following states:
before the first like: [1,2,3];
after the first like: [1,3,2];
after the second like: [1,2,3];
after the third like: [1,2,3];
after the fourth like: [1,3,2];
after the fifth like: [3,1,2].
Polycarp wants to know the highest (minimum) and the lowest (maximum) positions for each post. Polycarp considers all moments of time, including the moment “before all likes”.

Input

The first line contains two integer numbers n and m (1≤n≤105, 1≤m≤4⋅105) — number of posts and number of likes.
The second line contains integers a1,a2,…,am (1≤aj≤n), where aj is the post that received the j-th like.

Output

Print n pairs of integer numbers. The i-th line should contain the highest (minimum) and the lowest (maximum) positions of the i-th post. You should take into account positions at all moments of time: before all likes, after each like and after all likes. Positions are numbered from 1 (highest) to n (lowest).

Examples

Input
3 5
3 2 1 3 3
Output
1 2
2 3
1 3

Input
10 6
7 3 5 7 3 6
Output
1 2
2 3
1 3
4 7
4 5
6 7
5 7
8 8
9 9
10 10


题意

给定n个数的序列,序列为1~n,有m次操作。
每次操作将x这个数向前提一位。得到新的序列。
问每个数的最高位置和最低位置分别是什么。

思路

暴力模拟。

坑点

感觉有点小细节。


Code

#include <bits/stdc++.h>
//#include <cstdio>
//#include <algorithm>
//#include <cstring>
//#include <cmath>
//#include <vector>
//#include <queue>

using namespace std;

typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VII;
typedef vector<VII> VIII;
typedef pair<int, int> PII;

#define rep(i,a,b) for(int i=(a); i<=(b); i++)
#define per(i,a,b) for(int i=(a); i>=(b); i--)

const int N = 1e5 + 5;
const int INF = 0x3f3f3f3f;

int maxn[N], minn[N];
int pos[N];
int who[N];

void init()
{
	rep(i, 1, N-1)
	{
		maxn[i] = i;
		minn[i] = i;
		pos[i] = i;
		who[i] = i;
	}
}

int main()
{
	int n, m;
	scanf("%d%d", &n, &m);
	init();
	rep(i, 1, m)
	{
		int num;
		scanf("%d", &num);
		if(pos[num] == 1) continue;
		int p = pos[num];
		int tmp = who[p-1];
		swap(pos[num], pos[tmp]);
		who[p] = tmp;
		who[p-1] = num;
		maxn[num] = min(pos[num], maxn[num]);
		minn[tmp] = max(pos[tmp], minn[tmp]);
	}
	rep(i, 1, n)
	{
		printf("%d %d\n", maxn[i], minn[i]);
	}
	return 0;
}



C - Constanze’s Machine CodeForces - 1245C



D - Submarine in the Rybinsk Sea (easy edition) CodeForces - 1195D1



E - Almost All Divisors CodeForces - 1165D

Problem Description

We guessed some integer number x. You are given a list of almost all its divisors. Almost all means that there are all divisors except 1 and x in the list.
Your task is to find the minimum possible integer x that can be the guessed number, or say that the input data is contradictory and it is impossible to find such number.
You have to answer t independent queries.

Input

The first line of the input contains one integer t (1≤t≤25) — the number of queries. Then t queries follow.
The first line of the query contains one integer n (1≤n≤300) — the number of divisors in the list.
The second line of the query contains n integers d1,d2,…,dn (2≤di≤106), where di is the i-th divisor of the guessed number. It is guaranteed that all values di are distinct.

Output

For each query print the answer to it.
If the input data in the query is contradictory and it is impossible to find such number x that the given list of divisors is the list of almost all its divisors, print -1. Otherwise print the minimum possible x.

Example

Input
2
8
8 2 12 6 4 24 16 3
1
2
Output
48
4


题意

给定一个序列,求一个最小的x,这个x的所有除1和本身以外的因子序列正好为这个给定序列。若没有这样的x则输出-1。

思路

这个给定序列的最大值乘最小值必定为x的可能值,只需要验证x是否符合条件即可。
从2枚举到sqrt(x)判断即可。

坑点

无。


Code

#include <bits/stdc++.h>
//#include <cstdio>
//#include <algorithm>
//#include <cstring>
//#include <cmath>
//#include <vector>
//#include <queue>

using namespace std;

typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VII;
typedef vector<VII> VIII;
typedef pair<int, int> PII;

#define rep(i,a,b) for(int i=(a); i<=(b); i++)
#define per(i,a,b) for(int i=(a); i>=(b); i--)

const int N = 1e6 + 5;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;

int num[N];

void init()
{
    memset(num, 0, sizeof(num));
}

int main()
{
	int T;
	scanf("%d", &T);
	while(T --)
    {
        init();
        int n;
        scanf("%d", &n);
        int minn = 0x3f3f3f3f;
        int maxn = 1;
        rep(i,1,n)
        {
            int x;
            scanf("%d", &x);
            num[x] ++;
            minn = min(minn, x);
            maxn = max(maxn, x);
        }
        ll ans = 1ll * minn * maxn;
        int sqr = (int)sqrt(ans);
        rep(i,2,sqr)
        {
            if(ans%i == 0)
            {
                if(num[i]==0 || num[ans/i] == 0)
                {
                    ans = -1;
                    break;
                }
                else
                {
                    num[i] --;
                    if(i != ans/i) num[ans/i] --;
                }
            }
        }
        rep(i,1,N-1)
        {
            if(num[i] != 0)
            {
                ans = -1;
                break;
            }
        }
        printf("%I64d\n", ans);
    }
	return 0;
}

发布了42 篇原创文章 · 获赞 6 · 访问量 4014

猜你喜欢

转载自blog.csdn.net/qq_43383246/article/details/104090404