• CodeForces ~ 996


    Allen has a LOT of money. He has nn dollars in the bank. For security reasons, he wants to withdraw it in cash (we will not disclose the reasons here). The denominations for dollar bills are 11, 55, 1010, 2020, 100100. What is the minimum number of bills Allen could receive after withdrawing his entire balance?

    Input

    The first and only line of input contains a single integer nn (1n1091≤n≤109).

    Output

    Output the minimum number of bills that Allen could receive.

    Examples

    Input
    125
    Output
    3
    Input
    43
    Output
    5
    Input
    1000000000
    Output
    10000000

    Note

    In the first sample case, Allen can withdraw this with a 100100 dollar bill, a 2020dollar bill, and a 55 dollar bill. There is no way for Allen to receive 125125 dollars in one or two bills.

    In the second sample case, Allen can withdraw two 2020 dollar bills and three 11 dollar bills.

    In the third sample case, Allen can withdraw 100000000100000000 (ten million!) 100100 dollar bills.

    解题思路:简单贪心

    Allen wants to enter a fan zone that occupies a round square and has nn entrances.

    There already is a queue of aiai people in front of the ii-th entrance. Each entrance allows one person from its queue to enter the fan zone in one minute.

    Allen uses the following strategy to enter the fan zone:

    • Initially he stands in the end of the queue in front of the first entrance.
    • Each minute, if he is not allowed into the fan zone during the minute (meaning he is not the first in the queue), he leaves the current queue and stands in the end of the queue of the next entrance (or the first entrance if he leaves the last entrance).

    Determine the entrance through which Allen will finally enter the fan zone.

    Input

    The first line contains a single integer nn (2n1052≤n≤105) — the number of entrances.

    The second line contains nn integers a1,a2,,ana1,a2,…,an (0ai1090≤ai≤109) — the number of people in queues. These numbers do not include Allen.

    Output

    Print a single integer — the number of entrance that Allen will use.

    Examples

    Input
    4
    2 3 2 0
    Output
    3
    Input
    2
    10 10
    Output
    1
    Input
    6
    5 2 6 5 7 4
    Output
    6

    Note

    In the first example the number of people (not including Allen) changes as follows: [2,3,2,0][1,2,1,0][0,1,0,0][2,3,2,0]→[1,2,1,0]→[0,1,0,0]. The number in bold is the queue Alles stands in. We see that he will enter the fan zone through the third entrance.

    In the second example the number of people (not including Allen) changes as follows: [10,10][9,9][8,8][7,7][6,6][5,5][4,4][3,3][2,2][1,1][0,0][10,10]→[9,9]→[8,8]→[7,7]→[6,6]→[5,5]→[4,4]→[3,3]→[2, 2]→[1,1]→[0,0].

    In the third example the number of people (not including Allen) changes as follows: [5,2,6,5,7,4][4,1,5,4,6,3][3,0,4,3,5,2][2,0,3,2,4,1][1,0,2,1,3,0][0,0,1,0,2,0][5,2,6,5,7,4]→[4,1,5,4,6,3]→[3,0,4,3,5,2]→[2,0,3,2,4,1]→[1,0,2,1,3,0]→[0,0,1,0,2,0].

     题意:有n 个元素,而你会按顺序每个等一分钟,看哪个先到0,每过一分钟,都要减1

    思路:你可以把每个元素算出一个步数,至少第几步才可以排到这个,首先看这个元素是否>n,大于的话说明还要走几圈,可以算出这个圈数再来算步数

    这是我的方法,但是后来看了下大牛的方法,直接算出圈数,选最小的圈数的那个,有相同的圈数的话选最先出现的那个,这样方便了很多

    引用大牛博客https://blog.csdn.net/ZscDst/article/details/80807353

    #include <bits/stdc++.h>
    using namespace std;
    const int MAXN = 1e5+5;
    const int INF = 0x3f3f3f3f;
    int n, ans;
    int main()
    {
        scanf("%d", &n);
        int MIN = INF;
        for (int i = 1; i <= n; i++)
        {
            int a; scanf("%d", &a);
            if(MIN > (a-i+n)/n)//x*n+i=a =》x=abs(a-i)/n
            {
                MIN = (a-i+n)/n;
                ans = i;
            }
        }
        printf("%d
    ", ans);
        return 0;
    }

    Allen dreams of one day owning a enormous fleet of electric cars, the car of the future! He knows that this will give him a big status boost. As Allen is planning out all of the different types of cars he will own and how he will arrange them, he realizes that he has a problem.

    Allen's future parking lot can be represented as a rectangle with 44 rows and nn (n50n≤50) columns of rectangular spaces, each of which can contain at most one car at any time. He imagines having kk (k2nk≤2n) cars in the grid, and all the cars are initially in the second and third rows. Each of the cars also has a different designated parking space in the first or fourth row. Allen has to put the cars into corresponding parking places.

     Illustration to the first example.

    However, since Allen would never entrust his cars to anyone else, only one car can be moved at a time. He can drive a car from a space in any of the four cardinal directions to a neighboring empty space. Furthermore, Allen can only move one of his cars into a space on the first or fourth rows if it is the car's designated parking space.

    Allen knows he will be a very busy man, and will only have time to move cars at most 2000020000 times before he realizes that moving cars is not worth his time. Help Allen determine if he should bother parking his cars or leave it to someone less important.

    Input

    The first line of the input contains two space-separated integers nn and kk (1n501≤n≤50, 1k2n1≤k≤2n), representing the number of columns and the number of cars, respectively.

    The next four lines will contain nn integers each between 00 and kk inclusive, representing the initial state of the parking lot. The rows are numbered 11 to 44 from top to bottom and the columns are numbered 11 to nn from left to right.

    In the first and last line, an integer 1xk1≤x≤k represents a parking spot assigned to car xx (you can only move this car to this place), while the integer 00 represents a empty space (you can't move any car to this place).

    In the second and third line, an integer 1xk1≤x≤k represents initial position of car xx, while the integer 00 represents an empty space (you can move any car to this place).

    Each xx between 11 and kk appears exactly once in the second and third line, and exactly once in the first and fourth line.

    Output

    If there is a sequence of moves that brings all of the cars to their parking spaces, with at most 2000020000 car moves, then print mm, the number of moves, on the first line. On the following mm lines, print the moves (one move per line) in the format ircc, which corresponds to Allen moving car ii to the neighboring space at row rr and column cc.

    If it is not possible for Allen to move all the cars to the correct spaces with at most 2000020000 car moves, print a single line with the integer 1−1.

    Examples

    Input
    4 5
    1 2 0 4
    1 2 0 4
    5 0 0 3
    0 5 0 3
    Output
    6
    1 1 1
    2 1 2
    4 1 4
    3 4 4
    5 3 2
    5 4 2
    Input
    1 2
    1
    2
    1
    2
    Output
    -1
    Input
    1 2
    1
    1
    2
    2
    Output
    2
    1 1 1
    2 4 1

    Note

    In the first sample test case, all cars are in front of their spots except car 55, which is in front of the parking spot adjacent. The example shows the shortest possible sequence of moves, but any sequence of length at most 2000020000 will be accepted.

    In the second sample test case, there is only one column, and the cars are in the wrong order, so no cars can move and the task is impossible.

    引用大牛博客:https://blog.csdn.net/ZscDst/article/details/80821376

    Allen is hosting a formal dinner party. 2n2n people come to the event in nn pairs (couples). After a night of fun, Allen wants to line everyone up for a final picture. The 2n2n people line up, but Allen doesn't like the ordering. Allen prefers if each pair occupies adjacent positions in the line, as this makes the picture more aesthetic.

    Help Allen find the minimum number of swaps of adjacent positions he must perform to make it so that each couple occupies adjacent positions in the line.

    Input

    The first line contains a single integer nn (1n1001≤n≤100), the number of pairs of people.

    The second line contains 2n2n integers a1,a2,,a2na1,a2,…,a2n. For each ii with 1in1≤i≤n, iiappears exactly twice. If aj=ak=iaj=ak=i, that means that the jj-th and kk-th people in the line form a couple.

    Output

    Output a single integer, representing the minimum number of adjacent swaps needed to line the people up so that each pair occupies adjacent positions.

    Examples

    Input
    4
    1 1 2 3 3 2 4 4
    Output
    2
    Input
    3
    1 1 2 2 3 3
    Output
    0
    Input
    3
    3 1 2 3 1 2
    Output
    3

    Note

    In the first sample case, we can transform 11233244112323441122334411233244→11232344→11223344 in two steps. Note that the sequence 11233244113232441133224411233244→11323244→11332244 also works in the same number of steps.

    The second sample case already satisfies the constraints; therefore we need 00 swaps.

    思路:简单贪心,只要遍历一遍,然后再找到当前元素交换过来即可

    For a vector v⃗ =(x,y)v→=(x,y), define |v|=x2+y2−−−−−−√|v|=x2+y2.

    Allen had a bit too much to drink at the bar, which is at the origin. There are nnvectors v1,v2,,vnv1→,v2→,⋯,vn→. Allen will make nn moves. As Allen's sense of direction is impaired, during the ii-th move he will either move in the direction vivi→ or vi−vi→. In other words, if his position is currently p=(x,y)p=(x,y), he will either move to p+vip+vi→ or pvip−vi→.

    Allen doesn't want to wander too far from home (which happens to also be the bar). You need to help him figure out a sequence of moves (a sequence of signs for the vectors) such that his final position pp satisfies |p|1.5106|p|≤1.5⋅106 so that he can stay safe.

    Input

    The first line contains a single integer nn (1n1051≤n≤105) — the number of moves.

    Each of the following lines contains two space-separated integers xixi and yiyi, meaning that vi=(xi,yi)vi→=(xi,yi). We have that |vi|106|vi|≤106 for all ii.

    Output

    Output a single line containing nn integers c1,c2,,cnc1,c2,⋯,cn, each of which is either 11 or 1−1. Your solution is correct if the value of p=ni=1civip=∑i=1ncivi→, satisfies |p|1.5106|p|≤1.5⋅106.

    It can be shown that a solution always exists under the given constraints.

    Examples

    Input
    3
    999999 0
    0 999999
    999999 0
    Output
    1 1 -1 
    Input
    1
    -824590 246031
    Output
    1 
    Input
    8
    -67761 603277
    640586 -396671
    46147 -122580
    569609 -2112
    400 914208
    131792 309779
    -850150 -486293
    5272 721899
    Output
    1 1 1 1 1 1 1 -1 





    思路:贪心+随机算法,(随机算法没听过,今天算是初次见面了)
    大牛博客:https://blog.csdn.net/ZscDst/article/details/80818824
  • 相关阅读:
    Linux系统服务
    Linux进程管理
    Linux压缩打包
    Linux输入输出
    Linux权限管理
    Linux用户管理
    Linux文件管理
    Linux-Shell
    Centos7 安装jdk1.8
    Python数据分析之路
  • 原文地址:https://www.cnblogs.com/Lis-/p/9255956.html
Copyright © 2020-2023  润新知