zoukankan      html  css  js  c++  java
  • The Great Team(好题)

     The Great Team
    Time Limit:500MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

    Description

    When a few students of the Ural State University finished their sport career, the university encountered a lot of problems in team composition. Veterans of sports programming decided to play their role and create the most successful team in the history of the Ural SU.
    Veterans assumed that success of a team strongly depends on the number of friends in the ACM community the members of this team have. After more discussions they developed the criterion of success: all three members of the team should have the same number of friends.
    Unfortunately, the veterans failed to compose a team, as it turned out that there were no three programmers in the Ural SU that together satisfied this criterion.
    You should use this information to determine which students are friends of each other.

    Input

    The first line contains a single integer n (3 ≤ n ≤ 200), which is the number of students in the Ural SU participating in programming contests.

    Output

    If the veterans' calculations are correct, the first line should contain an integer k, which is the number of pairs of students that are friends of each other. The following k lines should contain these pairs. Students should be numbered 1 through n. If a problem has multiple correct answers, output any of them.
    If the veterans are wrong and the problem has no solution, output a single line containing a number −1.

    Sample Input

    inputoutput
    4
    
    2
    1 3
    3 4
     

    题意:有n个人,若可以使其中至多只有两个人朋友数同等,输出可能的朋友配对;否则,输出-1.

    实际上,对任意n,总可能使得至多有2人朋友数相等。采取的策略如下:

    (1)n为偶数

    设编号为i的人的朋友集合为S(i),如题中所给例子,S(1) = {3}, S(4) = {3},S(2) = ∅。对于编号由1到n/2的人,设置其朋友集合为S(i) = {n/2+i, n/2+i+1,… ,n},(1 <= i <= n/2)。这样就能使|S(i)| = |S(n - i + 1)| !=其它集合的势,此时每一个人的朋友数均与唯一的另一个人的朋友数相等,故满足至多有两个人朋友数相等的要求。

    (2)n为奇数

    设置第n个人没有朋友,而剩下的n - 1个人照上面的情况处理即可。

    AC CODE

     1 //Memory: 112 KB        Time: 15 MS
     2 //Language: C++        Result: Accepted
     3 
     4 #include <iostream>
     5 #include <cstdio>
     6 using namespace std;
     7 
     8 int main() 
     9 {
    10   int n;
    11   while(scanf("%d", &n) != EOF)
    12   {
    13     if(n & 1)  n--;
    14     printf("%d\n", (n + 2) * n / 8);
    15     for(int i = 1; i <= n / 2; i++)
    16     {
    17       for(int j = n / 2 + i; j <= n; j++)
    18       {
    19         printf("%d %d\n", i, j);        
    20       }
    21     }               
    22   }    
    23 }
  • 相关阅读:
    【数量技术宅|量化投资策略系列分享】股指期货IF分钟波动率统计策略
    【数量技术宅 | Python爬虫系列分享】实时监控股市重大公告的Python爬虫
    0-1背包问题
    活动选择的贪心算法与动态规划
    图的邻接表、拓扑排序、无权最短路径和加权最短路径
    把二叉树转变为左孩子右兄弟树
    基于接缝裁剪的图像压缩 算法导论
    公司聚会
    二叉堆部分练习
    编辑距离问题
  • 原文地址:https://www.cnblogs.com/cszlg/p/2910480.html
Copyright © 2011-2022 走看看