20200523混合训练总结

Points and Powers of Two

There are n distinct points on a coordinate line, the coordinate of i-th point equals to xi. Choose a subset of the given set of points such that the distance between each pair of points in a subset is an integral power of two. It is necessary to consider each pair of points, not only adjacent. Note that any subset containing one element satisfies the condition above. Among all these subsets, choose a subset with maximum possible size.

In other words, you have to choose the maximum possible number of points xi1,xi2,…,xim such that for each pair xij, xik it is true that |xij−xik|=2d where d is some non-negative integer number (not necessarily the same for each pair of points).

Input
The first line contains one integer n (1≤n≤2⋅105) — the number of points.

The second line contains n pairwise distinct integers x1,x2,…,xn (−109≤xi≤109) — the coordinates of points.

Output
In the first line print m — the maximum possible number of points in a subset that satisfies the conditions described above.

In the second line print m integers — the coordinates of points in the subset you have chosen.

If there are multiple answers, print any of them.

Examples
Input
6
3 5 4 7 10 12
Output
3
7 3 5
Input
5
-1 2 5 8 11
Output
1
8
Note
In the first example the answer is [7,3,5]. Note, that |7−3|=4=22, |7−5|=2=21 and |3−5|=2=21. You can’t find a subset having more points satisfying the required property.
本题为选取最大子集集合,满足两两相减为2^n,这个题也是刚开始没思路,后来去网上搜,发现数字最多有三个,原因是

a−b=2^x①
a−c=2^y②
b−c=2^z③

由①+③得a−c=2^ x+ 2^ z =2^y
⇒x=z=y−1
同理四个数时有:2^ x +2 ^ y+2^ z=2^d,明显式子不成立,所以最多有三个。 有了这个条件后,这个题就变得简单多了,依次判断一个数,两个数,三个数的情况即可。

#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
#define inf 0x3f3f3f3f
#define N 200005
#define ll long long
int n,ans;
ll a[N],b[10],vis[66];
int main()
{
    scanf("%d",&n);
    vis[0]=1;
    for(int i=1;i<=63;i++)
    {
        vis[i]=vis[i-1]<<1;
    }
    for(int i=0;i<n;i++)
    {
        scanf("%lld",&a[i]);
    }
    sort(a,a+n);
    ans=1;
    b[0]=a[0];
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<=62;j++)
        {
            if(i+2<n && (*lower_bound(a,a+n,a[i]+vis[j]))==a[i]+vis[j] && (*lower_bound(a,a+n,a[i]+vis[j+1]))==a[i]+vis[j+1])
            {
                printf("3\n");
                printf("%lld %lld %lld\n",a[i],a[i]+vis[j],a[i]+vis[j+1]);
                return 0;
            }
            else if(i+1<n && (*lower_bound(a,a+n,a[i]+vis[j]))==a[i]+vis[j] && ans==1)
            {
                ans=2;
                b[0]=a[i];
                b[1]=a[i]+vis[j];
            }
        }
    }
    printf("%d\n",ans);
    for(int i=0;i<ans;i++)
    {
        if(i) printf(" ");
        printf("%lld",b[i]);
    }
    printf("\n");
	return 0;
}

ConneR and the A.R.C. Markland-N

A.R.C. Markland-N is a tall building with n floors numbered from 1 to n. Between each two adjacent floors in the building, there is a staircase connecting them.

It’s lunchtime for our sensei Colin “ConneR” Neumann Jr, and he’s planning for a location to enjoy his meal.

ConneR’s office is at floor s of the building. On each floor (including floor s, of course), there is a restaurant offering meals. However, due to renovations being in progress, k of the restaurants are currently closed, and as a result, ConneR can’t enjoy his lunch there.

CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.

Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns’ way!

Input
The first line contains one integer t (1≤t≤1000) — the number of test cases in the test. Then the descriptions of t test cases follow.

The first line of a test case contains three integers n, s and k (2≤n≤109, 1≤s≤n, 1≤k≤min(n−1,1000)) — respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants.

The second line of a test case contains k distinct integers a1,a2,…,ak (1≤ai≤n) — the floor numbers of the currently closed restaurants.

It is guaranteed that the sum of k over all test cases does not exceed 1000.

Output
For each test case print a single integer — the minimum number of staircases required for ConneR to walk from the floor s to a floor with an open restaurant.

Example
Input
5
5 2 3
1 2 3
4 3 3
4 1 2
10 2 6
1 2 3 4 5 7
2 1 1
2
100 76 8
76 75 36 67 41 74 10 77
Output
2
0
4
0
2
Note
In the first example test case, the nearest floor with an open restaurant would be the floor 4.

In the second example test case, the floor with ConneR’s office still has an open restaurant, so Sensei won’t have to go anywhere.

In the third example test case, the closest open restaurant is on the 6-th floor.
这个题比较简单,直接上代码

#include<iostream>
#include<cstdio>
#include<stdlib.h>
#include<algorithm>
#include<stack>
#include<map>
#include<queue>
#define INF 0x3f3f3f3f
using namespace std;
int t,n,s,k,i,j,ans,cnt,temp;
int main()
{
    cin>>t;
    while(t--)
    {
        map<int,int>mp;
        scanf("%d%d%d",&n,&s,&k);
        int x;
        for (i=1;i<=k;i++)
        {
            scanf("%d",&x);
            mp[x]=1;
        }
        for (i=0;i<=n;i++)
            if ((!mp[s-i]&&s-i>=1)||(!mp[s+i]&&s+i<=n))
            {
                printf("%d\n",i);
                break;
            }
    }
    return 0;
}

K - Relatively Prime Graph

Let’s call an undirected graph G=(V,E) relatively prime if and only if for each edge (v,u)∈E GCD(v,u)=1 (the greatest common divisor of v and u is 1). If there is no edge between some pair of vertices v and u then the value of GCD(v,u) doesn’t matter. The vertices are numbered from 1 to |V|.

Construct a relatively prime graph with n vertices and m edges such that it is connected and it contains neither self-loops nor multiple edges.

If there exists no valid graph with the given number of vertices and edges then output “Impossible”.

If there are multiple answers then print any of them.

Input
The only line contains two integers n and m (1≤n,m≤105) — the number of vertices and the number of edges.

Output
If there exists no valid graph with the given number of vertices and edges then output “Impossible”.

Otherwise print the answer in the following format:

The first line should contain the word “Possible”.

The i-th of the next m lines should contain the i-th edge (vi,ui) of the resulting graph (1≤vi,ui≤n,vi≠ui). For each pair (v,u) there can be no more pairs (v,u) or (u,v). The vertices are numbered from 1 to n.

If there are multiple answers then print any of them.

Examples
Input
5 6
Output
Possible
2 5
3 2
5 1
3 4
4 1
5 4
Input
6 12
Output
Impossible
Note
Here is the representation of the graph from the first example
在这里插入图片描述

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<bits/stdc++.h>
using namespace std;
const int N =100005;
const int INF = 0x3fffffff;
typedef long long LL;
using namespace std;
int n, m;
struct node
{
    int x, y;
    node () {}
    node (int x, int y): x(x), y(y) {}
} qu[N];
int gcd(int a,int b)
{
    return a%b==0?b:gcd(b,a%b);
}
int main()
{
    scanf("%d%d",&n,&m);
    if(n-1>m)
    {
        printf("Impossible\n");
        return 0;
    }
    int k=0,flag=0;
    for (int i=1;i<=n;i++)
    {
        for(int j=i+1;j<=n;j++)
        {
            if(gcd(j, i)==1)
                qu[k++]=node(i,j);
            if(k==m)
            {
                flag=1;
                break;
            }
        }
        if(flag)
            break;
    }
    if(flag)
    {
        printf("Possible\n");
        for(int i=0;i<k;i++)
            printf("%d %d\n",qu[i].x,qu[i].y);
    }
    else
        printf("Impossible\n");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_46434074/article/details/106286373