Codeforces Round #643 (Div. 2)题目+详解+代码(A\B\C\D)

2023-11-07 06:08

本文主要是介绍Codeforces Round #643 (Div. 2)题目+详解+代码(A\B\C\D),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • A. Sequence with Digits
  • B. Young Explorers
  • C. Count Triangles
  • D. Game With Array

A. Sequence with Digits

来源:http://codeforces.com/contest/1355/problem/A

Let’s define the following recurrence:
an+1=an+minDigit(an)⋅maxDigit(an).
Here minDigit(x) and maxDigit(x) are the minimal and maximal digits in the decimal representation of x without leading zeroes. For examples refer to notes.

Your task is calculate aK for given a1 and K.

Input
The first line contains one integer t (1≤t≤1000) — the number of independent test cases.

Each test case consists of a single line containing two integers a1 and K (1≤a1≤1018, 1≤K≤1016) separated by a space.

Output
For each test case print one integer aK on a separate line.

Example
inputCopy
8
1 4
487 1
487 2
487 3
487 4
487 5
487 6
487 7
outputCopy
42
487
519
528
544
564
588
628
Note
a1=487

a2=a1+minDigit(a1)⋅maxDigit(a1)=487+min(4,8,7)⋅max(4,8,7)=487+4⋅8=519

a3=a2+minDigit(a2)⋅maxDigit(a2)=519+min(5,1,9)⋅max(5,1,9)=519+1⋅9=528

a4=a3+minDigit(a3)⋅maxDigit(a3)=528+min(5,2,8)⋅max(5,2,8)=528+2⋅8=544

a5=a4+minDigit(a4)⋅maxDigit(a4)=544+min(5,4,4)⋅max(5,4,4)=544+4⋅5=564

a6=a5+minDigit(a5)⋅maxDigit(a5)=564+min(5,6,4)⋅max(5,6,4)=564+4⋅6=588

a7=a6+minDigit(a6)⋅maxDigit(a6)=588+min(5,8,8)⋅max(5,8,8)=588+5⋅8=628
题意:
给定一个数NNN,定义操作op
op:
N=N+min(N的每一位数字)∗max(N的每一位数字)
思路:
可以发现,多次操作后N可能出现某一位出现0,所以,min(N的每一位数字)*max(N的每一位数字)为0,即:出现0后无论怎么操作N都不会改变
所以,执行K次模拟,当N出现0的时候break掉
代码:

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
typedef long long ll;
using namespace std;
ll zh = 1;
ll gg(ll n)
{ll minn = 10; ll maxx = 0;for (int i = 1;; i++){ll a = n % 10;n = n / 10;minn = min(minn, a);maxx = max(maxx, a);if (a == 0){zh = 0;break;}if (n <= 0) break;}return maxx * minn;
}
int main()
{int t; cin >> t;while (t--){zh = 1;ll a, k; cin >> a >> k;ll  yy = 0;for (ll i = 2; i <= k; i++){a += gg(a);if (zh == 0)break;}cout << a << endl;}
}

B. Young Explorers

来源:http://codeforces.com/contest/1355/problem/B

Young wilderness explorers set off to their first expedition led by senior explorer Russell. Explorers went into a forest, set up a camp and decided to split into groups to explore as much interesting locations as possible. Russell was trying to form groups, but ran into some difficulties…

Most of the young explorers are inexperienced, and sending them alone would be a mistake. Even Russell himself became senior explorer not long ago. Each of young explorers has a positive integer parameter ei — his inexperience. Russell decided that an explorer with inexperience e can only join the group of e or more people.

Now Russell needs to figure out how many groups he can organize. It’s not necessary to include every explorer in one of the groups: some can stay in the camp. Russell is worried about this expedition, so he asked you to help him.

Input
The first line contains the number of independent test cases T(1≤T≤2⋅105). Next 2T lines contain description of test cases.

The first line of description of each test case contains the number of young explorers N (1≤N≤2⋅105).

The second line contains N integers e1,e2,…,eN (1≤ei≤N), where ei is the inexperience of the i-th explorer.

It’s guaranteed that sum of all N doesn’t exceed 3⋅105.

Output
Print T numbers, each number on a separate line.

In i-th line print the maximum number of groups Russell can form in i-th test case.

Example
inputCopy
2
3
1 1 1
5
2 3 1 2 2
outputCopy
3
2
Note
In the first example we can organize three groups. There will be only one explorer in each group. It’s correct because inexperience of each explorer equals to 1, so it’s not less than the size of his group.

In the second example we can organize two groups. Explorers with inexperience 1, 2 and 3 will form the first group, and the other two explorers with inexperience equal to 2 will form the second group.

This solution is not unique. For example, we can form the first group using the three explorers with inexperience equal to 2, and the second group using only one explorer with inexperience equal to 1. In this case the young explorer with inexperience equal to 3 will not be included in any group.
题意:
每人都有一个组团人数,值为ei的人只能加入大于等于ei个人的团,求最多能组成多少个团.

思路:
就是一个贪心,先从小到大排序,然后积累人数看是否大于ei​ ,如果大于则人数重新积累,否则积累人数+1;
具体实现见代码

#include<iostream>
using namespace std;
int a[300010];
int main() {int t, n;cin >> t;while (t--) {int ans = 0;cin >> n;for (int i = 0; i < n; i++) cin >> a[i];sort(a, a + n);for (int i =0, cnt = 0; i < n; i++){if (++cnt >= a[i]) {cnt = 0;ans++;}}cout << ans << endl;}return 0;
}

C. Count Triangles

来源:http://codeforces.com/contest/1355/problem/C

Like any unknown mathematician, Yuri has favourite numbers: A, B, C, and D, where A≤B≤C≤D. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides x, y, and z exist, such that A≤x≤B≤y≤C≤z≤D holds?

Yuri is preparing problems for a new contest now, so he is very busy. That’s why he asked you to calculate the number of triangles with described property.

The triangle is called non-degenerate if and only if its vertices are not collinear.

Input
The first line contains four integers: A, B, C and D (1≤A≤B≤C≤D≤5⋅105) — Yuri’s favourite numbers.

Output
Print the number of non-degenerate triangles with integer sides x, y, and z such that the inequality A≤x≤B≤y≤C≤z≤D holds.

Examples
inputCopy
1 2 3 4
outputCopy
4
inputCopy
1 2 2 5
outputCopy
3
inputCopy
500000 500000 500000 500000
outputCopy
1
Note
In the first example Yuri can make up triangles with sides (1,3,3), (2,2,3), (2,3,3) and (2,3,4).

In the second example Yuri can make up triangles with sides (1,2,2), (2,2,2) and (2,2,3).

In the third example Yuri can make up only one equilateral triangle with sides equal to 5⋅105.

题意:
给定A、B、C、D
给定限定条件A<=x<=B<=y<=C<=z<=D
问有多少个(x,y,z)能组成三角形

思路:
题目即统计满足x+y<z的方案数
容易想到枚举一个,O(1)计算方案数,但是细节有点多,不好码
还有一种做法是枚举x+y,设当前枚举到的x+y为i
那么需要计算的有两个:
1.满足i<z的z的数量
2.满足x<=y且x+y=i的(x,y)的数量
考虑如何计算2:
如果x为A,那么y为i-A
如果x为A+1,那么y为i-A-1

如果x为B,那么y为i-B
容易观察到y的变化范围为[i-B,i-A]
不过y还需要在[B,C]之内,取区间交就行了
代码:

#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main(){int a,b,c,d;cin>>a>>b>>c>>d;int ans=0;for(int i=a+b;i<=b+c;i++){if(i>=c){int l=max(i-b,b),r=min(i-a,c);int cnt=min(i-c,d-c+1);ans+=cnt*(r-l+1);}}cout<<ans<<endl;return 0;
}

D. Game With Array

来源:http://codeforces.com/contest/1355/problem/D

Petya and Vasya are competing with each other in a new interesting game as they always do.

At the beginning of the game Petya has to come up with an array of N positive integers. Sum of all elements in his array should be equal to S. Then Petya has to select an integer K such that 0≤K≤S.

In order to win, Vasya has to find a non-empty subarray in Petya’s array such that the sum of all selected elements equals to either K or S−K. Otherwise Vasya loses.

You are given integers N and S. You should determine if Petya can win, considering Vasya plays optimally. If Petya can win, help him to do that.

Input
The first line contains two integers N and S (1≤N≤S≤106) — the required length of the array and the required sum of its elements.

Output
If Petya can win, print “YES” (without quotes) in the first line. Then print Petya’s array in the second line. The array should contain N positive integers with sum equal to S. In the third line print K. If there are many correct answers, you can print any of them.

If Petya can’t win, print “NO” (without quotes).

You can print each letter in any register (lowercase or uppercase).

Examples
inputCopy
1 4
outputCopy
YES
4
2
inputCopy
3 4
outputCopy
NO
inputCopy
3 8
outputCopy
YES
2 1 5
4

题意:
让你用n个正整数去构成和为S的数组(n,S将给出),问是否存在一个k(1 <= k <= S)使得这个数组中的任意子数组的和不为k,如果存在输出yes,并且输出这个数组和k,否则输出no。

思路:
我们先贪心一下,直接把数组的前n-1个数,赋值成1,然后最后一个数赋值成s-(n-1)。

1.先看这个数组的前n-1个数的子数组的和的范围。
很容易知道,范围为:1到n-1。
2.再看包含最后一个数的子数组的范围是多少。
范围为s-(n-1)到s。
3.再判断第一步的最大值(也就是n-1)是否大于或等于第二步的最小值-1。

实际上,就是最大化数组中的和的范围,然后再判断其中是否存在和不连续的情况

#include <bits/stdc++.h>
using namespace std;
int main()
{int n, s;cin >> n >> s;int Min = s - n + 1, Max = n - 1;if (Max >= Min - 1) cout << "NO" << endl;else{cout << "YES" << endl;for (int i = 1; i <= n - 1; i++) cout << 1 << " ";cout << Min << endl;cout << Max + 1 << endl;}
}

本人水平有限,如有不足之处,请指正

这篇关于Codeforces Round #643 (Div. 2)题目+详解+代码(A\B\C\D)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/361748

相关文章

Spring Security基于数据库验证流程详解

Spring Security 校验流程图 相关解释说明(认真看哦) AbstractAuthenticationProcessingFilter 抽象类 /*** 调用 #requiresAuthentication(HttpServletRequest, HttpServletResponse) 决定是否需要进行验证操作。* 如果需要验证,则会调用 #attemptAuthentica

OpenHarmony鸿蒙开发( Beta5.0)无感配网详解

1、简介 无感配网是指在设备联网过程中无需输入热点相关账号信息,即可快速实现设备配网,是一种兼顾高效性、可靠性和安全性的配网方式。 2、配网原理 2.1 通信原理 手机和智能设备之间的信息传递,利用特有的NAN协议实现。利用手机和智能设备之间的WiFi 感知订阅、发布能力,实现了数字管家应用和设备之间的发现。在完成设备间的认证和响应后,即可发送相关配网数据。同时还支持与常规Sof

活用c4d官方开发文档查询代码

当你问AI助手比如豆包,如何用python禁止掉xpresso标签时候,它会提示到 这时候要用到两个东西。https://developers.maxon.net/论坛搜索和开发文档 比如这里我就在官方找到正确的id描述 然后我就把参数标签换过来

poj 1258 Agri-Net(最小生成树模板代码)

感觉用这题来当模板更适合。 题意就是给你邻接矩阵求最小生成树啦。~ prim代码:效率很高。172k...0ms。 #include<stdio.h>#include<algorithm>using namespace std;const int MaxN = 101;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int n

6.1.数据结构-c/c++堆详解下篇(堆排序,TopK问题)

上篇:6.1.数据结构-c/c++模拟实现堆上篇(向下,上调整算法,建堆,增删数据)-CSDN博客 本章重点 1.使用堆来完成堆排序 2.使用堆解决TopK问题 目录 一.堆排序 1.1 思路 1.2 代码 1.3 简单测试 二.TopK问题 2.1 思路(求最小): 2.2 C语言代码(手写堆) 2.3 C++代码(使用优先级队列 priority_queue)

计算机毕业设计 大学志愿填报系统 Java+SpringBoot+Vue 前后端分离 文档报告 代码讲解 安装调试

🍊作者:计算机编程-吉哥 🍊简介:专业从事JavaWeb程序开发,微信小程序开发,定制化项目、 源码、代码讲解、文档撰写、ppt制作。做自己喜欢的事,生活就是快乐的。 🍊心愿:点赞 👍 收藏 ⭐评论 📝 🍅 文末获取源码联系 👇🏻 精彩专栏推荐订阅 👇🏻 不然下次找不到哟~Java毕业设计项目~热门选题推荐《1000套》 目录 1.技术选型 2.开发工具 3.功能

K8S(Kubernetes)开源的容器编排平台安装步骤详解

K8S(Kubernetes)是一个开源的容器编排平台,用于自动化部署、扩展和管理容器化应用程序。以下是K8S容器编排平台的安装步骤、使用方式及特点的概述: 安装步骤: 安装Docker:K8S需要基于Docker来运行容器化应用程序。首先要在所有节点上安装Docker引擎。 安装Kubernetes Master:在集群中选择一台主机作为Master节点,安装K8S的控制平面组件,如AP

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯:

Codeforces Round #261 (Div. 2)小记

A  XX注意最后输出满足条件,我也不知道为什么写的这么长。 #define X first#define Y secondvector<pair<int , int> > a ;int can(pair<int , int> c){return -1000 <= c.X && c.X <= 1000&& -1000 <= c.Y && c.Y <= 1000 ;}int m

Codeforces Beta Round #47 C凸包 (最终写法)

题意慢慢看。 typedef long long LL ;int cmp(double x){if(fabs(x) < 1e-8) return 0 ;return x > 0 ? 1 : -1 ;}struct point{double x , y ;point(){}point(double _x , double _y):x(_x) , y(_y){}point op