码迷,mamicode.com
首页 > 编程语言 > 详细

Codeforces Round #312 (Div. 2) D. Guess Your Way Out! II 贪心排序

时间:2015-07-16 16:49:12      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

D. Guess Your Way Out! II
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Amr bought a new video game “Guess Your Way Out! II”. The goal of the game is to find an exit from the maze that looks like a perfect binary tree of height h. The player is initially standing at the root of the tree and the exit from the tree is located at some leaf node.

Let’s index all the nodes of the tree such that

The root is number 1
Each internal node i (i?≤?2h?-?1?-?1) will have a left child with index = 2i and a right child with index = 2i?+?1
The level of a node is defined as 1 for a root, or 1 + level of parent of the node otherwise. The vertices of the level h are called leaves. The exit to the maze is located at some leaf node n, the player doesn’t know where the exit is so he has to guess his way out!

In the new version of the game the player is allowed to ask questions on the format “Does the ancestor(exit,?i) node number belong to the range [L,?R]?”. Here ancestor(v,?i) is the ancestor of a node v that located in the level i. The game will answer with “Yes” or “No” only. The game is designed such that it doesn’t always answer correctly, and sometimes it cheats to confuse the player!.

Amr asked a lot of questions and got confused by all these answers, so he asked you to help him. Given the questions and its answers, can you identify whether the game is telling contradictory information or not? If the information is not contradictory and the exit node can be determined uniquely, output its number. If the information is not contradictory, but the exit node isn’t defined uniquely, output that the number of questions is not sufficient. Otherwise output that the information is contradictory.

Input
The first line contains two integers h,?q (1?≤?h?≤?50, 0?≤?q?≤?105), the height of the tree and the number of questions respectively.

The next q lines will contain four integers each i,?L,?R,?ans (1?≤?i?≤?h, 2i?-?1?≤?L?≤?R?≤?2i?-?1, ), representing a question as described in the statement with its answer (ans?=?1 if the answer is “Yes” and ans?=?0 if the answer is “No”).

Output
If the information provided by the game is contradictory output “Game cheated!” without the quotes.

Else if you can uniquely identify the exit to the maze output its index.

Otherwise output “Data not sufficient!” without the quotes.

Sample test(s)
input
3 1
3 4 6 0
output
7
input
4 3
4 10 14 1
3 6 6 0
2 3 3 1
output
14
input
4 2
3 4 6 1
4 12 15 1
output
Data not sufficient!
input
4 2
3 4 5 1
2 3 3 1
output
Game cheated!
Note
Node u is an ancestor of node v if and only if

u is the same node as v,
u is the parent of node v,
or u is an ancestor of the parent of node v.
In the first sample test there are 4 leaf nodes 4,?5,?6,?7. The first question says that the node isn’t in the range [4,?6] so the exit is node number 7.

In the second sample test there are 8 leaf nodes. After the first question the exit is in the range [10,?14]. After the second and the third questions only node number 14 is correct. Check the picture below to fully understand.
题意就是,给出h层的完全二叉树,从上到下从左到右标号1 - 2^n,要找出出口,出口在叶子结点处。再给q个区间,表示这个区间的子结点的叶子结点中有一个是出口,或都不是出口。
技术分享
我们用区间s e表示其中可能是出口,那么如果给的区间是出口,只需要求交集就可以了。得到的结果仍然是一个区间。如果,给的区间,不是出口,那就是把这部分排除啦。这样就可以会出现多个集合了。如果,就这样不断扩展,有可能最张出现q^2个集区,复杂度就会达到n^2了,这样是不行的。考虑一种贪心方法,因为只有能去掉两端的结点,这样的区间才是有效的,如果是在中间,是对结果没有影响的,如图中,2 3 对0是有影响的,在没有2 3 的情况 1号线,是对结果没有影响的,所以我们只需要通过排序,使得 2 3这样的先遇上就可以,排序后,从前往后,再从后向前,就一定保证了 2 3这种在前面,而且,不会使得结果集扩大成多集一直是单集。总的复杂度就是排序复杂度o(q * log(q));

#define N 100050
#define M 100005
#define maxn 205
#define fi first
#define se second
#define MOD 1000000000000000007
int h,q,layer[N],flag[N],pn;
ll s,e,ss,ee,l[N],r[N];
pll p[N];
void get(int layer,ll l,ll r ){
    ss = l << (h - layer);
    ee = r;
    FI(h - layer) ee = ee * 2 + 1;
}
int main()
{
    while(S2(h,q)!=EOF)
    {
        s = 1ll<<(h-1);
        e = (1ll<<h) - 1ll;
        bool isRight = true;
        pn = 0;
        FI(q)
        {
            S(layer[i]);cin>>l[i];cin>>r[i];S(flag[i]);
            if(isRight){
                get(layer[i],l[i],r[i]);
                if(flag[i]){
                    s = max(s,ss);e = min(e,ee);
                    if(s > e) isRight = false;
                }
                else {
                    p[pn++] = make_pair(ss,ee);
                }
            }
        }
        if(isRight){
            sort(p,p+pn);
            FI(pn){
                if(p[i].first <= s && p[i].second >= s)
                s = p[i].second + 1;
            }
            for(int i= pn-1;i>=0;i--){
                if(p[i].first <= e && p[i].second >=e)
                e = p[i].first - 1;
            }
        }
        if(s > e)printf("Game cheated!\n");
        else if(s == e) cout<<s<<endl;
        else printf("Data not sufficient!\n");
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

Codeforces Round #312 (Div. 2) D. Guess Your Way Out! II 贪心排序

标签:

原文地址:http://blog.csdn.net/mengzhengnan/article/details/46911577

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!