프로그래밍

[HackerRank] New Year Chaos

tedhong 2023. 2. 20. 12:53
2019-05-22 글쓴이 TED HONG

[HackerRank] New Year Chaos

줄을 서있는데  뒷사람이 앞사람에게 뇌물을 주고 자리를 바꿀 수 있음

한 사람당 2회 가능.

주어진 순서가 되려면 몇번 뇌물이 오고갔는가?

헷갈리네. ㅠ 

 static void minimumBribes(int[] q)
        {
            const int bribePower = 2;
            bool isChaotic = false;
            int n = q.Length;
            for (int i = 0; i  bribePower)
                {
                    Console.WriteLine("Too chaotic");
                    isChaotic = true;
                    break;
                }
            }
            
            int bribeCount = 0;
            if (isChaotic == false)
            {
                for (int i = 0; i < n; i++)
                {
                    Console.WriteLine("i =" + i + "q[i]-2 =" + (q[i] - 2));
                    for (int j = Math.Max(0, q[i]-2); j < i; j++)
                    {
                        Console.WriteLine(" j = "+j);
                        Console.WriteLine(string.Format("q[{0}] : {1} / q[{2}] : {3}", i, q[i], j, q[j]));

                        if (q[j] > q[i])
                        {
                            Console.WriteLine("bribeCount++");
                            bribeCount++;
                        }
                            
                    }
                    Console.WriteLine("========================================");
                }
               
                Console.WriteLine(bribeCount);
            }

        }