読者です 読者をやめる 読者になる 読者になる

TopCoder SRM 500 DIV2

Rating: 859→843(灰)
下がったーまあ当然ですね…

167.26 Points

250: SRMCards

愚直に問題文の定義の通り実装してしまった…

167.26 Points

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

class SRMCards {
public:
	inline int exists(int x, int sz){
		return 0 <= x && x < sz;
	}
	int maxTurns(vector <int> cards) {
		sort(cards.begin(), cards.end());
		int turn = 0;
		while(!cards.empty()){
			vector<int> v(cards.size(), 0);
			for(int i = 0; i < cards.size(); i++){
				if(exists(i - 1, cards.size())){
					if(cards[i - 1] == cards[i] - 1) v[i]++;
				}
				if(exists(i + 1, cards.size())){
					if(cards[i + 1] == cards[i] + 1) v[i]++;
				}
			}
			vector<int> w;
			for(int i = 0; i < v.size(); i++){
				if(v[i] == 0){
					w.push_back(cards[i]);
				}
			}
			if(w.size()){
				for(int i = 0; i < w.size(); i++){
					cards.erase(remove(cards.begin(), cards.end(), w[i]), cards.end());
					turn++;
				}
				continue;
			}
			for(int i = 0; i < v.size(); i++){
				if(v[i] > 0){
					cards.erase(remove(cards.begin(), cards.end(), cards[i] - 1), cards.end());
					cards.erase(remove(cards.begin(), cards.end(), cards[i] + 1), cards.end());
					cards.erase(remove(cards.begin(), cards.end(), cards[i]), cards.end());
					turn++;
					break;
				}
			}
		}
		return turn;
	}
};

500:

1000:

分からない。とくに500