2102: [Usaco2010 Dec]The Trough Game

2102: [Usaco2010 Dec]The Trough Game

Time Limit: 10 Sec  Memory Limit: 64 MB
Submit: 117  Solved: 84
[Submit][Status]

Description

Farmer John and Bessie are playing games again. This one has to do with troughs of water. Farmer John has hidden N (1 <= N <= 20) troughs behind the barn, and has filled some of them with food. Bessie has asked M (1 <= M <= 100) questions of the form, "How many troughs from this list (which she recites) are filled?". Bessie needs your help to deduce which troughs are actually filled. Consider an example with four troughs where Bessie has asked these questions (and received the indicated answers): 1) "How many of these troughs are filled: trough 1" --> 1 trough is filled 2) "How many of these troughs are filled: troughs 2 and 3" --> 1 trough is filled 3) "How many of these troughs are filled: troughs 1 and 4" --> 1 trough is filled 4) "How many of these troughs are filled: troughs 3 and 4" --> 1 trough is filled From question 1, we know trough 1 is filled. From question 3, we then know trough 4 is empty. From question 4, we then know that trough 3 is filled. From question 2, we then know that trough 2 is empty. 求N位二进制数X,使得给定的M个数,满足X and Bi=Ci ,Bi ci分别是读入的两个数

Input

* Line 1: Two space-separated integers: N and M * Lines 2..M+1: A subset of troughs, specified as a sequence of contiguous N 0‘s and 1‘s, followed by a single integer that is the number of troughs in the specified subset that are filled.

Output

* Line 1: A single line with: * The string "IMPOSSIBLE" if there is no possible set of filled troughs compatible with Farmer John‘s answers. * The string "NOT UNIQUE" if Bessie cannot determine from the given data exactly what troughs are filled. * Otherwise, a sequence of contiguous N 0‘s and 1‘s specifying which troughs are filled.

Sample Input

4 4
1000 1
0110 1
1001 1
0011 1

Sample Output

1010

HINT

Source

Silver

题解:一上来居然没有别的想法——只有暴力。。。然后写了个纯粹的二进制穷举,然后,然后,然后,居然AC了?!?!44ms也是醉大了= =

 1 type
 2     point=^node;
 3     node=record
 4                g:longint;
 5                next:point;
 6     end;
 7 var
 8    i,j,k,l,m,n,t:longint;
 9    a:array[0..10000] of point;
10    b,c,d:array[0..10000] of longint;
11    c1,c2:char;
12 procedure add(x,y:longint);inline;
13           var p:point;
14           begin
15                new(p);p^.g:=y;
16                p^.next:=a[x];a[x]:=p;
17           end;
18 procedure dfs(x:longint);inline;
19           var i,j,k,l:longint;p:point;
20           begin
21                if x>n then
22                   begin
23                        for i:=1 to m do if b[i]>0 then exit;
24                        if t=0 then
25                           begin
26                                for i:=1 to n do d[i]:=c[i];
27                                t:=1;
28                           end
29                        else
30                            begin
31                                 writeln(‘NOT UNIQUE‘);
32                                 halt;
33                            end;
34                   end
35                else
36                    begin
37                         p:=a[x];l:=0;
38                         while p<>nil do
39                               begin
40                                    if b[p^.g]=0 then
41                                       begin
42                                            l:=1;
43                                            break;
44                                       end;
45                                    p:=p^.next;
46                               end;
47                         if l=0 then
48                            begin
49                                 p:=a[x];
50                                 while p<>nil do
51                                       begin
52                                            dec(b[p^.g]);
53                                            p:=p^.next;
54                                       end;
55                                 c[x]:=1;
56                                 dfs(x+1);
57                                 p:=a[x];
58                                 while p<>nil do
59                                       begin
60                                            inc(b[p^.g]);
61                                            p:=p^.next;
62                                       end;
63                            end;
64                         c[x]:=0;
65                         dfs(x+1);
66                    end;
67           end;
68
69 begin
70      readln(n,m);
71      for i:=1 to m do a[i]:=nil;
72      for i:=1 to m do
73          begin
74               for j:=1 to n do
75                   begin
76                        read(c1);
77                        if c1=‘1‘ then add(j,i);
78                   end;
79               readln(b[i]);
80          end;
81      t:=0;
82      dfs(1);
83      IF t=0 then write(‘IMPOSSIBLE‘) else for i:=1 to n do write(d[i]);
84      writeln;
85      readln;
86 end.               
时间: 2024-11-03 05:39:34

2102: [Usaco2010 Dec]The Trough Game的相关文章

[Usaco2010 Dec]Exercise 奶牛健美操

[Usaco2010 Dec]Exercise 奶牛健美操 题目 Farmer John为了保持奶牛们的健康,让可怜的奶牛们不停在牧场之间 的小路上奔跑.这些奶牛的路径集合可以被表示成一个点集和一些连接 两个顶点的双向路,使得每对点之间恰好有一条简单路径.简单的说来, 这些点的布局就是一棵树,且每条边等长,都为1. 对于给定的一个奶牛路径集合,精明的奶牛们会计算出任意点对路径的最大值, 我们称之为这个路径集合的直径.如果直径太大,奶牛们就会拒绝锻炼. Farmer John把每个点标记为1..V

bzoj2101[Usaco2010 Dec]Treasure Chest 藏宝箱*

bzoj2101[Usaco2010 Dec]Treasure Chest 藏宝箱 题意: 给个序列,A与B轮流取数,谁取的数总和大谁赢.每次只能取序列两端,问A能取的数总和最大是多少.假设两人都用最优策略.序列大小≤5000 题解: dp.f[i][j][0]=max(f[i+1][j][1]+a[i],f[i][j-1][1]+a[j]),f[i][j][1]=min(f[i+1][j][0],f[i][j-1][0]). 代码: 1 #include <cstdio> 2 #includ

BZOJ 2101: [Usaco2010 Dec]Treasure Chest 藏宝箱( dp )

dp( l , r ) = sum( l , r ) - min( dp( l + 1 , r ) , dp( l , r - 1 ) ) 被卡空间....我们可以发现 l > r 是无意义的 , 所以可以省下一半的空间 -------------------------------------------------------------------------------- #include<cstdio> #include<cstring> #include<a

BZOJ2097[Usaco2010 Dec] 奶牛健美操

我猜我这样继续做水题会狗带 和模拟赛的题很像,贪心搞一下. 1 #include<bits/stdc++.h> 2 using namespace std; 3 int read(){ 4 int x=0,f=1;char ch=getchar(); 5 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 6 while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getch

[bzoj2097][Usaco2010 Dec]Exercise 奶牛健美操_贪心_树形dp_二分

Exercise bzoj-2097 Usaco-2010 Dec 题目大意:题目链接 注释:略. 想法:题目描述生怕你不知道这题在考二分. 关键是怎么验证?我们想到贪心的删边. 这样的策略是显然正确的. 之后树形dp的时候维护一下就行. 最后,附上丑陋的代码... ... #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #define N 10001

BZOJ2100 [Usaco2010 Dec]Apple Delivery

水水更健康...话说回来,这真的是水题?T T 首先,容易想到: 令ans1 = t1为源,到s和t2的距离之和:ans2 = t2为源,到s和t1的距离之和 ans = min(ans1, ans2) 然后,开始写单元最短路...spfa... 1 /************************************************************** 2 Problem: 2100 3 User: rausen 4 Language: C++ 5 Result: Tim

BZOJ2099: [Usaco2010 Dec]Letter 恐吓信

给两个长度不超过50000的串,A串可每次截连续一段复制出来,求最少复制几次能得到B串. 方法一:SAM.不会. 方法二:其实就是拿B去A上面匹配若干次.多次匹配的话可以把A先建个后缀数组,然后每次二分到B的位置搞匹配.匹配一次怕太慢的话,用hash吧!二分匹配长度找到第一个不可匹配的位置,就可以算匹配长度啦! 1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 #include<algo

【bzoj2097】[Usaco2010 Dec]Exercise 奶牛健美操 二分+贪心

题目描述 Farmer John为了保持奶牛们的健康,让可怜的奶牛们不停在牧场之间 的小路上奔跑.这些奶牛的路径集合可以被表示成一个点集和一些连接 两个顶点的双向路,使得每对点之间恰好有一条简单路径.简单的说来, 这些点的布局就是一棵树,且每条边等长,都为1. 对于给定的一个奶牛路径集合,精明的奶牛们会计算出任意点对路径的最大值, 我们称之为这个路径集合的直径.如果直径太大,奶牛们就会拒绝锻炼. Farmer John把每个点标记为1..V (2 <= V <= 100,000).为了获得更加

【BZOJ】2100: [Usaco2010 Dec]Apple Delivery(spfa+优化)

http://www.lydsy.com/JudgeOnline/problem.php?id=2100 这题我要吐血啊 我交了不下10次tle.. 噗 果然是写挫了. 一开始没加spfa优化果断t 然后看了题解加了(加错了T_T)还是tle..我就怀疑数据了... 噗 原来我有个地方打错了.. 这个spfa的队列优化真神.. #include <cstdio> #include <cstring> using namespace std; #define rep(i, n) fo