A. Trust Nobody

Topic: Example:

enter
7
2
1 2
2
2 2
2
0 0
1
1
1
0
5
5 5 3 3 5
6
5 3 6 6 3 5

output
1
-1
0
-1
0
3
4

Idea:

        This question is also a thinking question. The key is that this kind of question has multiple answers, so we can output one of the answers. If it may not be determined, we need to output -1.

        The meaning of the question is, given n people, n people say that aj people lied, ask how many people may have lied that can be determined, if it cannot be determined, output -1.

        We assume that there are n situations in total, in which i (0 <= i <= n) people may lie, and then determine which one can definitely tell the truth, and then output the answer.

        What is important is that the condition for judging which one can be determined to be the truth is the comparison between the number of lies currently expressed by this person and the number of lies we assume person i, that is, when a[j] <= i, this person may be The truth.

The code is explained in detail below:

#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>
#define endl '\n'
#define YES puts("YES")
#define NO puts("NO")
#define umap unordered_map
#define All(x) x.begin(),x.end()
#pragma GCC optimize(3,"Ofast","inline")
#define ___G std::ios::sync_with_stdio(false),cin.tie(0), cout.tie(0)
using namespace std;
const int N = 2e5 + 10;

inline void solve()
{
	int a[N];	// 存储说话人 告诉我们的撒谎人数
	
	int n;	// n 个人
	cin >> n;
	
	for(int i = 0;i < n;++i)
	{
		// 存储说话人 说的撒谎人数
		cin >> a[i];
	}
	
	// 开始枚举情况,假设可能有 i 人说谎
	for(int i = 0;i <= n;++i)
	{
		int cnt = 0;	// 存储说真话的人数
		
		// 枚举每个人说话,比较是否符合我们的情况
		for(int j = 0;j < n;++j)
		{
			// 如果符合我们的情况,说真话人数累加
			// i >= a[j] 条件,表示 可能的 i 人说谎,而 j 号人说 有 a[j] 个人说慌,a[j] <= i
			// 即 a[j] 更接近答案一些,所以他有可能说的是真话的

			if(i >= a[j]) ++cnt;
		}
		
		// 如果说谎的人数等于了我们假设的情况
		// 那么输出该答案,有 i 个人说谎
		if(n - cnt == i)
		{
			cout << i << endl;
			return ;
		}
	}
	// 如果都不符合我们假设的情况,说明无法确定
	// 输出 -1
	cout << -1 << endl;
}


int main()
{
//	freopen("a.txt", "r", stdin);
	___G;
	int _t = 1;
	cin >> _t;
	while (_t--)
	{
		solve();
	}

	return 0;
}

Last commit:

Guess you like

Origin blog.csdn.net/hacker_51/article/details/133381070