Educational Codeforces Round 131 - Div.2

Educational Codeforces Round 131 - Div.2

A

题意

有一个 \(2*2\) 的矩阵,\(0\)\(1\) 填入其中,你可以消除一列和一行的数,使他们从 \(1\) 都变为 \(0\) ,问最少多少次操作可以使他们都变为 \(0\)

Solution

一共两种可能。

  1. 不需要操作(及矩阵中没有 \(1\)
  2. \(1\) 次操作(及矩阵中有 \(1-3\)\(1\)
  3. \(2\) 次操作(及矩阵中全是 \(1\)
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;

int t;
int a[5];
int main()
{
	cin>>t;
	while(t--)
	{
		int x = 0;
		for(int i=1;i<=4;i++)
		{
			cin>>a[i];
			if(a[i]==1)	x++;
		}
		if(x==0)	cout<<'0'<<endl;
		else
		if(x==4)	cout<<'2'<<endl;
		else
			cout<<'1'<<endl;
			
	}
	return 0;
}

B

题意

有一个 \(1-n\) 的排序记为 \(p_i\) ,存在一个 \(d\) 使得 \(p_i \times d = p_{i+1}\) ,找出一个 \(d\) 使得存在该条件的数对尽可能的多,输出 \(d\) 并输出该排序。

Solution

TIPS :如 \(p = [5,2,6,7,1,3,4]\) 即为 \(n\)\(6\) 时的排序

发现,当此数对连续时,将是一个指数级别的增长,如 $ [1,2,4,8,...]$ ,那么,在 \(d=2\) 时是最优的,根据此规律进行构造。

注意:\(1\) 可以作为开头,\([1,2,4,3,6,5]\) 优于 \([1,2,4,3,5,6]\)

即从前往后找,找到一个未访问的数,依次 $ \times 2$ 向后即可。

#include <bits/stdc++.h>
#define mod 1000000007
#define int long long
using namespace std;
int vis[2000005];
signed main()
{
	int T;
	cin >> T;
	while(T--)
	{
		int n;
		cin >> n;
		cout << "2\n";
		for(int i=1;i<=n;i++)
			vis[i]=0;
		for(int i=1;i<=n;i++)
		{
			int x=i;
			while(x<=n&&!vis[x])
			{
				cout << x << " ";
				vis[x]=1;
				x*=2;
			}
		}
		cout << "\n";
	}
	return 0;
}
posted @ 2022-08-11 13:48  xlqs23  阅读(16)  评论(0编辑  收藏  举报