How many people have ipad II
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 174 Accepted Submission(s): 60
Problem Description
hh found more and more of his friends are having ipad IIs. One day when they get together, hh asked his five friends, "How many of you have ipad II now?"
"One!"
"Three!"
"Everyone!"
"Four!"
"Two!"
hh's friends knew each other. They were clear about the "how many" question, while the answers are different, so there must be some people telling lies.
One of hh's friends told him(hh):
number of people, who had ipad IIs, and lied, was no more than 1.
number of people, who didn't have ipad IIs, and told the truth, was no more than 2.
at least one people have ipad II.
Given the information, hh realized there may be one or two people having ipad IIs.
Now hh asks N people the "how many" question. These N friends answer one by one. Some tell the truth, some lie. What hh knows is:
1.number of people, who have ipad IIs, and lie, is no more than A.
2.number of people, who don't have ipad IIs, and tell the truth, is no more than B.
3.At least one people have ipad II.
How many ipad IIs do these N people have?
"One!"
"Three!"
"Everyone!"
"Four!"
"Two!"
hh's friends knew each other. They were clear about the "how many" question, while the answers are different, so there must be some people telling lies.
One of hh's friends told him(hh):
number of people, who had ipad IIs, and lied, was no more than 1.
number of people, who didn't have ipad IIs, and told the truth, was no more than 2.
at least one people have ipad II.
Given the information, hh realized there may be one or two people having ipad IIs.
Now hh asks N people the "how many" question. These N friends answer one by one. Some tell the truth, some lie. What hh knows is:
1.number of people, who have ipad IIs, and lie, is no more than A.
2.number of people, who don't have ipad IIs, and tell the truth, is no more than B.
3.At least one people have ipad II.
How many ipad IIs do these N people have?
Input
The input begins with an integer T(1<=T<=100).
The next T blocks each indicates a case.
The first line of each case contain a number N(1<=N<=16) then N positive integers follow, integers won't be lager than N.
Then following two numbers A , B(0 <= A,B <= N).
The next T blocks each indicates a case.
The first line of each case contain a number N(1<=N<=16) then N positive integers follow, integers won't be lager than N.
Then following two numbers A , B(0 <= A,B <= N).
Output
Output the number of people have ipad II.
There may be many answers, output them by increasing order. (separated by space)
Output "impossible" if that's impossible.
There may be many answers, output them by increasing order. (separated by space)
Output "impossible" if that's impossible.
Sample Input
3 5 1 2 3 4 5 1 2 3 0 0 0 1 1 4 0 0 0 0 0 1
Sample Output
1 2 1 impossible
Author
NotOnlySuccess
Source
Recommend
lcy
Statistic | Submit | Discuss | Note
m为拥有ipad的人数,tr为说真话的人数,N - tr为说谎人数。
假设tr个说真话的人中有j个拥有ipad,得
1、拥有ipad但说谎的人数为m - j,且0 <= m - j <= A;
2、木有ipad但说真话的人数为tr - j,且0 <= tr - j <= B;
3、j的范围:0 <= j <= tr.
4、i的范围:0 <= m <= N.
此外,这题有一个让人极其无语之处,就是N的范围不是题中所说的那么小。。。
AC Code:
#include <iostream>
#include <cstdio>
using namespace std;
int N, A, B, T, a[50], ans[50], top;
bool Judge(int m)
{
int tr = 0;
for(int i = 0; i < N; i++)
if(a[i] == m) tr++;
for(int j = 0; j <= tr; ++j)
{
if(m - j >= 0 && m - j <= A && tr - j >= 0 && tr - j <= B) return true;
}
return false;
}
int main()
{
scanf("%d", &T);
while(T--)
{
top = 0;
scanf("%d", &N);
for(int i = 0; i < N; i++)
{
scanf("%d", &a[i]);
}
scanf("%d %d", &A, &B);
for(int i = 1; i <= N; i++)
{
if(Judge(i)) ans[top++] = i;
}
if(!top) puts("impossible");
else
{
for(int i = 0; i < top - 1; i++) printf("%d ", ans[i]);
printf("%d\n", ans[top - 1]);
}
}
return 0;
}