0
点赞
收藏
分享

微信扫一扫

Codeforces Round #575 (Div. 3)

千白莫 2023-05-19 阅读 114


Problems

Codeforces Round #575 (Div. 3)_ide

 

 

#

Name

 

 

A

Three Piles of Candies

standard input/output

Codeforces Round #575 (Div. 3)_ide_02

 


Codeforces Round #575 (Div. 3)_ci_03

Codeforces Round #575 (Div. 3)_ide_04

 x7785


B

Odd Sum Segments

standard input/output

Codeforces Round #575 (Div. 3)_ide_02

 


Codeforces Round #575 (Div. 3)_ci_03

Codeforces Round #575 (Div. 3)_ide_04

 x4630


C

Robot Breakout

standard input/output

Codeforces Round #575 (Div. 3)_ide_02

 


Codeforces Round #575 (Div. 3)_ci_03

Codeforces Round #575 (Div. 3)_ide_04

 x2633


D1

RGB Substring (easy version)

standard input/output

Codeforces Round #575 (Div. 3)_ide_02

 


Codeforces Round #575 (Div. 3)_ci_03

Codeforces Round #575 (Div. 3)_ide_04

 x2725


D2

RGB Substring (hard version)

standard input/output

Codeforces Round #575 (Div. 3)_ide_02

 


Codeforces Round #575 (Div. 3)_ci_03

Codeforces Round #575 (Div. 3)_ide_04

 x1677


E

Connected Component on a Chessboard

standard input/output

Codeforces Round #575 (Div. 3)_ide_02

 


Codeforces Round #575 (Div. 3)_ci_03

Codeforces Round #575 (Div. 3)_ide_04

 x630


F

K-th Path

standard input/output

Codeforces Round #575 (Div. 3)_ide_02

 


Codeforces Round #575 (Div. 3)_ci_03

Codeforces Round #575 (Div. 3)_ide_04

 x143


 

 

 

B. Odd Sum Segments

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given an array aa consisting of nn integers a1,a2,…,ana1,a2,…,an. You want to split it into exactly kk non-empty non-intersecting subsegments such that each subsegment has odd sum (i. e. for each subsegment, the sum of all elements that belong to this subsegment is odd). It is impossible to rearrange (shuffle) the elements of a given array. Each of the nn elements of the array aa must belong to exactly one of the kk subsegments.

Let's see some examples of dividing the array of length 55 into 33 subsegments (not necessarily with odd sums): [1,2,3,4,5][1,2,3,4,5] is the initial array, then all possible ways to divide it into 33 non-empty non-intersecting subsegments are described below:

  • [1],[2],[3,4,5][1],[2],[3,4,5];
  • [1],[2,3],[4,5][1],[2,3],[4,5];
  • [1],[2,3,4],[5][1],[2,3,4],[5];
  • [1,2],[3],[4,5][1,2],[3],[4,5];
  • [1,2],[3,4],[5][1,2],[3,4],[5];
  • [1,2,3],[4],[5][1,2,3],[4],[5].

Of course, it can be impossible to divide the initial array into exactly kk subsegments in such a way that each of them will have odd sum of elements. In this case print "NO". Otherwise, print "YES" and any possible division of the array. See the output format for the detailed explanation.

You have to answer qq independent queries.

Input

The first line contains one integer qq (1≤q≤2⋅1051≤q≤2⋅105) — the number of queries. Then qq queries follow.

The first line of the query contains two integers nn and kk (1≤k≤n≤2⋅1051≤k≤n≤2⋅105) — the number of elements in the array and the number of subsegments, respectively.

The second line of the query contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109), where aiai is the ii-th element of aa.

It is guaranteed that the sum of nn over all queries does not exceed 2⋅1052⋅105 (∑n≤2⋅105∑n≤2⋅105).

Output

For each query, print the answer to it. If it is impossible to divide the initial array into exactly kk subsegments in such a way that each of them will have odd sum of elements, print "NO" in the first line. Otherwise, print "YES" in the first line and any possible division of the array in the second line. The division can be represented as kk integers r1r1, r2r2, ..., rkrk such that 1≤r1<r2<⋯<rk=n1≤r1<r2<⋯<rk=n, where rjrj is the right border of the jj-th segment (the index of the last element that belongs to the jj-th segment), so the array is divided into subsegments [1;r1],[r1+1;r2],[r2+1,r3],…,[rk−1+1,n][1;r1],[r1+1;r2],[r2+1,r3],…,[rk−1+1,n]. Note that rkrk is always nn but you should print it anyway.

Example

input

Copy

3
5 3
7 18 3 14 1
5 4
1 2 3 4 5
6 2
1 2 8 4 10 2

output

Copy

YES
1 3 5
NO
NO

 

题意:

n个数,划分成k段,每一段的和为奇数,问是否存在方案?

分析:

奇+奇=偶

奇+偶=奇

偶+偶=偶

假设有m个奇数

所以我们先保证有m>=k,则我们先选出k-1个奇数,剩下的奇数个数m-k+1必须是奇数个,所有偶数可以看成一个偶数就行。

输出方案注意最后的n和特判1.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define N 500005
ll a[N];
ll n,m;
ll sum[N];
ll ans[N];
int main()
{
 
 
    int T;
    scanf("%lld",&T);
    while(T--)
    {
        ll sum=0;
 
        scanf("%lld%lld",&n,&m);
        for(int i=1; i<=n; i++)
        {
            scanf("%lld",&a[i]);
            if(a[i]%2==1)
                sum++;
        }
 
        if(sum<m)
        {
            printf("NO\n");
            continue;
        }
        else
        {
            if((sum-m+1)%2==0)
            {
                printf("NO\n");
                continue;
            }
            else
            {
                int cnt=0;
                printf("YES\n");
                if(m==1)
				{
					printf("%lld\n",n);   
					continue;
				}
                sum=0;
 
                for(int i=1; i<=n; i++)
                {
                    sum+=a[i];
                    if(sum%2==1)
                    {
                        ans[cnt++]=i;
                        if(cnt>=m-1)
                            break;
                        sum=0;
                    }
                }
                
                
                
                for(int i=0; i<m-1; i++)
                {
                    printf("%lld ",ans[i]);
                }
                printf("%lld\n",n);  
            }
 
        }
    }
 
 
}

C. Robot Breakout

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

nn robots have escaped from your laboratory! You have to find them as soon as possible, because these robots are experimental, and their behavior is not tested yet, so they may be really dangerous!

Fortunately, even though your robots have escaped, you still have some control over them. First of all, you know the location of each robot: the world you live in can be modeled as an infinite coordinate plane, and the ii-th robot is currently located at the point having coordinates (xixi, yiyi). Furthermore, you may send exactly one command to all of the robots. The command should contain two integer numbers XX and YY, and when each robot receives this command, it starts moving towards the point having coordinates (XX, YY). The robot stops its movement in two cases:

  • either it reaches (XX, YY);
  • or it cannot get any closer to (XX, YY).

Normally, all robots should be able to get from any point of the coordinate plane to any other point. Each robot usually can perform four actions to move. Let's denote the current coordinates of the robot as (xcxc, ycyc). Then the movement system allows it to move to any of the four adjacent points:

  1. the first action allows it to move from (xcxc, ycyc) to (xc−1xc−1, ycyc);
  2. the second action allows it to move from (xcxc, ycyc) to (xcxc, yc+1yc+1);
  3. the third action allows it to move from (xcxc, ycyc) to (xc+1xc+1, ycyc);
  4. the fourth action allows it to move from (xcxc, ycyc) to (xcxc, yc−1yc−1).

Unfortunately, it seems that some movement systems of some robots are malfunctioning. For each robot you know which actions it can perform, and which it cannot perform.

You want to send a command so all robots gather at the same point. To do so, you have to choose a pair of integer numbers XX and YY so that each robot can reach the point (XX, YY). Is it possible to find such a point?

Input

The first line contains one integer qq (1≤q≤1051≤q≤105) — the number of queries.

Then qq queries follow. Each query begins with one line containing one integer nn (1≤n≤1051≤n≤105) — the number of robots in the query. Then nn lines follow, the ii-th of these lines describes the ii-th robot in the current query: it contains six integer numbers xixi, yiyi, fi,1fi,1, fi,2fi,2, fi,3fi,3 and fi,4fi,4 (−105≤xi,yi≤105−105≤xi,yi≤105, 0≤fi,j≤10≤fi,j≤1). The first two numbers describe the initial location of the ii-th robot, and the following four numbers describe which actions the ii-th robot can use to move (fi,j=1fi,j=1 if the ii-th robot can use the jj-th action, and fi,j=0fi,j=0 if it cannot use the jj-th action).

It is guaranteed that the total number of robots over all queries does not exceed 105105.

Output

You should answer each query independently, in the order these queries appear in the input.

To answer a query, you should do one of the following:

  • if it is impossible to find a point that is reachable by all nn robots, print one number 00 on a separate line;
  • if it is possible to find a point that is reachable by all nn robots, print three space-separated integers on the same line: 11 XX YY, where XXand YY are the coordinates of the point reachable by all nn robots. Both XX and YY should not exceed 105105 by absolute value; it is guaranteed that if there exists at least one point reachable by all robots, then at least one of such points has both coordinates not exceeding 105105 by absolute value.

Example

input

Copy

4
2
-1 -2 0 0 0 0
-1 -2 0 0 0 0
3
1 5 1 1 1 1
2 5 0 1 0 1
3 5 1 0 0 0
2
1337 1337 0 1 1 1
1336 1337 1 1 0 1
1
3 5 1 1 1 1

output

Copy

1 -1 -2
1 2 5
0
1 -100000 -100000

 

题意:

n个坐标,有上下左右的不能走的限制条件,问是否能到一个点上?

分析:

对于每一个坐标,求出的他的上下左右的边界来,保存所有的就行。如果最后构不成矩形,NO。

 

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define INF 0x3f3f3f3f
const int MAXC = 1e5;

int main() {
    int q;
    cin >> q;
    while (q--) {
        int n;
        cin >> n;
        int mnx = -MAXC, mxx = MAXC;
        int mny = -MAXC, mxy = MAXC;
        while (n--) {
            int x, y, f1, f2, f3, f4;
            cin >> x >> y >> f1 >> f2 >> f3 >> f4;
            if (!f1) mnx = max(mnx, x);
            if (!f2) mxy = min(mxy, y);
            if (!f3) mxx = min(mxx, x);
            if (!f4) mny = max(mny, y);
        }
        if (mnx <= mxx && mny <= mxy)
            cout << "1 " << mnx << " " << mny << "\n";
        else
            cout << "0\n";
    }

    return 0;
}

 

D. RGB Substring (hard version)

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

The only difference between easy and hard versions is the size of the input.

You are given a string ss consisting of nn characters, each character is 'R', 'G' or 'B'.

You are also given an integer kk. Your task is to change the minimum number of characters in the initial string ss so that after the changes there will be a string of length kk that is a substring of ss, and is also a substring of the infinite string "RGBRGBRGB ...".

A string aa is a substring of string bb if there exists a positive integer ii such that a1=bia1=bi, a2=bi+1a2=bi+1, a3=bi+2a3=bi+2, ..., a|a|=bi+|a|−1a|a|=bi+|a|−1. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not.

You have to answer qq independent queries.

Input

The first line of the input contains one integer qq (1≤q≤2⋅1051≤q≤2⋅105) — the number of queries. Then qq queries follow.

The first line of the query contains two integers nn and kk (1≤k≤n≤2⋅1051≤k≤n≤2⋅105) — the length of the string ss and the length of the substring.

The second line of the query contains a string ss consisting of nn characters 'R', 'G' and 'B'.

It is guaranteed that the sum of nn over all queries does not exceed 2⋅1052⋅105 (∑n≤2⋅105∑n≤2⋅105).

Output

For each query print one integer — the minimum number of characters you need to change in the initial string ss so that after changing there will be a substring of length kk in ss that is also a substring of the infinite string "RGBRGBRGB ...".

Example

input

Copy

3
5 2
BGGGG
5 3
RBRGR
5 5
BBBRR

output

Copy

1
0
3

Note

In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB".

In the second example, the substring is "BRG".

题意:

给你一个字符串s和一个标准串RGBRGBRGB……,s截取长度为k个子串str,将其子串str修改为标准串的子串代价的最小值?

分析:

前缀和的思想。

举个例子就明白了:

字符:BGGGGG

标准:BRGBRG

代价:01 0 11 0

循环对于修改长度为k子串: sum[i]-sum[i-k]

然后标准换为:RGBRGB、GBRGBR都循环一遍即可。

 

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define INF 0x3f3f3f3f
#define N 500005
char str[N];
char t[N];
int a[N],sum[N];
int main()
{
    int q;
    scanf("%d",&q);
    while(q--)
    {
    	
        int n,k;
        scanf("%d%d",&n,&k);
        scanf("%s",str+1);

        for(int i=1;i<=n+100;i+=3)
		{
			t[i]='R';
			t[i+1]='G';
			t[i+2]='B';
		}
		int ans=1e18;
		for(int j=1;j<=3;j++)
		{
			for(int i=1;i<=n;i++)
			{
				if(t[i+j-1]!=str[i])
					a[i]=1;
				else
					a[i]=0;
			}
			for(int i=1;i<=n;i++)
			{
				sum[i]=sum[i-1]+a[i];
			}

			for(int i=k;i<=n;i++)
			{
				ans=min(ans,sum[i]-sum[i-k]);
			}
		}
		cout<<ans<<endl;
		
		
    }
    return 0;
}

 

E. Connected Component on a Chessboard

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given two integers bb and ww. You have a chessboard of size 109×109109×109 with the top left cell at (1;1)(1;1), the cell (1;1)(1;1) is painted white.

Your task is to find a connected component on this chessboard that contains exactly bb black cells and exactly ww white cells. Two cells are called connected if they share a side (i.e. for the cell (x,y)(x,y) there are at most four connected cells: (x−1,y),(x+1,y),(x,y−1),(x,y+1)(x−1,y),(x+1,y),(x,y−1),(x,y+1)). A set of cells is called a connected component if for every pair of cells C1C1 and C2C2 from this set, there exists a sequence of cells c1c1, c2c2, ..., ckck such that c1=C1c1=C1, ck=C2ck=C2, all cici from 11 to kk are belong to this set of cells and for every i∈[1,k−1]i∈[1,k−1], cells cici and ci+1ci+1 are connected.

Obviously, it can be impossible to find such component. In this case print "NO". Otherwise, print "YES" and any suitable connected component.

You have to answer qq independent queries.

Input

The first line of the input contains one integer qq (1≤q≤1051≤q≤105) — the number of queries. Then qq queries follow.

The only line of the query contains two integers bb and ww (1≤b,w≤1051≤b,w≤105) — the number of black cells required and the number of white cells required.

It is guaranteed that the sum of numbers of cells does not exceed 2⋅1052⋅105 (∑w+∑b≤2⋅105∑w+∑b≤2⋅105).

Output

For each query, print the answer to it.

If it is impossible to find the required component, print "NO" on the first line.

Otherwise, print "YES" on the first line. In the next b+wb+w lines print coordinates of cells of your component in any order. There should be exactly bb black cells and ww white cells in your answer. The printed component should be connected.

If there are several answers, you can print any. All coordinates in the answer should be in the range [1;109][1;109].

Example

input

Copy

3
1 1
1 4
2 5

output

Copy

YES


2 2
1 2
YES
2 3
1 3
3 3
2 2
2 4
YES
2 3
2 4
2 5
1 3
1 5
3 3
3 5

 

题意:

q次询问,一个棋盘,要求n个黑色,m个白色,问这个n+m块能否连成一个连通块,输出坐标。

分析:

 

 

Codeforces Round #575 (Div. 3)_i++_23

图中红线是最优方案,max(m,n)>3*min(m,n)+1输出NO

 

 

 

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define INF 0x3f3f3f3f
#define N 500005
int main()
{
    int q;
    scanf("%d",&q);
    while(q--)
    {

        int n,m;
        scanf("%d%d",&n,&m);
        if(max(m,n)>3*min(m,n)+1)
        {
            printf("NO\n");
        }
        else
        {
            printf("YES\n");
            int x=2,y=3;
            if(n>m)
            {
                y--;
            }
            int minn=min(n,m);
            int maxx=max(n,m);
            
            int x1=x,y1=y;
            for(int i=1; i<=minn; i++)
            {
                cout<<x1<<" "<<y1<<endl;
                y1+=2;
            }
            x1=x;
            y1=y-1;
            for(int i=1; i<=minn; i++)
            {
                cout<<x1<<" "<<y1<<endl;
                y1+=2;
            }
            maxx-=minn;

            if(maxx>0)
            {
                cout<<x1<<" "<<y1<<endl;
                maxx-=1;
                if(maxx>0)
                {
                    y1=y;
                    for(int i=1; i<=maxx/2; i++)
                    {
                        cout<<x+1<<" "<<y1<<endl;
                        cout<<x-1<<" "<<y1<<endl;
                        y1+=2;
                    }
                    if(maxx%2==1)
                    {
                        cout<<x+1<<" "<<y1<<endl;
                    }

                }

            }



        }

    }
    return 0;
}

F. K-th Path

time limit per test

2.5 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a connected undirected weighted graph consisting of nn vertices and mm edges.

You need to print the kk-th smallest shortest path in this graph (paths from the vertex to itself are not counted, paths from ii to jj and from jjto ii are counted as one).

More formally, if dd is the matrix of shortest paths, where di,jdi,j is the length of the shortest path between vertices ii and jj (1≤i<j≤n1≤i<j≤n), then you need to print the kk-th element in the sorted array consisting of all di,jdi,j, where 1≤i<j≤n1≤i<j≤n.

Input

The first line of the input contains three integers n,mn,m and kk (2≤n≤2⋅1052≤n≤2⋅105, n−1≤m≤min(n(n−1)2,2⋅105)n−1≤m≤min(n(n−1)2,2⋅105), 1≤k≤min(n(n−1)2,400)1≤k≤min(n(n−1)2,400) — the number of vertices in the graph, the number of edges in the graph and the value of kk, correspondingly.

Then mm lines follow, each containing three integers xx, yy and ww (1≤x,y≤n1≤x,y≤n, 1≤w≤1091≤w≤109, x≠yx≠y) denoting an edge between vertices xx and yy of weight ww.

It is guaranteed that the given graph is connected (there is a path between any pair of vertices), there are no self-loops (edges connecting the vertex with itself) and multiple edges (for each pair of vertices xx and yy, there is at most one edge between this pair of vertices in the graph).

Output

Print one integer — the length of the kk-th smallest shortest path in the given graph (paths from the vertex to itself are not counted, paths from ii to jj and from jj to ii are counted as one).

Examples

input

Copy

6 10 5
2 5 1
5 3 9
6 2 2
1 3 1
5 1 8
6 5 10
1 6 5
6 4 6
3 6 2
3 4 5

output

Copy

3

input

Copy

7 15 18
2 6 3
5 7 4
6 5 4
3 6 9
6 7 7
1 6 4
7 1 6
7 2 1
4 3 2
3 2 8
5 3 6
2 5 5
3 7 9
4 1 8
2 1 1

output

Copy

9

 

题意:

求无向图中所以路径的第k短路径?

分析:

我们首先先对所有的边排序,选前min(k,m)个,因为我们要求第k小的,肯定是在这前k小的边组合产生的,然后刷选出来点,选出来的点可能太大,离散化一下,跑floyed即可。

 

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
#define N 500005
struct Node
{
    ll index;
    ll u,v,w;

} arc;
bool cmp(Node x,Node y)
{
	return x.w<y.w;
}
vector<Node>e;
vector<ll>vert;
ll dist[1010][1010];
int main()
{
    ll n,m,k;
    scanf("%lld%lld%lld",&n,&m,&k);
    for(int i=1; i<=m; i++)
    {
        ll a,b,c;
        scanf("%lld%lld%lld",&a,&b,&c);
      //  a--;b--;
        arc.u=a;
        arc.v=b;
        arc.w=c;
        e.push_back(arc);
    }

    sort(e.begin(),e.end(),cmp);

    for(int i=0; i<min(m,k); i++)
    {
        vert.push_back(e[i].u);
        vert.push_back(e[i].v);
    }

    sort(vert.begin(),vert.end());
    
    vert.resize(unique(vert.begin(), vert.end()) - vert.begin());
    

    int cntv = vert.size();
    
    memset(dist,0x3f,sizeof(dist));
    
    for(int i=0; i<=cntv; i++)
    {
        dist[i][i]=0;
    }
    

    for (int i = 0; i < min(m, k); ++i)
    {
        int x = lower_bound(vert.begin(), vert.end(), e[i].u) - vert.begin();
        
        int y = lower_bound(vert.begin(), vert.end(), e[i].v) - vert.begin();
        
     //   cout<<x<<" "<<y<<"    "<<e[i].w<<endl;
        dist[x][y] = dist[y][x] = min(dist[x][y], e[i].w);
    }

    for (int z = 0; z <cntv; ++z)
    {
        for (int x = 0; x <cntv; ++x)
        {
            for (int y = 0; y <cntv; ++y)
            {
                dist[x][y] = min(dist[x][y], dist[x][z] + dist[z][y]);
            }
        }
    }
    
    vector<ll> res;
    for (int i = 0; i <= cntv; ++i)
    {
        for (int j = 0; j < i; ++j)
        {
            res.push_back(dist[i][j]);
        }
    }

    sort(res.begin(), res.end());
    cout << res[k - 1] << endl;

    return 0;
}

 

 

举报

相关推荐

0 条评论