中级农民
- 积分
- 294
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2013-1-5
- 最后登录
- 1970-1-1
|
- #include <iostream>
- #include <map>
- #include <vector>
- #include <list>
- #include <algorithm>
- #include <sstream>
- using namespace std;
- bool can_divide_x_kind(std::vector<int> input)
- {
- std::map<int, int> htable;
- for (int i = 0; i < input.size(); i++) {
- htable[input[i]] ++;
- }
- std::map<int, int>::iterator it = htable.begin();
- for (; it != htable.end(); it++) {
- if (it->second < 2) {
- return false;
- }
- }
- }
- bool can_divide_5straight(std::vector<int> input)
- {
- std::map<int, int> htable;
- for (int i = 0; i < input.size(); i++) {
- htable[input[i]] ++;
- }
- while (htable.size() >= 5) {
- std::map<int, int>::iterator it = htable.begin();
- int val = it->first;
- it->second--;
- if (it->second == 0) {
- htable.erase(it++);
- } else {
- it++;
- }
- for (int i = 0; i < 4; i++) {
- int val1 = it->first;
- if (val1 = val + 1) {
- it->second--;
- if (it->second == 0) {
- htable.erase(it++);
- } else {
- val = val1;
- it++;
- }
- } else {
- return false;
- }
- }
- }
- if (htable.size()) {
- return false;
- }
- return true;
- }
- string list_to_string(std::list<int> list)
- {
- ostringstream oss;
- std::list<int>::iterator it = list.begin();
- for (; it != list.end(); it++) {
- oss << *it;
- }
- return oss.str();
- }
- int call = 0;
- int ret = 0;
- std::map<string, bool> memoized;
- bool can_divide_x_straight(std::list<int> input)
- {
- call++;
- string str = list_to_string(input);
- if (memoized.count(str)) {
- ret++;
- return memoized[str];
- }
- if (input.size() <= 2) {
- return false;
- }
- for (int i = 3; i <= input.size(); i++) {
-
- std::list<int> copy = input;
- std::list<int>::iterator it = copy.begin();
- std::list<int>::iterator it1;
- bool impossible = false;
- for (int j = 1; j <= i-1; j++) {
- it1 = it;
- while (*it1 == *it && it1 != copy.end()) {
- it1++;
- }
- if (it1 == copy.end()) {
- impossible = true;
- break;
- }
- if (*it1 != *it + 1) {
- impossible = true;
- break;
- }
- copy.erase(it);
- it = it1;
- }
- if (impossible == true) {
- continue;
- }
- copy.erase(it);
- if (copy.size() == 0 || can_divide_x_straight(copy)) {
- string str = list_to_string(input);
- memoized[str] = true;
- return true;
- }
- }
- str = list_to_string(input);
- memoized[str] = false;
- return false;
- }
- bool comp(int i, int j) {
- return i<j;
- }
- int main(void)
- {
- int vec[] = {
- 1,1,2,2,3,3,4,4,5,5,5
- };
- std::vector<int> input(vec, vec + sizeof(vec)/sizeof(int));
- std::sort(input.begin(), input.end(), comp);
- std::list<int> input1(vec, vec+ sizeof(vec)/sizeof(int));
- std::sort(input.begin(), input.end(), comp);
- if (can_divide_x_kind(input)) {
- cout << "1_good" <<endl;
- }
- if (can_divide_5straight(input)) {
- cout << "2_good" << endl;
- }
- if (can_divide_x_straight(input1)) {
- cout << "3_good" << " " << call << " " << ret << endl;
- } else {
- cout << call << " " << ret << endl;
- }
- return 0;
- }
复制代码 |
|