【CodeForces - 260C】Balls and Boxes (思维模拟,有坑,时光倒流)

题干:

Little Vasya had n boxes with balls in the room. The boxes stood in a row and were numbered with numbers from 1 to n from left to right.

Once Vasya chose one of the boxes, let's assume that its number is i, took all balls out from it (it is guaranteed that this box originally had at least one ball), and began putting balls (one at a time) to the boxes with numbers i + 1, i + 2, i + 3 and so on. If Vasya puts a ball into the box number n, then the next ball goes to box 1, the next one goes to box 2 and so on. He did it until he had no balls left in his hands. It is possible that Vasya puts multiple balls to the same box, and it is also possible that one or more balls will go to the box number i. If i = n, Vasya puts the first ball into the box number 1, then the next ball goes to box 2 and so on.

For example, let's suppose that initially Vasya had four boxes, and the first box had 3 balls, the second one had 2, the third one had 5 and the fourth one had 4balls. Then, if i = 3, then Vasya will take all five balls out of the third box and put them in the boxes with numbers: 4, 1, 2, 3, 4. After all Vasya's actions the balls will lie in the boxes as follows: in the first box there are 4 balls, 3 in the second one, 1 in the third one and 6 in the fourth one.

At this point Vasya has completely forgotten the original arrangement of the balls in the boxes, but he knows how they are arranged now, and the number x — the number of the box, where he put the last of the taken out balls.

He asks you to help to find the initial arrangement of the balls in the boxes.

Input

The first line of the input contains two integers n and x (2 ≤ n ≤ 105, 1 ≤ x ≤ n), that represent the number of the boxes and the index of the box that got the last ball from Vasya, correspondingly. The second line contains n space-separated integers a1, a2, ..., an, where integer ai (0 ≤ ai ≤ 109, ax ≠ 0) represents the number of balls in the box with index i after Vasya completes all the actions.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

Output

Print n integers, where the i-th one represents the number of balls in the box number i before Vasya starts acting. Separate the numbers in the output by spaces. If there are multiple correct solutions, you are allowed to print any of them.

Examples

Input

4 4
4 3 1 6

Output

3 2 5 4 

Input

5 2
3 2 0 2 7

Output

2 1 4 1 6 

Input

3 3
2 3 1

Output

1 2 3 

题目大意:

   就是说啊我有一堆放着球的盒子(但是可能有的盒子中没有球),我们现在选择一个有球的盒子,然后把里面的球全都拿出来并且给后面的分球,挨个分球(比如我从2号盒子拿出所有球来了,那么就从3号节点开始分球,,4号,5号这样,如果到头了那就再回到1号盒子开始分球)。现在给你了一共n个盒子,每个盒子在过程结束时的球数,以及是在最后发完球的时候是停在哪个盒子上了(也就是最后一个球是给的哪个盒子)。问你这些盒子中的每个盒子最初由多少球在里面。

解题报告:

    这题刚开始就想着模拟,,,结果wa4了。。。其实就是倒着模拟整个过程就好了。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
ll a[MAX];
int main()
{ll n,x,tmp=0;cin>>n>>x;ll minn = 0x3f3f3f3f3f3f;for(int i = 1; i<=n; i++) scanf("%lld",a+i),minn = min(minn,a[i]);for(int i = 1; i<=n; i++) a[i] -= minn;while(a[x] != 0) {a[x]--;x--;tmp++;if(x==0) x=n;}a[x] = tmp + minn*n;for(int i = 1; i<=n; i++) printf("%lld%c",a[i],i == n ? '\n' : ' ');return 0 ;}

 

错误代码:(这个是真的不用看了,,面目全非了)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
ll a[MAX];
ll n,x;
ll ans1[MAX],ans2[MAX];
bool ok(int i) {
//	ll up = a[i]; // n;
//	ll tmp = a[i] - up;
//	if(tmp % n + i == x) return 1;
//	else return 0;return 1;
}
int main()
{int cnt1=0,cnt2=0;cin>>n>>x;x--;for(int i = 0; i<n; i++) cin>>a[i];//从0开始读入ll minn = 0x3f3f3f3f3f; for(int i = 0; i<n; i++) {if(a[i] < minn) {cnt1 = 0;ans1[++cnt1] = i;minn = a[i];}else if (a[i] == minn) {ans1[++cnt1] = i;}}ll minn2 = 0x3f3f3f3f3f;for(int i = 0; i<n; i++) {if(a[i] < minn2 && a[i] > minn) {cnt2=0;ans2[++cnt2] = i;minn2 = a[i];}else if(a[i] == minn2) {ans2[++cnt2] == i;}}int that=-1;//先遍历最小for(int i = 1; i<=cnt1; i++) {if(ok(ans1[i])) {that = ans1[i];break;}}if(that != -1) {ll up = a[that];ll tmp = that <= x ?  x-that : x+n-that;a[that]=0;a[that]-=tmp;for(int i = 0; i<n; i++) {if(i != that) a[i] -= up;else a[i] -= up*n;}for(int i = 1; i<=tmp; i++) {a[(that+i)%n]--;} for(int i = 0; i<n; i++) {printf("%lld%c",i == that ? -a[i] : a[i] , i == (n-1) ? '\n' : ' ');}return 0 ;}for(int i = 1; i<=cnt2; i++) {if(ok(ans2[i])) {that = ans2[i];break;}}ll up = a[that];ll tmp = that < x ?  x-that : x+n-that;a[that]=0;a[that] -=tmp;for(int i = 0; i<n; i++) {if(i != that) a[i] -= up;else a[i] -= up*n;}for(int i = 1; i<=tmp; i++) {a[(that+i)%n]--;} for(int i = 0; i<n; i++) {printf("%lld%c",i == that ? -a[i] : a[i] , i == (n-1) ? '\n' : ' ');}
//	if(cnt == 1 && ans[1] <= x) {
//		ll all = a[ans[1]];//先存下来 
//		a[ans[1]]=0;
//		ll up = all / n;//ans[1] == n-1 ? 
//		all = all-up;//还剩多少需要发出去 
//		for(int i = 0; i<n; i++) {
//			if(i != ans[1]) a[i] += up;
//		}
//		for(int i = 1; i<=all; i++) {
//			a[(ans[1]+i)%n]++;
//		}
//	}return 0 ;}

总结:

  其实这题还可以:

         首先不难证明出必须要选择最小的,,,(一开始以为还有可能是第二小的,后来发现题意理解错了,,我出发点的值要变成1,一定是发了完整一圈以后的结果,所以和图形(笛卡尔坐标系?或者一个柱形图)结合一下就显然可证。)然后看在读入的x左方有没有这个最小值,如果有的话,就用最小值的当起点,如果多个,就用最右端的;如果左方没有,那就看右方,如果有的话那就用,如果有多个,那就用最右端的、、、应该这段口胡的算法没错、、、抽空尝试实现一下。(也就是先看左边,如果有就找离x最近的;否则看 右边,此时一定有,因为要保证有解,我们看离x最远的)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441200.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

文件服务器共享文件夹访问权限,5对文件服务器中的共享文件夹进行访问权限控制...

对文件服务器中的共享文件夹进行访问权限控制1. 实训目的在Windows Server 2003环境下设置文件服务器的目的是要对多用户进行资源共享&#xff0c;这其中经常遇到不同用户应该分配不同权限的问题&#xff0c;通过这个实训希望读者了解Windows Server 2003中访问权限设置方法和具…

java生日正则表达式_java之正则表达式、日期操作

正则表达式和日期操作正则表达式简介正则表达式就是使用一系列预定义的特殊字符来描述一个字符串的格式规则&#xff0c;然后使用该格式规则匹配某个字符串是否符合格式要求。作用:比如注册邮箱,邮箱有用户名和密码,一般会对其限制长度,这个限制长度的事情就是正则表达式做的规…

渲染服务器位置,如何用服务器做渲染

如何用服务器做渲染 内容精选换一换&#xfffd;&#xfffd;&#xfffd;&#xfffd;BoostKit ARMԭ&#xfffd;&#xfffd;ʹ&#xfffd;&#xfffd;&#xfffd;׼&#xfffd;&#xfffd;&#xfffd;&#xfffd;&#xfffd;&#xfffd;嵥&#xfffd;&#xfffd…

【HDU - 2376】Average distance (树,平均距离,算贡献)

题干&#xff1a; Given a tree, calculate the average distance between two vertices in the tree. For example, the average distance between two vertices in the following tree is (d 01 d 02 d 03 d 04 d 12 d 13 d 14 d 23 d 24 d 34)/10 (63799131510122)/10…

读取ppt并存入数据库_Java解析Excel文件并把数据存入数据库

前段时间做一个小项目&#xff0c;为了同时存储多条数据&#xff0c;其中有一个功能是解析Excel并把其中的数据存入对应数据库中。花了两天时间&#xff0c;不过一天多是因为用了"upload"关键字作为URL从而导致总报同一个错&#xff0c;最后在同学的帮助下顺利解决&a…

两台虚拟服务器如何级联,[教程] 利用open vswitch建立vxlan隧道实现不同主机上的虚拟交换机级联...

写在开头在某些环境下&#xff0c;需要实现两台物理机中的openvswitch交换机级联&#xff0c;以实现两台交换机中的设备互相通讯&#xff0c;这里使用vxlan隧道技术&#xff0c;将数据包封装在UDP中&#xff0c;通过以太网实现数据包传输。VXLAN是一种大二层的虚拟技术&#xf…

【POJ - 3041】Asteroids (二分图,最小点覆盖)

题干&#xff1a; Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 < N < 500). The grid contains K asteroids (1 < K < 10,000), which are conveniently located at the lattice points of the…

华为虚拟服务器lanip地址,2018软考网络工程师《华为基础实验》十九配置路由器为DHCPServer...

原标题&#xff1a;2018软考网络工程师《华为基础实验》十九配置路由器为DHCPServer实验要求:在R1上使能DHCP 功能。创建三个全局地址池&#xff0c;用于为三个不同部门的PC分配IP 地址。配置地址池的相关属性。在R1的接口下配置基于全局地址池的服务方式&#xff0c;实现DHCP …

电脑重启bootmgr_电脑系统启动:显示0xc0000428怎么办

错误代码&#xff1a;0xc0000428 一般都是驱动问题&#xff0c;只需要找到报错的路径驱动程序&#xff0c;删除再重启就基本上可以解决了。制作一个U盘启动&#xff0c;进入PE&#xff0c;然后删除”\Windoiws\System32\drivers\DsArk64.sys“文件&#xff0c;再重启就可以了。…

【 POJ - 2033 】Alphacode (dp,有坑)

题干&#xff1a; Alice and Bob need to send secret messages to each other and are discussing ways to encode their messages: Alice: "Lets just use a very simple code: Well assign A the code word 1, B will be 2, and so on down to Z being assigned 26.&…

两组的数据平均值合并_不要进入数据陷进

学习统计让我们不再被一些数据迷惑进入数据陷进&#xff08;例如平均工资&#xff09;从而做出正确的决策。描述性统计分析包括数据的分布、集中、波动的测度指标。平均值&#xff1a;一组数据的加和除以数据的个数&#xff08;容易随极端值变化&#xff09; 中位数&#xff1a…

【POJ - 3342】Party at Hali-Bula(树形dp,最大独立集,是否有唯一解)

题干&#xff1a; Dear Contestant, Im going to have a party at my villa at Hali-Bula to celebrate my retirement from BCM. I wish I could invite all my co-workers, but imagine how an employee can enjoy a party when he finds his boss among the guests! So, I…

安川西格玛7驱动器手册_什么是伺服驱动器?选型的原则有哪些?

头条号私信回复1&#xff0c;获取海量免费学习资源&#xff0c;内容包括自动化电气工程师必备的软件、电子书、视频教程等题图&#xff1a;伺服驱动器来源&#xff1a;百度图片什么是伺服驱动器&#xff1f;该如何选型&#xff1f;有哪些主流品牌&#xff1f;你想知道的全在这里…

java猜数游戏图形界面_Java做一个猜数的小游戏

Author &#xff1a; By Runsen效果展现猜数字游戏是一个简单&#xff0c;有趣的小游戏。游戏者通过输入一个指定区间的数字&#xff0c;与系统产生的随机数进行对比&#xff0c;然后输出相应的结果。游戏运行时产生一个0&#xff0d;10之间的随机整数&#xff0c;要求用户从控…

【CodeForces - 266C】Below the Diagonal (递归,子问题,贪心模拟)

题干&#xff1a; You are given a square matrix consisting of n rows and n columns. We assume that the rows are numbered from 1 to n from top to bottom and the columns are numbered from 1to n from left to right. Some cells (n - 1 cells in total) of the t…

python 0o_Python 中的比较:is 与 ==

在 Python 中会用到对象之间比较&#xff0c;可以用 &#xff0c;也可以用 is 。但是它们的区别是什么呢&#xff1f;is 比较的是两个实例对象是不是完全相同&#xff0c;它们是不是同一个对象&#xff0c;占用的内存地址是否相同。莱布尼茨说过&#xff1a;“世界上没有两片完…

python中long类型_浅谈python 四种数值类型(int,long,float,complex)

Python支持四种不同的数值类型,包括int(整数)long(长整数)float(浮点实际值)complex (复数),本文章向码农介绍python 四种数值类型,需要的朋友可以参考一下。 数字数据类型存储数值。他们是不可改变的数据类型,这意味着改变数字数据类型的结果,在一个新分配的对象的值。 N…

【CodeForces - 264A】Escape from Stones (模拟,卡精度的处理)

题干&#xff1a; Squirrel Liss lived in a forest peacefully, but unexpected trouble happens. Stones fall from a mountain. Initially Squirrel Liss occupies an interval [0, 1]. Next, nstones will fall and Liss will escape from the stones. The stones are nu…

python开发mbus程序_Python pywmbus包_程序模块 - PyPI - Python中文网

#WIP WM总线在Python中的实现本项目实施了无线m-bus标准的部分内容&#xff0c;定义见din en 13757-1及以下。目前&#xff0c;只支持未加密的短帧(即ci 0x7a)。欢迎拉取请求。##安装###点pip install pywmbus###手动git clone https://github.com/jalmeroth/pywmbus.gitcd pyw…

【CodeForces - 266B 】Queue at the School (模拟)

题干&#xff1a; During the break the schoolchildren, boys and girls, formed a queue of n people in the canteen. Initially the children stood in the order they entered the canteen. However, after a while the boys started feeling awkward for standing in fr…