codeforces 872 A(模拟)B(思维)C(思维)

本文解析了Codeforces平台上的三道经典算法题目,包括寻找最好数、求最大最小值及合数组成的最大数量,提供了清晰的解题思路与代码实现。

A. Search for Pretty Integers(模拟)

题目链接:codeforces 872B

题意:

     给两行数,求“最好数”,“最好数”是从第一行选一个数,从第二行选一个数组成的最小的数

题解:

   直接模拟

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
   int n, m;
   cin >> n >> m;
   vector<int> a(n);
   vector<int> b(m);
   for (int i = 0; i < n; ++i) {
      cin >> a[i];
   }
   for (int i = 0; i < m; ++i) {
      cin >> b[i];
   }
   int ans = 100500;
   for(int i = 0; i < n; ++i) {
      for(int j = 0; j < m; ++j) {
         if (a[i] == b[j]) {
            ans = min(ans, a[i]);
         }
	 else{
            ans = min(ans, a[i] * 10 + b[j]);
            ans = min(ans, b[j] * 10 + a[i]);
      	 }
      }
   }
   cout << ans << endl;
   return 0;
}

B. Maximum of Maximums of Minimums(思维)

题目链接:codeforces 872B

题意:

   给n个数,分成k个子序列,每个子序列都选出一个最小值,然后求这些最小值的最大值为多少。

题解:

   当k == 1时,无法再分解,答案就是数组中最小值

   当k == 2时,答案是 max(a[1], a[n]),    因为只能分为两个序列,如果a[1] > a[n] 那么将1和其他数分开,反之亦然

   当k >= 3时,答案就是数组中最大值,将最大值分为一个序列,然后将其他数分开就好

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 5;
int a[maxn];
int main(){
	int n, k, ans1 = 1e9+1, ans2 = -1e9-1;
	cin >> n >> k;
	for(int i = 1; i <= n; i++){
		cin >> a[i];
		ans1 = min(ans1, a[i]);
		ans2 = max(ans2, a[i]);
	}
	if(k == 1){
		cout << ans1 << endl;
	}
	else if(k == 2){
		cout << max(a[1], a[n]) << endl;
	}
	else{
		cout << ans2 << endl;
	}
	return 0;
} 

C. Maximum splitting(思维)

题目链接:codeforces 872C

题意:

    给一个数n,问n最多可以由多少个合数构成。

解题思路:

   3个基础合数为 4, 6, 9;(8是由两个4组成)

   n % 4 == 0,那么可以最多有 n / 4个合数    ,ans = n / 4;

   n % 4 == 1,那么需要两个4 去组成 9  ,ans = (n - 9) / 4 + 1;

  n % 4 == 2,那么需要1个4去组成 6,  ans = (n - 6) / 4 + 1;

   n % 4 == 3,那么需要3个4去组成 6和9,ans = (n - 15) / 4 + 2;

 

#include<bits/stdc++.h>
using namespace std;
int main(){
   int t;
   cin >> t;
   while(t--){
   	int n;
   	cin >> n;
   	if(n % 4 == 0){
   		cout << n / 4 << endl;
		}
		else if(n % 4 == 1){
			if(n < 9){
				cout << -1 << endl;
			}
			else{
				cout << (n - 9) / 4 + 1 << endl;
			}
		}
		else if(n % 4 == 2){
			if(n < 6){
				cout << -1 << endl;
			}
			else{
				cout << n / 4 << endl;
			}
		}
		else{
			if(n < 15){
				cout << -1 << endl;
			}
			else{
				cout << (n - 15) / 4 + 2 << endl;
			}
		}
	}
   return 0;
}

 

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值