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

相关文章

Java内存分配与JVM参数详解(推荐)

《Java内存分配与JVM参数详解(推荐)》本文详解JVM内存结构与参数调整,涵盖堆分代、元空间、GC选择及优化策略,帮助开发者提升性能、避免内存泄漏,本文给大家介绍Java内存分配与JVM参数详解,... 目录引言JVM内存结构JVM参数概述堆内存分配年轻代与老年代调整堆内存大小调整年轻代与老年代比例元空

Python中注释使用方法举例详解

《Python中注释使用方法举例详解》在Python编程语言中注释是必不可少的一部分,它有助于提高代码的可读性和维护性,:本文主要介绍Python中注释使用方法的相关资料,需要的朋友可以参考下... 目录一、前言二、什么是注释?示例:三、单行注释语法:以 China编程# 开头,后面的内容为注释内容示例:示例:四

mysql表操作与查询功能详解

《mysql表操作与查询功能详解》本文系统讲解MySQL表操作与查询,涵盖创建、修改、复制表语法,基本查询结构及WHERE、GROUPBY等子句,本文结合实例代码给大家介绍的非常详细,感兴趣的朋友跟随... 目录01.表的操作1.1表操作概览1.2创建表1.3修改表1.4复制表02.基本查询操作2.1 SE

MySQL中的锁机制详解之全局锁,表级锁,行级锁

《MySQL中的锁机制详解之全局锁,表级锁,行级锁》MySQL锁机制通过全局、表级、行级锁控制并发,保障数据一致性与隔离性,全局锁适用于全库备份,表级锁适合读多写少场景,行级锁(InnoDB)实现高并... 目录一、锁机制基础:从并发问题到锁分类1.1 并发访问的三大问题1.2 锁的核心作用1.3 锁粒度分

MySQL数据库中ENUM的用法是什么详解

《MySQL数据库中ENUM的用法是什么详解》ENUM是一个字符串对象,用于指定一组预定义的值,并可在创建表时使用,下面:本文主要介绍MySQL数据库中ENUM的用法是什么的相关资料,文中通过代码... 目录mysql 中 ENUM 的用法一、ENUM 的定义与语法二、ENUM 的特点三、ENUM 的用法1

MySQL count()聚合函数详解

《MySQLcount()聚合函数详解》MySQL中的COUNT()函数,它是SQL中最常用的聚合函数之一,用于计算表中符合特定条件的行数,本文给大家介绍MySQLcount()聚合函数,感兴趣的朋... 目录核心功能语法形式重要特性与行为如何选择使用哪种形式?总结深入剖析一下 mysql 中的 COUNT

一文详解Git中分支本地和远程删除的方法

《一文详解Git中分支本地和远程删除的方法》在使用Git进行版本控制的过程中,我们会创建多个分支来进行不同功能的开发,这就容易涉及到如何正确地删除本地分支和远程分支,下面我们就来看看相关的实现方法吧... 目录技术背景实现步骤删除本地分支删除远程www.chinasem.cn分支同步删除信息到其他机器示例步骤

Java中调用数据库存储过程的示例代码

《Java中调用数据库存储过程的示例代码》本文介绍Java通过JDBC调用数据库存储过程的方法,涵盖参数类型、执行步骤及数据库差异,需注意异常处理与资源管理,以优化性能并实现复杂业务逻辑,感兴趣的朋友... 目录一、存储过程概述二、Java调用存储过程的基本javascript步骤三、Java调用存储过程示

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

Go语言数据库编程GORM 的基本使用详解

《Go语言数据库编程GORM的基本使用详解》GORM是Go语言流行的ORM框架,封装database/sql,支持自动迁移、关联、事务等,提供CRUD、条件查询、钩子函数、日志等功能,简化数据库操作... 目录一、安装与初始化1. 安装 GORM 及数据库驱动2. 建立数据库连接二、定义模型结构体三、自动迁