题干:

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最远的)

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

  1. 【CodeForces - 270C】Magical Boxes (思维,进制,有坑)

    题干: Emuskald is a well-known illusionist. One of his trademark tricks involves a set of magical boxe ...

  2. Codeforces 821C - Okabe and Boxes

    821C - Okabe and Boxes 思路:模拟.因为只需要比较栈顶和当前要删除的值就可以了,所以如果栈顶和当前要删除的值不同时,栈就可以清空了(因为下一次的栈顶不可能出现在前面那些值中). ...

  3. CodeForces 985E Pencils and Boxes

    Description Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives ...

  4. CodeForces - 985E Pencils and Boxes

    可以证明的是,总是存在一种最优策略使得每个组内的权值都是连续的. 所以排完序一遍 two pointers就好啦. Discription Mishka received a gift of mult ...

  5. CodeForces - 551C GukiZ hates Boxes(二分+贪心)

    题目链接:点击查看 题目大意:给出一个长度为 n 的数列 a,表示每个位置的障碍物数量,现在有 m 个学生可以来搬走障碍物,每一秒钟可以做出的行为如下: 从位置 i 移动到位置 i + 1 从当前位置 ...

  6. 【CodeForces - 349C】Mafia(思维模拟,优秀的二分)

    题干: One day n friends gathered together to play "Mafia". During each round of the game som ...

  7. CodeForces - 985E(Pencils and Boxes)

    题意:分配 n 个铅笔到笔筒中,每个笔筒最少放k个笔,每只铅笔有个value,同一个盒子中放入的笔的value差不能大于d,问能否找到满足条件的方案. 分析:根据题意,我们可以推出答案一定是将铅笔按 ...

  8. Codeforces - 985E Pencils and Boxes

    题意: 给定n支铅笔,问能不能分成若干堆,使得每堆数量不小于k且每堆的最大值和最小值之差不大于d. 题解: 排序.从后往前扫一遍.考虑以每个点为最小值建堆是否合法.对于当前点i,它需要至少k支铅笔,但 ...

  9. CodeForces - 821C Okabe and Boxes

    这题我说实话有点迷,题解的方法和模拟的思路相差有点大,但是对于每个可怀疑的细节都可以通过... 参考博客: https://blog.csdn.net/m0_37729344/article/deta ...

最新文章

  1. 浅析网站友情链接交换潜在问题有哪些?
  2. MATLAB从入门到精通-Matlab R2020b新功能 | 子标题和标题/标签对齐功能!
  3. man iptables by iptables-save v1.3.5
  4. tinymce 设置和获取编辑器的内容
  5. 《IBM-PC汇编语言程序设计》(第2版)【沈美明 温冬婵】答案
  6. OpenGL Gamma校正 (Gamma Correction)
  7. python学习-测试(文档测试 doctest、单元测试 unittest)
  8. linux查看php日志命令,linux查看日志的三种命令是什么,linux查看进程命令
  9. 处理minist数据集,把网络和数据都放在gpu上面。
  10. 重磅!阿里巴巴Blink正式开源,重要优化点解读\n
  11. 当信贷风控遇见机器学习,模型还是规则?
  12. pythonweb测试_python的web自动化测试
  13. JS常用事件兼容性处理方法
  14. dumpDex脱壳教程
  15. 20130408-[转]贴片钽电容的封装、尺寸和标识
  16. 树状分支图的html如何编写,流程图控件GoJS教程:树状图的分支设置
  17. 一个很实用的造数工具—Spawner Data Generator
  18. 树莓派linux控制录音,树莓派通过USB声卡录音和播放
  19. android遥控杆控件,Android自定义滑杆控件SeekBar多功能版本
  20. Oracle中查询用户表/索引/视图的创建语句

热门文章

  1. String ... String 三个点 jdk1.5的特性.才知道
  2. 地球化学图解系统GCDPlot 0.33
  3. Bound Found POJ - 2566 (尺取+前缀和)
  4. Java学习笔记4——I/O框架
  5. java程序设计实验报告册_20145215《Java程序设计》实验一实验报告
  6. mysql上k8s_通过搭建MySQL掌握k8s(Kubernetes)重要概念(上):网络与持久卷
  7. 增加数据_咱晋城人口又增加了?最新数据来了
  8. 车间生产能耗管控方案_如何给生产车间降温 环保空调的这些方案一定能帮到你...
  9. 基于探究式教学法的计算机网络原理课程的教学改革与实践,基于探究式教学法的“计算机网络原理”课程的教学改革与实践分析...
  10. echarts 生成 迁徙图_echarts3 迁徙图 迁入迁出(示例代码)