Chinaunix首页 | 论坛 | 博客
  • 博客访问: 8014
  • 博文数量: 5
  • 博客积分: 353
  • 博客等级: 一等列兵
  • 技术积分: 60
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-10 14:00
文章分类
文章存档

2010年(5)

我的朋友
最近访客

分类: C/C++

2010-07-10 14:05:22

Now and then you play the following game with your friend. Your friend writes down a sequence consisting of zeroes and ones. You choose a continuous subsequence (for example the subsequence from the third to the fifth digit inclusively) and ask him, whether this subsequence contains even or odd number of ones. Your friend answers your question and you can ask him about another subsequence and so on. Your task is to guess the entire sequence of numbers. You suspect some of your friend's answers may not be correct and you want to convict him of falsehood. Thus you have decided to write a program to help you in this matter. The program will receive a series of your questions together with the answers you have received from your friend. The aim of this program is to find the first answer which is provably wrong, i.e. that there exists a sequence satisfying answers to all the previous questions, but no such sequence satisfies this answer. InputInput contains a series of tests. The first line of each test contains one number, which is the length of the sequence of zeroes and ones. This length is less or equal to 1 000 000 000. In the second line, there is one non-negative integer which is the number of questions asked and answers to them. The number of questions and answers is less or equal to 5 000. The remaining lines specify questions and answers. Each line contains one question and the answer to this question: two integers (the position of the first and last digit in the chosen subsequence) and one word which is either “even” or “odd” (the answer, i.e. the parity of the number of ones in the chosen subsequence, where “even” means an even number of ones and “odd” means an odd number). The input is ended with a line containing −1. OutputEach line of output containing one integer X. Number X says that there exists a sequence of zeroes and ones satisfying first X parity conditions, but there exists none satisfying X + 1 conditions. If there exists a sequence of zeroes and ones satisfying all the given conditions, then number X should be the number of all the questions asked.



#include <vector>
#include <map>
#include <set>
#include <stdio.h>
#include <algorithm>

using namespace std;

typedef set<int> NSet;

static vector<NSet> sets;
static map<int, int> vertex_mapping;
static vector<int> unused_set_num;
static vector<int> set_mapping;
static vector<int> conflict_sets;
/*----------- manages the value to vertex index mapping -----------*/
static int get_vertex_num(int a)
{
  if (vertex_mapping.find(a) == vertex_mapping.end()) {
    int new_num = vertex_mapping.size();
    vertex_mapping.insert(make_pair(a, new_num));
    return new_num;
  }
  return vertex_mapping[a];
}

/*----------- manages the set number assignment---------*/
static int allocateNewSetNum()
{
  int num = unused_set_num[unused_set_num.size() - 1];
  unused_set_num.pop_back();
  return num;
}

static void freeSetNum(int num)
{
  unused_set_num.push_back(num);
  conflict_sets[num] = -1;
}

/*---------- set operations ----------------*/
static int assignNewSet(int va)
{
  int set_num = allocateNewSetNum();
  /** allocate an empty conflict set, this would simplify
   * the operations */

  int conflict_num = allocateNewSetNum();
  conflict_sets[set_num] = conflict_num;
  conflict_sets[conflict_num] = set_num;
  sets[set_num].insert(va);
  return set_num;
}
static NSet& get_set(int va)
{
  int& set_num = set_mapping[va];
  if (set_num == -1)
    set_num = assignNewSet(va);
  return sets[set_num];
}

static int get_set_num(int va)
{
  int& set_num = set_mapping[va];
  if (set_num == -1)
    set_num = assignNewSet(va);
  return set_num;
}

static NSet& get_conflict_set(int va)
{
  int& set_num = set_mapping[va];
  if (set_num == -1)
    set_num = assignNewSet(va);
  return sets[conflict_sets[set_num]];
}

static int get_conflict_set_num(int va)
{
  int& set_num = set_mapping[va];
  if (set_num == -1)
    set_num = assignNewSet(va);
  return conflict_sets[set_num];
}

/* semantic of merging two sets, merge s2 to s1 */
static void doMerge(NSet& s1, int set_num1, NSet& s2, int set_num2)
{
  for (NSet::iterator it2 = s2.begin();
      it2 != s2.end(); ++it2) {
    if (s1.find(*it2) == s1.end()) {
      s1.insert(*it2);
      set_mapping[*it2] = set_num1;
    }
  }
  s2.clear();
  freeSetNum(set_num2);
}

static void dumpSet(NSet& s, int set_num)
{
  printf("set %d\n", set_num);
  for (NSet::iterator sit = s.begin(); sit != s.end();
      ++sit) {
    printf("%d ", *sit);
  }
  printf("\nconflict set is %d\n", conflict_sets[set_num]);
}

static void dumpAll()
{
  printf("vertex mapping\n");

  for (map<int,int>::iterator vit = vertex_mapping.begin();
      vit != vertex_mapping.end(); ++vit) {
    printf("val %d is vertex %d\n", vit->first, vit->second);
    printf("vertex %d maps to set %d\n", vit->second, set_mapping[vit->second]);
  }

  for (int i = 0; i < sets.size(); i++) {
    dumpSet(sets[i], i);
  }
  
}

/*----------- judge the conflict condition --------------*/
static int insertSegment(int a, int b, bool conflict)
{
  int va = get_vertex_num(a);
  int vb = get_vertex_num(b);
  int nsa = get_set_num(va);
  int nsb = get_set_num(vb);
  if (nsa == nsb) return conflict? -1: 0;
  
  int nca = get_conflict_set_num(va);
  int ncb = get_conflict_set_num(vb);
  if (nsa == ncb) return conflict? 0: -1;

  NSet& sa = get_set(va);
  NSet& sb = get_set(vb);
  NSet& ca = get_conflict_set(va);
  NSet& cb = get_conflict_set(vb);

  if (conflict) {
    if (sa.find(vb) != sa.end())
      return -1;
    doMerge(sa, nsa, cb, ncb);
    doMerge(sb, nsb, ca, nca);
    conflict_sets[nsa] = nsb;
    conflict_sets[nsb] = nsa;
  } else {
    if (ca.find(vb) != ca.end())
      return -1;
    doMerge(sa, nsa, sb, nsb);
    doMerge(ca, nca, cb, ncb);
  }
  return 0;
}

static void initStruct(int N)
{
  /** initialize the set number list */
  for (int i = 0; i < 4 * N; i++)
    unused_set_num.push_back(i);
  fill_n(back_inserter(set_mapping), 2 * N, -1);
  /* each set corresponds to a conflict set */
  fill_n(back_inserter(sets), 4 * N, NSet());
  fill_n(back_inserter(conflict_sets), 4 * N, -1);
}

static void solve(int N)
{
  for (int i = 0; i < N; i++) {
    int a, b, ret;
    /** BUG: suffers from overflow. Just for convenience */
    char condition[100];
    scanf("%d%d%s", &a, &b, condition);
    if (strcmp(condition, "even") == 0)
      ret = insertSegment(a, b + 1, false);
    else
      ret = insertSegment(a, b + 1, true);
    //dumpAll();

    if (ret == -1) {
      printf("%d\n", i);
      return;
    }
  }
  printf("%d\n", N);
}

int main()
{
  int N;
  scanf("%*d%d", &N);
  initStruct(N);
  solve(N);
  return 0;
}



阅读(357) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:循环小数

给主人留下些什么吧!~~