0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

特定操作を繰り返して3つの数字を同じにする

Last updated at Posted at 2022-06-18

はじめに

ABC093のC問題の解き方が公式の解答とやり方違ったのでメモで残しておく。
ただ、計算量は明らかに公式の解き方より多くなるので、いい解き方ではない。

問題

引用元
https://atcoder.jp/contests/abc093/tasks/arc094_a

3 つの整数 A,B,C が与えられます。
以下の 2 種類の操作を好きな順で繰り返して A,B,C をすべて等しくするために必要な操作の最小回数を求めてください。

  • A,B,C のうち 2 つを選んで、その両方を 1 増やす
  • A,B,C のうち 1 つを選んで、その整数を 2 増やす

解き方の方針

image.png

実際の提出コード

#include <iostream>
#include <vector>
#include <string>
#include <algorithm> // sort

using namespace std;

int main()
{
	vector<long> a(3);
	for (long i = 0; i < 3; i++)cin >> a[i];

	sort(a.begin(), a.end());

	long cnt = 0;
	while (true)
	{
		if (a[0] == a[1] && a[0] == a[2])break;

		if (a[2] != a[1])
		{
			a[2]--;
			cnt++;
		}

		if (a[1]!=a[0])
		{
			a[0] += 2;
			cnt++;
		}
		sort(a.begin(), a.end());
	}

	cout << cnt;
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?