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.

ABC113 C - ID(diff 878) を C++ で解く

Last updated at Posted at 2022-11-15

既に解いた方も多いと思うが、
解説記事が Qiita に少ないので書いてみる。

思考のアプローチはシンプル。
step 1. 入力を丸ごと 昇順で sort。
step 2. 上から順番に答えとなる文字列を作り、map で保存。
step 3. 入力の順番に沿って map から取り出す。

あとは実装。

id.cpp
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
using namespace std;

int main() {
	int N, M; cin >> N >> M;
	int P, Y; vector<pair<int, int>>memo;
	for (int i = 0; i < M; i++) {
		cin >> P >> Y;
		memo.push_back(pair(P, Y));
	}
	vector<pair<int, int>>ans = memo;
    //step1: sort
	sort(memo.begin(), memo.end());

    //step 2: make map
	map<string, string>list;
	string temp = "000000";
	int cur = memo[0].first; int cnt = 1;
	for (int i = 0; i < M; i++) {
		string key = to_string(memo[i].first) + "_" + to_string(memo[i].second);

		if (cur != memo[i].first)
			cnt = 1; cur = memo[i].first;

		string A = to_string(memo[i].first);
		string B = to_string(cnt);
		cnt++;

		list[key] = temp.substr(0, 6 - A.size()) + A + temp.substr(0, 6 - B.size()) + B;
	}

    //step 3:output answer
	for (auto x : ans) {
		string key = to_string(x.first) + "_" + to_string(x.second);
		cout << list[key] << "\n";
	}
	//while (1) {}
	return 0;
}

有識者によると座標圧縮なる考え方があるらしい。

コメント少なすぎ!? 解説になったのかな;( ;•ω•ก)

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?