【解決方法】配列内の最大要素を見つける方法


Problem 3

Time limit: 1.0s
Memory limit: 256M
Author:
root
Allowed languages
C
Problem Description

You and your friend were sitting on the first and last bench respectively during the quiz. Recently, you got hold of an array that consists of the marks of all N

students seated for the quiz (from the first seat to the last seat in the same order). You want to maximize the sum of marks you and your friend get by potentially modifying the array. However, you don't want to raise suspicion. So, you both decide to perform a single operation on the array of the form:

    Cyclically shifting the array to the right by any amount

Here, a right cyclic shift by one unit on the array A={A1,A2,A3,A4}
would transform the array unto A={A4,A1,A2,A3}. Similarly, a right cyclic shift by some k units is equivalent to shifting it by one unit for k

times.

Note that after a cyclic shift, the scores of you and your friend are still given by the values present at the first and last place of the array respectively. Only the values in those places may be different from the original array due to the shift operation.

By right cyclic shifting the array by any amount at most once, find the maximum sum of marks you and your friend can achieve.
Input Format

The first line of input contains a single integer denoting the number of test-cases T. Then, 2T

lines follow:

For each test-case:

The first line contains a single integer N
that denotes the size of the array A

.

The second line contains N
space-separated integers A1,A2,....,AN describing the array A

that holds the marks of all the students.
Input constraints

    1≤T≤105

2≤N≤105

0≤Ai≤109

It is guaranteed that the sum of N
across all test-cases does not exceed 105

    .

Output Format

For each test-case: output on a single line the maximum sum of marks you and your friend can get by right cyclically shifting the array by any amount at most once.
Sample Input
Copy

2
4
1 2 3 3
2
1 2

Sample Output
Copy

6
3

Note

Explanation for sample input 1:

    For the first test-case of the first sample: notice that by right shifting array cyclically by 1 unit, you obtain the array A={3,1,2,3}

, where you and your friend both have 3 marks.

We can show that the sum 3+3=6

is the maximum sum possible.

For the second test-case of the first sample: the sum is already maximized and hence the array doesn't require any cyclic shifts.
#include <stdio.h>

int
main ()
{
  int t;
  scanf ("%d", &t);
  int arr
  while (t--)
    {
      for (int i = 0; i < t; i++)
	    {
	  scanf ("%d", &arr[i]);
	    }
      for (int j = 0; j < t - 1; j++)
	    {
	  sum[j] = arr[j] + arr[j + 1];

	    }
      sum[t - 1] = arr[t - 1] + arr[0];
      sum
      for (int k = 0; k < t ; k++)
	    {

	    if (sum
	        {
	      sum
	        }

	    }
      printf ("%d", sum



    }

  return 0;
}

私が試したこと:

Here, a right cyclic shift by one unit on the array A={A1,A2,A3,A4}
would transform the array unto A={A4,A1,A2,A3}. Similarly, a right cyclic shift by some k units is equivalent to shifting it by one unit for k

times.

Note that after a cyclic shift, the scores of you and your friend are still given by the values present at the first and last place of the array respectively. Only the values in those places may be different from the original array due to the shift operation.

By right cyclic shifting the array by any amount at most once, find the maximum sum of marks you and your friend can achieve.
Input Format

The first line of input contains a single integer denoting the number of test-cases T. Then, 2T

lines follow:

For each test-case:

The first line contains a single integer N
that denotes the size of the array A

.

The second line contains N
space-separated integers A1,A2,....,AN describing the array A

that holds the marks of all the students.
Input constraints

解決策 1

私たちは立ち往生している人々を喜んで助けますが、それは私たちがあなたのためにすべてをするためにここにいるという意味ではありません! 私たちがすべての作業を行うことはできません。あなたはこれに対して報酬を受け取っているか、またはそれはあなたの成績の一部であり、私たちがあなたのためにすべてを行うことはまったく公平ではありません.

だから私たちはあなたが仕事をする必要があり、あなたが行き詰まったときにあなたを助けます. それは、あなたが提出できる段階的な解決策を提供するという意味ではありません!
現在の状況と、プロセスの次のステップを説明することから始めます。 次に、その次のステップを機能させるために何を試みたか、またその際に何が起こったかを教えてください。

しかし、自問自答してください: あるループで値を読み取ってから、別のループで再度実行するのはなぜですか? そして、探しているのが数値の最大のペアだけである場合、なぜ 3 番目のループが必要なのでしょうか?
問題を注意深く考えてみてください。実際に値を移動する必要がないことは明らかです…

開始するのに問題がある場合は、これが役立つ場合があります。 問題を解決するためのコードの書き方、初心者向けガイド[^]

コメント

タイトルとURLをコピーしました