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

相关文章

Linux换行符的使用方法详解

《Linux换行符的使用方法详解》本文介绍了Linux中常用的换行符LF及其在文件中的表示,展示了如何使用sed命令替换换行符,并列举了与换行符处理相关的Linux命令,通过代码讲解的非常详细,需要的... 目录简介检测文件中的换行符使用 cat -A 查看换行符使用 od -c 检查字符换行符格式转换将

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La

使用C#代码在PDF文档中添加、删除和替换图片

《使用C#代码在PDF文档中添加、删除和替换图片》在当今数字化文档处理场景中,动态操作PDF文档中的图像已成为企业级应用开发的核心需求之一,本文将介绍如何在.NET平台使用C#代码在PDF文档中添加、... 目录引言用C#添加图片到PDF文档用C#删除PDF文档中的图片用C#替换PDF文档中的图片引言在当

详解C#如何提取PDF文档中的图片

《详解C#如何提取PDF文档中的图片》提取图片可以将这些图像资源进行单独保存,方便后续在不同的项目中使用,下面我们就来看看如何使用C#通过代码从PDF文档中提取图片吧... 当 PDF 文件中包含有价值的图片,如艺术画作、设计素材、报告图表等,提取图片可以将这些图像资源进行单独保存,方便后续在不同的项目中使

C#使用SQLite进行大数据量高效处理的代码示例

《C#使用SQLite进行大数据量高效处理的代码示例》在软件开发中,高效处理大数据量是一个常见且具有挑战性的任务,SQLite因其零配置、嵌入式、跨平台的特性,成为许多开发者的首选数据库,本文将深入探... 目录前言准备工作数据实体核心技术批量插入:从乌龟到猎豹的蜕变分页查询:加载百万数据异步处理:拒绝界面

Android中Dialog的使用详解

《Android中Dialog的使用详解》Dialog(对话框)是Android中常用的UI组件,用于临时显示重要信息或获取用户输入,本文给大家介绍Android中Dialog的使用,感兴趣的朋友一起... 目录android中Dialog的使用详解1. 基本Dialog类型1.1 AlertDialog(

用js控制视频播放进度基本示例代码

《用js控制视频播放进度基本示例代码》写前端的时候,很多的时候是需要支持要网页视频播放的功能,下面这篇文章主要给大家介绍了关于用js控制视频播放进度的相关资料,文中通过代码介绍的非常详细,需要的朋友可... 目录前言html部分:JavaScript部分:注意:总结前言在javascript中控制视频播放

C#数据结构之字符串(string)详解

《C#数据结构之字符串(string)详解》:本文主要介绍C#数据结构之字符串(string),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录转义字符序列字符串的创建字符串的声明null字符串与空字符串重复单字符字符串的构造字符串的属性和常用方法属性常用方法总结摘

Java中StopWatch的使用示例详解

《Java中StopWatch的使用示例详解》stopWatch是org.springframework.util包下的一个工具类,使用它可直观的输出代码执行耗时,以及执行时间百分比,这篇文章主要介绍... 目录stopWatch 是org.springframework.util 包下的一个工具类,使用它

Java进行文件格式校验的方案详解

《Java进行文件格式校验的方案详解》这篇文章主要为大家详细介绍了Java中进行文件格式校验的相关方案,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、背景异常现象原因排查用户的无心之过二、解决方案Magandroidic Number判断主流检测库对比Tika的使用区分zip