纯转载,还没理解~
B. Party
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
n people came to a party. Then those, who had no friends among people at the party, left. Then those, who had exactly 1 friend among those who stayed, left as well. Then those, who had exactly 2, 3, ..., n - 1 friends among those who stayed by the moment of their leaving, did the same.
What is the maximum amount of people that could stay at the party in the end?
Input
The first input line contains one number t — amount of tests (1 ≤ t ≤ 105). Each of the following t lines contains one integer number n(1 ≤ n ≤ 105).
Output
For each test output in a separate line one number — the maximum amount of people that could stay in the end.
Examples
input
1
3
output
1
题目类型 想法题
题目意思
现在派对中有 n 个人 (1 <= n <= 1e5), 人与人之间可能存在朋友关系 (这个关系是要你选定的)
刚开始的时候 没有朋友的人离场
下一时刻 如果某个人在剩下的人中有 1 个朋友, 那么这个人就要离场, 即所有在剩下的人中拥有 1 个朋友的人都要离场
再下一时刻 在剩下有人中拥有 2 个朋友的人离场 直到没有人离场为止, 问要怎么安排人与人之间的朋友关系才能使最后剩下的人尽量多
只需输出最后最多能剩下多少人
解题方法
首先 每个人与其他的人都是朋友关系 即每个人拥有 n - 1 个朋友 , 这时我们令 1 和 n 不是朋友 , 那么在 n 个人中
只有 1 和 n 是拥有 n - 2个朋友的, 他们在 n - 2 时刻就要离场了这时剩下的 n - 2个人中所有人都是拥有 n - 3个朋友, 而 n - 3时刻
已经过去 所以剩下的人都可以留下
注意当 n == 1时答案是 0 其他情况答案都是 n - 2
附加解释 :
首先派对至少会有一个人离场 因为总有一个朋友数最少的人会在某时刻第一个离场
假设这个人有 d 个朋友, 那么我们可以推理下看是否能剩下n-1个人
既然第一个走的人有 d 个朋友, 那么其他人至少有 d+1 个朋友(这样才可能是第一个人先走)
现在要求 第一个人走后剩下的人的朋友数要 <= d 才可能会出现最后剩下n-1个人的情况(下一时刻就是d+1时刻了)
那么就意味着第一个人与其他人都是朋友(这样第一个人走后其他人的朋友数都可以从 d+1变成 d,只有这样才可能满足条件)
这样的话第一个人就有 n-1个朋友了(因为除了第1个人其余的人数为n-1),那么就意味着刚开始其他人至少要有 d+1=n个朋友
(显然是不可能的,因为某个人的朋友数最多才是n-1) 所以我们可以发现 最后剩下 n-1个人是不可行的
那么我们可以考虑剩下n-2个人可不可以呢? 刚才推理剩下n-1个人不行的原因是 第一个离场的人任务太重了
(任务太重的意思是其他人最后不用离场都要他的配合)
那么可以考虑再加一个人来配合他, 例如刚开始时所有人都是相互的朋友, 只有1和n不是朋友, 那么朋友数最少显然是1和n
而且当1和n同时走了以后其他人的朋友数就降为n-3了(原来是n-1,剩下的人各少了两个朋友1和n)
n-3时刻已经过去了所以剩下的人都不用离场
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
using namespace std;
const int maxn=4005;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
if(n==1)
printf("0\n");
else
printf("%d\n",n-2);
}
return 0;
}