当前位置:网站首页>Acwing week 52
Acwing week 52
2022-06-22 13:17:00 【A man of many ages】
List of topics
AcWing 4422. Intelligence test
Title Description
An intelligence test contains an infinite number of questions , The serial numbers are 1,2,3…
Completion of i The time required for this topic is i(i+1)2.
The total duration of the test is n.
Please calculate , Within the prescribed time , From 1 Start with the questions and answer them in sequence , How many questions can you complete at most ?
Input format
An integer n.
Output format
An integer , Indicates the maximum number of questions that can be completed .
Data range
The first three test points meet 1≤n≤25.
All test points meet 1≤n≤104.
sample input 1:
1
sample output 1:
1
sample input 2:
25
sample output 2:
4
analysis
Can directly simulate to solve this problem ,t(i) = i * (i+1) / 2, from i = 1 Start enumeration , until t(1) + t(2) + … + t(i) > until , The maximum number of questions that can be completed is i - 1.
Of course, you can also directly deduce the formula , Yes i2 and i The summation formula of , It is easy to find the sum formula of the sum of the two .
Code
#include <iostream>
using namespace std;
int main(){
int n;
cin>>n;
int sum = 0,i = 0;
while(sum <= n) {
i++;
sum += i * (i + 1) / 2;
}
cout<<i - 1<<endl;
return 0;
}
AcWing 4423. The nearest distance
Title Description
Given a length of n Array of integers for a1,a2,…,an.
For each integer i(1≤i≤n), Please find an integer j, requirement :
1≤j≤n
aj=0
If the above two conditions are met ,|i−j| It should be as small as possible .|i−j| The minimum possible value of may be bi To express .
Please calculate and output b1,b2,…,bn.
Ensure that there must be... In the given array 0.
Input format
The first line contains integers n.
The second line contains n It's an integer a1,a2,…,an.
Output format
a line ,n It's an integer b1,b2,…,bn.
Data range
front 4 Test points meet 1≤n≤10.
All test points meet 1≤n≤2×105,−109≤ai≤109.
sample input 1:
9
2 1 0 3 0 0 3 2 4
sample output 1:
2 1 0 1 0 0 1 2 3
sample input 2:
5
0 1 2 3 4
sample output 2:
0 1 2 3 4
sample input 3:
7
5 6 0 1 -2 3 4
sample output 3:
2 1 0 1 2 3 4
analysis
The title requires the output of each element in the array and its nearest 0 The distance between elements , Current 0 The element is either on its left or on its right , It can also be itself . So scan the array left and right , Record 0 At the same time, save the nearest to each element 0 The position of the element .
Code
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 200005;
int a[N],l[N];
int main() {
int n;
scanf("%d",&n);
int p = -1e9;
for(int i = 0;i < n;i++) {
scanf("%d",&a[i]);
if(!a[i]) p = i;
l[i] = p;
}
p = 1e9;
for(int i = n - 1;i >= 0;i--) {
if(!a[i]) p = i;
a[i] = min(p - i,i - l[i]);
}
for(int i = 0;i < n;i++) printf("%d ",a[i]);
return 0;
}
AcWing 4424. equation
Title Description
Given a nonnegative integer d, Please find two non negative real numbers a,b, Make the equation a+b=d and a×b=d Simultaneous establishment .
Input format
The first line contains integers T, Expressing common ownership T Group test data .
Each group of data occupies one row , Contains an integer d.
Output format
Output one line of answers for each group of data :
If there are a and b, First output a letter Y, Then output any group of a and b.
If there is no a and b, Then a letter is output N.
As long as you output a and b Be able to satisfy at the same time |(a+b)−a×b|≤10−6 as well as |(a+b)−d|≤10−6, As correct . In order to guarantee the accuracy , It is recommended that the output results be retained 10 Decimal place .
Data range
The first three test points meet 1≤T≤10.
All test points meet 1≤T≤1000,0≤d≤1000.
sample input :
10
0
1
2
3
4
5
6
7
8
9
sample output :
Y 0.0000000000 0.0000000000
N
N
N
Y 2.0000000000 2.0000000000
Y 3.6180339754 1.3819660246
Y 4.7320508212 1.2679491788
Y 5.7912878394 1.2087121606
Y 6.8284271359 1.1715728641
Y 7.8541019708 1.1458980292
analysis
This problem is to solve a system of equations , take b = d - a Into the ab = d, Available ,ab = ad - a2 = d, namely a2 - da + d = 0; That is, a quadratic equation of one variable ,delta = d2 - 4d, because d Nonnegative number , therefore d >= 4 when , The equation has two solutions ,d = 0 when ,a = b = 0; When the equation has a solution ,delta = sqrt(d2 - 4d) < d, So whether it's (d + delta) / 2 still (d - delta)/ 2 Are greater than 0 Of , Take one of the solutions and find b that will do , It's not difficult to find out b Also greater than 0 Of , Because two solutions a Not more than d.
Code
#include <cstdio>
#include <cmath>
using namespace std;
int main(){
int T,d;
scanf("%d",&T);
while(T--) {
scanf("%d",&d);
double delta = d * d - 4 * d;
if(!d) puts("Y 0.0000000000 0.0000000000");
else if(d < 4) puts("N");
else {
delta = sqrt(delta);
double a = (delta + d) / 2;
double b = d - a;
printf("Y %.10lf %.10lf\n",a,b);
}
}
return 0;
}
边栏推荐
- SNC processing failed SAP Router证书重新生成
- In June, China database industry analysis report was released! Smart wind, train storage and regeneration
- Write a contract testing tool from scratch
- Tips of setup in robotframework
- JAXB元素详解
- 155. Min Stack
- Maui uses Masa blazor component library
- 342. Power of Four
- Tis tutorial 04 client
- MySQL 5.7 + Navicat download and installation tutorial (with installation package)
猜你喜欢

SAP 开发Keys 申请SSCR Keys申请

310. Minimum Height Trees

Stop using system Currenttimemillis() takes too long to count. It's too low. Stopwatch is easy to use!

PHP deserialization & Magic method

MySQL_ Addition, deletion and modification of data processing

Pycharm shell script cannot be run

leetcode 11. 盛最多水的容器

Heavyweight live | bizdevops: the way to break the technology situation under the tide of digital transformation

SSM based library management system, high-quality graduation thesis example (can be used directly), project import video, attached source code and database script, Thesis Writing Tutorial

Tis tutorial 04 client
随机推荐
termux设置电脑连接手机。(敲打命令贼快),手机termux端口8022
Application of motion capture system in positioning and mapping of mobile robot in underground tunnel
redis修改密码,及启动、查看等操作
Using Sqlalchemy for combined paging queries
Redis
Reconstruction practice of complex C-end project of acquisition technology
文件下载漏洞&文件读取漏洞&文件删除漏洞
Reddit product director: a practical guide for NFT members for Web3 creators
2022-6-21os review group linking method
Termux set up the computer to connect to the mobile phone. (knock the command quickly), mobile phone termux port 8022
342. Power of Four
测试方法论——数据驱动测试
260. Single Number III
A2L file analysis based on CAN bus (1)
46. Permutations
SAP ABAP ole core code
docker安装postgresql
leetcode 11. 盛最多水的容器
Secondary development of robotframework -- file parsing
46. Permutations