codeforces 258div2C Predict Outcome of the Game

题目链接:http://codeforces.com/contest/451/problem/C

解题报告:三个球队之间一共有n场比赛,现在已经进行了k场,不知道每个球队的胜场是多少,如三个球队的胜场分别为a,b,c,那么已知的是:

|a - b | = d1

|b -c | = d2

输入n,k,d1,d2,要你判断有没有可能让三个球队最后的胜场数相同。

只要分四种情况就可以求出a,b,c分别是多少,然后判断是不是满足:

n % 3 == 0

a,b,c都小于n / 3

a >= 0 && b >= 0 && c >= 0

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 using namespace std;
 6 typedef long long INT;
 7 INT n,k,d1,d2;
 8 
 9 int judge(INT a,INT b,INT c)
10 {
11     if(n % 3 != 0) return 0;
12     if(a < 0 || b < 0 || c < 0) return 0;
13     if(a + b + c > k) return 0;
14     INT temp = n / 3;
15     if(a > temp || b > temp || c > temp) return 0;
16     return 1;
17 }
18     
19 int main()
20 {
21     int T;
22     scanf("%d",&T);
23     while(T--)
24     {
25         scanf("%lld%lld%lld%lld",&n,&k,&d1,&d2);
26         if(n % 3 != 0)
27         {
28             printf("no
");
29             continue;
30         }
31         int flag = 0;
32         INT t = (k + 2 * d1 + d2),a;
33         if(t % 3 == 0)
34         {
35             a = t / 3;
36             if(judge(a,a-d1,a-d1-d2)) flag = 1;
37         }
38         t = (k + 2 * d1 - d2);
39         if(t % 3 == 0)
40         {
41             a = t / 3;
42             if(judge(a,a-d1,a-d1+d2)) flag = 1;
43         }
44         t = (k - 2 * d1 + d2);
45         if(t % 3 == 0)
46         {
47             a = t / 3;
48             if(judge(a,a+d1,a+d1-d2)) flag = 1;
49         }
50         t = (k - 2 * d1 - d2);
51         if(t % 3 == 0)
52         {
53             a = t / 3;
54             if(judge(a,a+d1,a+d1+d2)) flag = 1;
55         }
56         printf(flag? "yes
":"no
");
57     }
58     return 0;
59 }    
View Code
原文地址:https://www.cnblogs.com/xiaxiaosheng/p/3868106.html