Codeforces Round 854 by cybercats (Div. 1 + Div. 2)

A.

 

思路:    在给出的p序列中找不同的数字,如果出现了相同的数字则不需要删除原数组中的数字。

代码:

#include <bits/stdc++.h>
using namespace std;
#define int long long
int a[200010];

void solve()
{
	int n,m;
	cin>>n>>m;
	
	for (int i=1;i<=n;i++){
		a[i]=-1;//先全部假定不删除
	}
	
	map<int,int> mp;//用来标记是否需要删除。
	int cnt=n;
	for (int i=1;i<=m;i++){
		int x;
		cin>>x;
		if(cnt==0) {
			continue;当原数组中的全部数已全部删除时,可以跳过剩余的步骤,防止数组越界。
		}
		if(mp[x]==0){
			a[cnt]=i;
			cnt--;
			
		}
		mp[x]++;
	}
	for (int i=1;i<=n;i++) {
		cout<<a[i]<<" ";
		
	}
	cout<<endl;
}
signed main()
{
	int t;
	cin>>t;
	
	while(t--){
		solve();
	}
	
}

B。

思路:学习了jiangly 大佬的思路。

1.如果全部相同,则不需要操作。

2.如果数组中出现了1,则无解,因为1无法改变也无法被改变。

3.在数组中凑2,  

4.把其他数依次和2进行操作。

代码:
 

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

using PII = pair<int, int>;

const int N = 110;

int n;
int a[N];
PII ans[N * 35];
int cnt;

void solve() {
	cnt = 0;
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[i];
	
	if (count(a + 1, a + n + 1, a[1]) == n) {
		cout << 0 << '\n';
		return;
	}
	
	if (count(a + 1, a + n + 1, 1) >= 1) {
		cout << -1 << '\n';
		return;
	}//不能有1.
	
	while (count(a + 1, a + n + 1, a[1]) < n && count(a + 1, a + n + 1, 2) == 0) {
		int pos = 2;
		while (a[pos] == a[1]) {
			pos++;
		}
		
		if (a[pos] > a[1]) {
			if (a[pos] % a[1]) a[pos] = a[pos] / a[1] + 1;
			else a[pos] = a[pos] / a[1];
			ans[++cnt] = {pos, 1};
		}
		else {
			if (a[1] % a[pos]) a[1] = a[1] / a[pos] + 1;
			else a[1] = a[1] / a[pos];
			ans[++cnt] = {1, pos};
		}
	}//构造2。
	
	if (count(a + 1, a + n + 1, a[1]) < n) {
		int pos2 = find(a + 1, a + n + 1, 2) - a;//2的位置。
		for (int i = 1; i <= n; i++) {
			while (a[i] != 2) {
				if (a[i] & 1) a[i] = a[i] / 2 + 1;
				else a[i] = a[i] / 2;
				ans[++cnt] = {i, pos2};
			}
		}
	}//
	
	cout << cnt << '\n';
	for (int i = 1; i <= cnt; i++) cout << ans[i].first << ' ' << ans[i].second << '\n';
	
	

}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int T;
	cin >> T;
	while (T--) solve();
	return 0;
}

注意:

本题使用了count 函数,作用是在a+1,到a+n+1中统计,某个数的数量。
 

  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值