Decrease (Judge ver.)

本文针对一个特定算法问题提出了一种优化方案,通过对最大值一次性减少到阈值以下的方法,简化了原有复杂操作流程,有效提高了程序效率。

题目描述

We have a sequence of length N consisting of non-negative integers. Consider performing the following operation on this sequence until the largest element in this sequence becomes N−1 or smaller. (The operation is the same as the one in Problem D.)
Determine the largest element in the sequence (if there is more than one, choose one). Decrease the value of this element by N, and increase each of the other elements by 1.
It can be proved that the largest element in the sequence becomes N−1 or smaller after a finite number of operations.
You are given the sequence ai. Find the number of times we will perform the above operation.

Constraints
2≤N≤50
0≤ai≤1016+1000

 

 

输入

Input is given from Standard Input in the following format:
N
a1 a2 ... aN
 

 

输出

Print the number of times the operation will be performed.

 

样例输入

4
3 3 3 3

 

样例输出

0

 

首先做这个题目不要被他的描述给绕进去了,题目虽然说的是每次找最大的数-N然后其他的数+1,但是这样在写程序的时候会发现很复杂,程序做了很多重复性的工作,通过认真分析,完全可以把最大的数一次性的减到N以下,然后计算减的次数,然后其他的数就加上减的次数,会发现这两个操作其实是等价的,显然后者会让程序算法优化很多,下面是程序代码

#include<iostream>
#include<vector>
using namespace std;
typedef long long int ll;

int main(){
	bool flag=true;//下方程序循环判断的标志,如果最大的值小于n的话,那么就结束循环
	ll  ans=0;

	vector<ll>ve;
	int n;
	cin>>n;
  ll temporary;
	int position;
	ll x;
	for(int i=0;i<n;i++){
		cin>>x;
		ve.push_back(x);//把数据读入到一个不定长数组里
		
	}
	ll max;
	while(flag){
	max=ve[0];//首先假定第一个元素为最大值
	position=0;//同时也假定最大元素的坐标是0;
	
	for(int i=1;i<n;i++){
	if(ve[i]>max){
		max=ve[i];//开始寻找数组中的最大值,并记录它的位置
	position=i;
	}
	}
	if(max>=n){
	temporary=max/n;//计算减到n以下需要的次数
	ve[position]%=n; 
	ans+=temporary;
	for(int i=0;i<n;i++){
		if(position==i)
			continue;
		ve[i]+=temporary;//把其他的元素加上上边减到n一下需要的次数
	}
	}
	else{
	cout<<ans<<endl;
	flag=false;
	}
	}
return 0;

}

 

LED Brightness Control with Push Buttons Objective: Design and implement an embedded system using the Nucleo L432KC board and Mbed that allows users to control the brightness of an LED using push buttons. One button will increase the brightness, and the other button will decrease it. Show the demo to the GTA. What you will need: 1 x Nucleo L432KC development board 1 x LED 2 x Push buttons Resistors (if required for LED current limiting and/or for push buttons to pull up or down) Breadboard and jumper wires Setup: Connect an LED to the Nucleo L432KC board's pin. Connect two push buttons to the pins for brightness control. Initialisation: Define a PWM output for controlling LED brightness (led1). Define digital inputs for the two push buttons (button and button2). Main Program: Implement a loop to continuously monitor the state of the push buttons. When one button is pressed, set a flag to indicate the desired action (increase or decrease brightness). Adjust LED brightness gradually based on the button press: oIf the first button is pressed, increase brightness gradually. oIf the second button is pressed, decrease brightness gradually. oIf no button is pressed, toggle the LED on/off. Gradual Brightness Change: Use a loop to increment or decrement the LED brightness gradually. Use thread_sleep_for() to introduce delays for a smooth brightness transition. Testing: Test the functionality by pressing the buttons and observing the LED brightness changes.Verify that the LED turns on/off when no button is pressed. Documentation: Document the circuit connections, code explanation, and any observations during testing. Take note of any challenges faced and how they were resolved.
最新发布
03-19
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值