Educational Codeforces Round 89 (Rated for Div. 2)

题目

    • A. Shovels and Swords
    • B. Shuffle
    • C. Palindromic Paths

A. Shovels and Swords

Polycarp plays a well-known computer game (we won’t mention its name). In this game, he can craft tools of two types — shovels and swords. To craft a shovel, Polycarp spends two sticks and one diamond; to craft a sword, Polycarp spends two diamonds and one stick.
Each tool can be sold for exactly one emerald. How many emeralds can Polycarp earn, if he has a sticks and b diamonds?
Input
The first line contains one integer t (1≤t≤1000) — the number of test cases.
The only line of each test case contains two integers a and b (0≤a,b≤109) — the number of sticks and the number of diamonds, respectively.
Output
For each test case print one integer — the maximum number of emeralds Polycarp can earn.
Example
inputCopy
4
4 4
1000000000 0
7 15
8 7
outputCopy
2
0
7
5
Note
In the first test case Polycarp can earn two emeralds as follows: craft one sword and one shovel.
In the second test case Polycarp does not have any diamonds, so he cannot craft anything.
题意
给定一个a和一个b,已知两个a加一个b可以换取一个宝石,两个b加一个a也可以换取一个宝石,问最多可以换取多少宝石

int main()
{
	ll t,,x,y;
	cin>>t;
	while(t--)
	{
		cin>>x>>y;
		ll ans=(x+y)/3;
		cout<<min(ans,min(x,y))<<endl;
	}
	return 0;
}

B. Shuffle

You are given an array consisting of n integers a1, a2, …, an. Initially ax=1, all other elements are equal to 0.
You have to perform m operations. During the i-th operation, you choose two indices c and d such that li≤c,d≤ri, and swap ac and ad.
Calculate the number of indices k such that it is possible to choose the operations so that ak=1 in the end.
Input
The first line contains a single integer t (1≤t≤100) — the number of test cases. Then the description of t testcases follow.
The first line of each test case contains three integers n, x and m (1≤n≤109; 1≤m≤100; 1≤x≤n).
Each of next m lines contains the descriptions of the operations; the i-th line contains two integers li and ri (1≤li≤ri≤n).
Output
For each test case print one integer — the number of indices k such that it is possible to choose the operations so that ak=1 in the end.
Example
inputCopy
3
6 4 3
1 6
2 3
5 5
4 1 2
2 4
1 2
3 3 2
2 3
1 2
outputCopy
6
2
3
Note
In the first test case, it is possible to achieve ak=1 for every k. To do so, you may use the following operations:
swap ak and a4;
swap a2 and a2;
swap a5 and a5.
In the second test case, only k=1 and k=2 are possible answers. To achieve a1=1, you have to swap a1 and a1 during the second operation. To achieve a2=1, you have to swap a1 and a2 during the second operation.
题意
给定n,x,m,有一个包含n个数的数组a,初始时a[x]=1,在给出m组L和R,可以交换L到R中任意两个数,问最后可以有多少个位置的值可以是1
思路
只要x在L到R中,那么L到R的这些位置就都可以被交换变成1,所以只要把最开始的区间长度为x~x,然后更新这个区间

int main()
{
	int t,n,m,x,y,u,v,a,b;
	cin>>t;
	while(t--)
	{
		cin>>n>>x>>m;
		int l=x,r=x;
		int ans=0;
		while(m--)
		{
			cin>>a>>b;
			if(a<=l&&b>=r)
			{
				l=a;
				r=b;
			}
			else if(b>=l&&l>a)
			{
				l=a;
			}
			else if(a<=r&&r<b)
			{
				r=b;
			}
		}
		cout<<r-l+1<<endl;
	}
	return 0;
}

C. Palindromic Paths

You are given a matrix with n rows (numbered from 1 to n) and m columns (numbered from 1 to m). A number ai,j is written in the cell belonging to the i-th row and the j-th column, each number is either 0 or 1.
A chip is initially in the cell (1,1), and it will be moved to the cell (n,m). During each move, it either moves to the next cell in the current row, or in the current column (if the current cell is (x,y), then after the move it can be either (x+1,y) or (x,y+1)). The chip cannot leave the matrix.
Consider each path of the chip from (1,1) to (n,m). A path is called palindromic if the number in the first cell is equal to the number in the last cell, the number in the second cell is equal to the number in the second-to-last cell, and so on.
Your goal is to change the values in the minimum number of cells so that every path is palindromic.
Input
The first line contains one integer t (1≤t≤200) — the number of test cases.
The first line of each test case contains two integers n and m (2≤n,m≤30) — the dimensions of the matrix.
Then n lines follow, the i-th line contains m integers ai,1, ai,2, …, ai,m (0≤ai,j≤1).
Output
For each test case, print one integer — the minimum number of cells you have to change so that every path in the matrix is palindromic.
Example
inputCopy
4
2 2
1 1
0 1
2 3
1 1 0
1 0 0
3 7
1 0 1 1 1 1 1
0 0 0 0 0 0 0
1 1 1 1 1 0 1
3 5
1 0 1 0 0
1 1 1 1 0
0 0 1 0 0
outputCopy
0
3
4
4
Note
The resulting matrices in the first three test cases
题意
有一个n*m的01矩阵,每次从(1,1),(n,m),修改这个矩阵,要让所有的路径是一个回文路径,比如第一个有两条路,101和111,已经是回文了,所以不要修改
思路
因为每次只能向右或者向下走,所以路径是确定的,比如(1,1)对应(n,m),(1,2)、(2,1)对应(n-1,m),(n,m-1),可以得出(x,y),x+y从1+1开始,2对应n+m,3对应n+m-1,4对应n+m-2…,所以只要求出对应的两个里面0和1的最小值

int t,n,m,u,v;
	cin>>t;
	int a[40][40],f[62][3];
	while(t--)
	{
		cin>>n>>m;
		memset(f,0,sizeof(f));
		for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
		{
			if(a[i][j]==1)
				f[i+j][1]++;
			else
			 	f[i+j][0]++;
		}
		int ans=0,x=2,y=n+m;
		while(1)
		{
			if(x==y)break;
			ans=ans+min(f[x][0]+f[y][0],f[x][1]+f[y][1]);
			x++;y--;
			if(x>y)break;
		}
		cout<<ans<<endl;
	}

你可能感兴趣的:(codeforces)