当前位置:网站首页>P1031 [NOIP2002 提高组] 均分纸牌
P1031 [NOIP2002 提高组] 均分纸牌
2022-07-07 08:14:00 【暴揍键盘的程序猿】
P1031 [NOIP2002 提高组] 均分纸牌
# [NOIP2002 提高组] 均分纸牌
## 题目描述
有$N$堆纸牌,编号分别为 $1,2,…,N$。每堆上有若干张,但纸牌总数必为 $N$ 的倍数。可以在任一堆上取若干张纸牌,然后移动。
移牌规则为:在编号为 $1$ 堆上取的纸牌,只能移到编号为 $2$ 的堆上;在编号为 $N$ 的堆上取的纸牌,只能移到编号为 $N-1$ 的堆上;其他堆上取的纸牌,可以移到相邻左边或右边的堆上。
现在要求找出一种移动方法,用最少的移动次数使每堆上纸牌数都一样多。
例如 $N=4$ 时,$4$ 堆纸牌数分别为 $9,8,17,6$。
移动 $3$ 次可达到目的:
- 从第三堆取 $4$ 张牌放到第四堆,此时每堆纸牌数分别为 $9,8,13,10$。
- 从第三堆取 $3$ 张牌放到第二堆,此时每堆纸牌数分别为 $9,11,10,10$。
- 从第二堆取 $1$ 张牌放到第一堆,此时每堆纸牌数分别为 $10,10,10,10$。
## 输入格式
第一行共一个整数 $N$,表示纸牌堆数。
第二行共 $N$ 个整数 $A_1,A_2,\cdots,A_N$,表示每堆纸牌初始时的纸牌数。
## 输出格式
共一行,即所有堆均达到相等时的最少移动次数。
## 样例 #1
### 样例输入 #1
```
4
9 8 17 6
```
### 样例输出 #1
```
3
```
## 提示
对于 $100\%$ 的数据,$1 \le N \le 100$,$1 \le A_i \le 10000$。
**【题目来源】**
NOIP 2002 提高组第一题
【AC代码】
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<string>
using namespace std;
typedef long long LL;
const int INF=0x3f3f3f3f;
const int N=110;
const double eps=1e-5;
int read()
{
char ch=getchar();
int x=0,f=1;
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-48,ch=getchar();}
return x*f;
}
int n,a[N],avr,ans;
int main()
{
n=read();
for(int i=1;i<=n;i++)
{
a[i]=read();
avr+=a[i];
}
avr/=n;
for(int i=1;i<=n;i++)
if(a[i]-avr)
{
a[i+1]+=a[i]-avr;
ans++;
}
printf("%d",ans);
return 0;
}
边栏推荐
猜你喜欢

The variables or functions declared in the header file cannot be recognized after importing other people's projects and adding the header file

JMeter loop controller and CSV data file settings are used together

Word自动生成目录的方法

Chris LATTNER, the father of llvm: why should we rebuild AI infrastructure software

ORM model -- creation and query of data records
![[second on] [jeecgboot] modify paging parameters](/img/59/55313e3e0cf6a1f7f6b03665e77789.png)
[second on] [jeecgboot] modify paging parameters

嵌入式背景知识-芯片

【acwing】789. 数的范围(二分基础)

Fiddler simulates the interface test

【acwing】786. Number k
随机推荐
Adb 实用命令(网络包、日志、调优相关)
Guide de signature du Code Appx
STM32 Basics - memory mapping
A wave of open source notebooks is coming
单片机(MCU)最强科普(万字总结,值得收藏)
反射效率为什么低?
VS Code指定扩展安装位置
JMeter installation
柏拉图和他的三个弟子的故事:如何寻找幸福?如何寻找理想伴侣?
Differences between MCU and MPU
Mongodb creates an implicit database as an exercise
Video based full link Intelligent Cloud? This article explains in detail what Alibaba cloud video cloud "intelligent media production" is
CONDA creates virtual environment offline
ORM model -- associated fields, abstract model classes
ISP、IAP、ICP、JTAG、SWD的编程特点
The physical meaning of imaginary number J
移动端通过设置rem使页面内容及字体大小自动调整
Postman interface test I
Leetcode exercise - 113 Path sum II
UnityWebRequest基础使用之下载文本、图片、AB包