[USACO14MAR] Counting Friends

题目描述

Farmer John‘s N cows (2 <= N <= 500) have joined the social network "MooBook".

Each cow has one or more friends with whom they interact on MooBook. Just for fun, Farmer John makes a list of the number of friends for each of his cows, but during the process of writing the list he becomes distracted, and he includes one extra number by mistake (so his list contains N+1 numbers, instead of N numbers as he intended).

Please help Farmer John figure out which numbers on his list could have been the erroneous extra number.

FJ又有n(1<=n<=500)头奶牛都有一个或一个以上的朋友。FJ记录每头牛的朋友数,但他傻不小心混入了一个错的数字,请找出。

输入输出格式

输入格式:

  • Line 1: The integer N.
  • Lines 2..2+N: Line i+1 contains the number of friends for one of FJ‘s cows, or possibly the extra erroneous number.

输出格式:

  • Line 1: An integer K giving the number of entries in FJ‘s list that could be the extra number (or, K=0 means that there is no number on the list whose removal yields a feasible pairing of friends).
  • Lines 2..1+K: Each line contains the index (1..N+1) within the input ordering of a number of FJ‘s list that could potentially be the extra number -- that is, a number that can be removed such that the remaining N numbers admit a feasible set of

friendships among the cows. These lines should be in sorted order.

输入输出样例

输入样例#1:

4
1
2
2
1
3

输出样例#1:

3
1
4
5

说明

Farmer John has 4 cows. Two cows have only 1 friend each, two cows have 2 friends each, and 1 cow has 3 friends (of course, one of these numbers is extra and does not belong on the list).

Removal of the first number in FJ‘s list (the number 1) gives a remaining list of 2,2,1,3, which does lead to a feasible friendship pairing -- for example, if we name the cows A..D, then the pairings (A,B), (A,C), (A,D), and (B,C) suffice, since A has 3 friends, B and C have 2 friends, and D has 1 friend. Similarly, removing the other "1" from FJ‘s list also works, and so does removing the "3" from FJ‘s list. Removal of either "2" from FJ‘s list does not work -- we can see this by the fact that the sum of the remaining numbers is odd, which clearly prohibits us from finding a feasible pairing.

如果删除这个数合法,

那么按朋友数从大到小排序后,

枚举每个人,枚举它的朋友,

朋友数都减1,最后所有人的朋友数都减为0

注意每轮删减之后都要重新排序

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int a[504],b[504],ans[504];
int read(int &x)
{
    x=0; char c=getchar();
    while(!isdigit(c)) c=getchar();
    while(isdigit(c)) { x=x*10+c-‘0‘; c=getchar(); }
}
int main()
{
    int n,cnt;
    bool ok;
    read(n);
    for(int i=1;i<=n+1;i++) read(a[i]);
    for(int i=1;i<=n+1;i++)
    {
        ok=true; cnt=0;
        for(int j=1;j<=n+1;j++)
            if(j!=i) b[++cnt]=a[j];
        for(int j=1;j<=n+1;j++)
        {
            sort(b+1,b+cnt+1,greater<int>());
            for(int k=2;k<=n+1 && b[1];k++)
            {
                if(!b[k]) { ok=false; break; }
                b[1]--; b[k]--;
            }
            if(b[1])
            {
                ok=false;
                break;
            }
        }
        if(ok) ans[++ans[0]]=i;
    }
    printf("%d\n",ans[0]);
    sort(ans+1,ans+ans[0]+1);
    for(int i=1;i<=ans[0];i++) printf("%d\n",ans[i]);

}
时间: 2024-10-10 22:54:43

[USACO14MAR] Counting Friends的相关文章

解题:USACO14MAR Counting Friends

题面 枚举每个数字是否能被删去,然后就是如何判定图是否存在.应该从按"度数"从大到小排序,从最大的顺次向其他点连边(先连"度数"小的可能会把一些可以和大"度数"点连接的点用掉).但是这个排序每连一次都要做一次,而$N<=500$的情况下$O(n^3log$ $n)$并不能过.但是发现度数最多只有$n$,所以可以桶排,水过=.= USACO官方题解说可以再套一个数据结构变成$O(n^2log$ $n)$,然而并不会做 1 #include&l

UVA - 12075 Counting Triangles

Description Triangles are polygons with three sides and strictly positive area. Lattice triangles are the triangles all whose vertexes have integer coordinates. In this problem you have to find the number of lattice triangles in anMxN grid. For examp

POJ 2386 Lake Counting 搜索题解

简单的深度搜索就可以了,看见有人说什么使用并查集,那简直是大算法小用了. 因为可以深搜而不用回溯,故此效率就是O(N*M)了. 技巧就是增加一个标志P,每次搜索到池塘,即有W字母,那么就认为搜索到一个池塘了,P值为真. 搜索过的池塘不要重复搜索,故此,每次走过的池塘都改成其他字母,如'@',或者'#',随便一个都可以. 然后8个方向搜索. #include <stdio.h> #include <vector> #include <string.h> #include

Counting Divisors HDU - 6069

Counting Divisors HDU - 6069 题意:给定区间[a,b]和k,求xk有多少因子(x属于[a,b]),求和. 题解:http://blog.csdn.net/zlh_hhhh/article/details/76680641 a.b最大可达到1e12,但是b-a<1e6. 一开始愚蠢的一个一个分解然后去求有多少因子然后求和,范围那么大裸裸的超时啊! 可以枚举素数,对每一个素数,把区间内所有可以分解的进行分解. 最后再求和. 1 #include <bits/stdc++

LightOJ - 1148 Mad Counting(坑)

Mad Counting Time Limit: 500MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu Submit Status Description Mob was hijacked by the mayor of the Town "TruthTown". Mayor wants Mob to count the total population of the town. Now the naive a

【LeetCode】338. Counting Bits (2 solutions)

Counting Bits Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array. Example:For num = 5 you should return [0,1,1,2,1,2]. Follow up

[LeetCode][Java][JavaScript]Counting Bits

Counting Bits Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array. Example:For num = 5 you should return [0,1,1,2,1,2]. Follow up

Project Euler 85 :Counting rectangles 数长方形

Counting rectangles By counting carefully it can be seen that a rectangular grid measuring 3 by 2 contains eighteen rectangles: Although there exists no rectangular grid that contains exactly two million rectangles, find the area of the grid with the

HDU3664 Permutation Counting

Permutation Counting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1487    Accepted Submission(s): 754 Problem Description Given a permutation a1, a2, … aN of {1, 2, …, N}, we define its E-val