目录
A-Together
Problem Statement
You are given an integer sequence of length N,a1,a2,...,aN.
For each 1≤i≤N, you have three choices: add 1 to ai, subtract 1 from ai or do nothing.
After these operations, you select an integer X and count the number of i such that ai=X.
Maximize this count by making optimal choices.
Constraints
- 1≤N≤10^5
- 0≤ai<10^5(1≤i≤N)
- ai is an integer.
Input
The input is given from Standard Input in the following format:
N a1 a2 .. aN
Output
Print the maximum possible number of i such that ai=X.
Sample 1
| Inputcopy | Outputcopy |
|---|---|
7 3 1 4 1 5 9 2 |
4 |
For example, turn the sequence into 2,2,3,2,6,9,2and select X=2 to obtain 4, the maximum possible count.
Sample 2
| Inputcopy | Outputcopy |
|---|---|
10 0 1 2 3 4 5 6 7 8 9 |
3 |
Sample 3
| Inputcopy | Outputcopy |
|---|---|
1 99999 |
1 |
题目大意
输入N,a1....ai....aN,每个ai可以选择三种处理方式,+1,-1,+0, 输出经过操作后数组中存在的个数最多的那个数
思路
对每个数都进行三次处理,找出处理后数组中个数最多的数
代码
#include<iostream>
using namespace std;
const int N=1e5+5;
int a[N]; //数组较大,提前定义
int main(){
int n;
scanf("%d",&n);
int ai;
while(n--){
scanf("%d",&ai);
a[ai]++;
a[ai-1]++;
a[ai+1]++; //记录每种情况
}
int max=0;
for(int i=0;i<N-1;i++){
if(max<a[i])
max=a[i]; //找出
}
cout<<max<<endl;
return 0;
}
B-Fraction Again?!
It is easy to see that for every fraction in the form 1 k (k > 0), we can always find two positive integers x and y, x ≥ y, such that:
1/ k = 1 /x + 1/ y
Now our question is: can you write a program that counts how many such pairs of x and y there are for any given k?
Input
Input contains no more than 100 lines, each giving a value of k (0 < k ≤ 10000).
Output
For each k, output the number of corresponding (x, y) pairs, followed by a sorted list of the values of x and y, as shown in the sample output.
Sample Input
2 12
Sample Output
2
1/2 = 1/6 + 1/3
1/2 = 1/4 + 1/4
8
1/12 = 1/156 + 1/13
1/12 = 1/84 + 1/14
1/12 = 1/60 + 1/15
1/12 = 1/48 + 1/16
1/12 = 1/36 + 1/18
1/12 = 1/30 + 1/20
1/12 = 1/28 + 1/21
1/12 = 1/24 + 1/24
题目大意
多组输入,每组有一个k,输出满足1/k=1/x+1/y的 x y
思路
1/k=1/x+1/y变式得 x =(y*x)/(y-k) ,而y的范围一定(k+1<=y<=2*k),因此写一个y范围的循环,其中满足条件x =(y*x)/(y-k)便输出
代码
#include<cstdio>
int main()
{
int k,z,x,y,a[10005],b[10005];
while(~scanf("%d",&k)){
z=0;
for(y=k+1;y<=2*k;y++){ //y可取的范围
if((y*k)%(y-k)==0){
x=(y*k)/(y-k); //满足条件
if(x>=y){
a[z]=x; b[z]=y; z++; //记录

文章提供了多个编程挑战的详细解析,包括优化整数序列以找到最大匹配数,寻找分数的特定组合,计算最大正乘积以及解决蚂蚁行走问题。每个问题都涉及到数组处理、数学逻辑和最优化策略。
最低0.47元/天 解锁文章
1059





