• Balls and Boxes (模拟题)


    Balls and Boxes

     CodeForces - 260C 

    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 4 balls. 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 ≤ 1051 ≤ 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 nspace-separated integers a1, a2, ..., an, where integer ai (0 ≤ ai ≤ 109ax ≠ 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 
    题意:第一行n,m表示有n个数,我们先将其中一个位置上的数全部取走,然后从该位置的下一个开始每个位置放一个,到了位置n则从头开始,放完这所有数的最后一个位置为M,问最初每个位置上的个数为多少。
    题解:大模拟。QAQ QAQ
       注意坑:最初想的是先找一个最小值,以最小值的下一个坐标作为起点,但是会忽略一点,最小值可能不止一个,而这种情况中你随意找到这个最小值不一定符合要求。
       例如 10 3
         1 1 1 1 1 1 1 1 1 1 我找到的最小值是第一个数,但是以第一个数作为起点明显不满足题意。
      正解:%大佬%大佬 大佬tql QAQQAQQAQQAQ
       直接从最后到达的位置倒着模拟,遇到为0的就一定是起始点。


     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #define inf 0x3f3f3f3f
     6 using namespace std;
     7 typedef long long ll;
     8 const int maxn=1e5+10;
     9 ll a[maxn];
    10 ll minn=inf;
    11 int main()
    12 {
    13     int pos,n, m;
    14     cin>>n>>m;
    15     ll sum=0;
    16     for(int i=1;i<=n;i++)
    17     {
    18         cin>>a[i];
    19         if(a[i]<minn)
    20         {
    21             minn=a[i];
    22         }
    23     }
    24     for(int i=1;i<=n;i++)
    25     {
    26         a[i]-=minn;
    27     }
    28     sum+=minn*n;
    29     int i=m;
    30     while(1)
    31     {
    32      //   cout<<i<<" "<<a[i]<<endl;
    33         if(a[i])
    34         {
    35             a[i]--;
    36             sum++;
    37         }
    38         else
    39         {
    40             a[i]=sum;
    41             break;
    42         }
    43         if(i==1)
    44             i=n;
    45         else
    46             i--;
    47     }
    48     for(int i=1;i<n;i++)
    49         cout<<a[i]<<" ";
    50     cout<<a[n]<<endl;
    51 }


  • 相关阅读:
    1、三八妇女节
    16、领导休假了,如何让领导帮忙签一下考勤呢?
    30、如何获取百分号前面的数字?
    29、如何在单元格里面添加单位?
    13、笔记本中的Excel 如何锁定单元格的$符号?
    28、把鼠标放在Excel的单元格中,单元格右下角十字架不能往下拖动
    31、如何把公式和单位合并在一起?
    27、Excel高级筛选
    2、中秋节
    9、香港人在大陆的驾驶证年审过期了,可不可以延迟办理?
  • 原文地址:https://www.cnblogs.com/1013star/p/9944861.html
Copyright © 2020-2023  润新知