CSP-J semi-finals sprint must-answer questions | P5016 Dragon-Tiger Fight

Learn C++ from a young age! Record the questions in the CSP-J exam preparation study process and record every moment.

Attached is a summary post: CSP-J semi-final sprint must-answer questions | Summary_Blog of a communicator who loves programming-CSDN Blog


[Title description]

Xuanxuan and Kaikai are playing a game called "Dragon and Tiger Fight". The game board is a line segment. There are  n  barracks on the line segment (numbered 1∼n from left to right ) . Adjacent numbered barracks are separated from each other. 1 cm, that is, the chessboard is a line segment with a length of  n −1 cm. There are ci engineers  in barracks No.  i . Figure 1 below is  an example of n =6:

Xuanxuan is on the left, representing "dragon"; Kaikai is on the right, representing "tiger". They use  barracks No. M  as a dividing line. The engineers on the left belong to the dragon force, and the engineers on the right belong to the tiger force. However, the engineers   in barracks No. M are very entangled. They do not belong to either party.

The momentum of a barracks is: the number of engineers in the barracks × the distance from the barracks to  barracks No. m  ; the power of a party participating in the game is defined as: the sum of the momentum of all barracks belonging to this side. Figure 2 below is  an example of n =6, m =4, where red is the dragon square and yellow is the tiger square:

During the game, at a certain moment, a total of  s 1 engineers suddenly appeared in  the p 1 barracks. As friends of Xuan Xuan and Kai Kai, you know that if the difference in momentum between dragon and tiger is too great, Xuan Xuan and Kai Kai will not be willing to continue playing. In order for the game to continue, you need to choose a barracks  p 2 and send all s 2 engineers  in your hand  to barracks p 2 to make the difference in momentum between the two sides as small as possible.

Note: The engineer in your hand will belong to the same force as the other engineers in the barracks in whichever camp it falls in (if it falls in  barracks No. M  , it will not belong to any force).

【enter】

The first line of the input file contains a positive integer n , representing the number of barracks.

The next line contains  n  positive integers, separated by a space between two adjacent numbers. The  i  -th positive integer represents the   initial number of engineers  ci in the barracks numbered i .

The next line contains four positive integers, separated by a space between two adjacent numbers, representing  m , p 1, s 1, s 2 respectively.

[Output format]

The output file has one line containing a positive integer, namely  p 2, which represents the barracks number you selected. If there are multiple numbers that satisfy the optimal condition at the same time, use the smallest number.

【Input sample】

6 
2 3 2 3 2 3 
4 6 5 2

【Output sample】

2

[Detailed code explanation]

#include <bits/stdc++.h>
using namespace std;

int main()
{
    long long n;
    cin >> n;
    long long s[n+5];

    //遍历输入人工兵数量
    for (long long i=1; i<=n; i++) {
        cin >> s[i];
    }
    long long m, p1, s1, s2, max1=0, max2=0;

    cin >> m >> p1 >> s1 >> s2;

    //遍历累加得到龙与虎两方的势力
    for (long long i=1; i<=n; i++) {
        if (i<m) {
            max1 += s[i]*(m-i);
        }
        else if (i>m) {
            max2 += s[i]*(i-m);
        }
    }
    // cout << "max1 max2 " << max1 << " " << max2 << endl;
    //题目里面的p1指的是虎一方,虎方势力得到加强
    max2 += s1*(p1-m);

    //我们手里的棋子可以给m(两方差不多)、龙方(龙方比虎方少太多)、虎方(龙方笔虎方多太多)
    //手中的棋子给龙方或给m
    long long min1 = 1e9, idx1 = 0;
    for (int i=1; i<=m; i++) {
        long long ch, ch1;
        if (i!=m) {
            ch = max1 + s2*(m-i);
        } else {
            ch = max1;
        }

        ch1 = abs(ch-max2);
        if (min1>ch1){
            min1 = ch1; 
            idx1 = i;
        }

    }
    //手中的棋子给虎方
    long long min2 = 1e9, idx2 = 0;
    for (int i=m+1; i<=n; i++) {
        long long ch, ch1;
        ch = max2 + s2*(i-m);
        ch1 = abs(ch-max1);
        if (min2>ch1){
            min2 = ch1; 
            idx2 = i;
        }
    }

    //最后判断min1和min2的大小关系
    if (min1<min2) cout << idx1;
    else if (min1==min2) {
        if (idx1<idx2) cout << idx1;
        else cout << idx2;
    } else {
        cout << idx2;
    }
    return 0;
}

【operation result】

6 
2 3 2 3 2 3 
4 6 5 2 
2

Guess you like

Origin blog.csdn.net/guolianggsta/article/details/133386714