Codeforces 714B. Filya and Homework

题目链接:http://codeforces.com/problemset/problem/714/B

题意:

  给你一个含有 n 个数的数组, 问你是否存在一个 x, 使得这个数组中的某些数加上 x, 某些数减去 x 后所有数都相等.

思路:

  如果这个数组里面不相等的数大于 3 个那么 x 就不可能存在. 否则如果这个数组里仅有三个数不一样且设为 a, b, c(a < b < c), 则还必须满足 b - a == c - b. 使得所有等于 a 的数加上 b - a 变为 b, 所有等于 c 的数减去 c - b 也变为 b, 则可以让所有数相等.  如果这个数组里面仅有两个数不一样且设为 a, b, 那么给所有为 a 的数加 (b - a) 或者给所有值为 b 的数减去(b - a) 那么这个数组里所有数也会相等. 如果这个数组里的数全部相等, 则不用变化就好, 上述三种情况满足其一就可以满足题意.

代码:

 1 #include <bits/stdc++.h>
 2
 3 using namespace std;
 4 typedef long long LL;
 5 const int MAXN = 100000;
 6 int arv[MAXN + 3];
 7
 8 int main() {
 9     ios_base::sync_with_stdio(0); cin.tie(0);
10     int n; cin >> n;
11     for(int i = 0; i < n; i++) cin >> arv[i];
12     sort(arv, arv + n);
13     int jud[MAXN + 3], len = 0;
14     jud[len++] = arv[0];
15     for(int i = 1; i < n; i++) if(arv[i] != arv[i - 1]) jud[len++] = arv[i];
16     if(len == 1 || len == 2  || (len == 3 && jud[2] + jud[0] == jud[1] * 2) ) cout << "YES" << endl;
17     else cout << "NO" << endl;
18     return 0;
19 }
时间: 2024-10-15 15:46:28

Codeforces 714B. Filya and Homework的相关文章

Codeforces Round #371 (Div. 2)B. Filya and Homework

题目链接:http://codeforces.com/problemset/problem/714/B 题目大意: 第一行输入一个n,第二行输入n个数,求是否能找出一个数x,使得n个数中的部分数加上x或部分数减去x ,n个数相等. 例如:5 1 3 3 2 1 输出: YES Init: x=1 情况,第一个数和最后一个数+x,第二三个数减去x ,则这n个数都为2(相等),故输出“YES” 解题思路: 对于刚才举的例子 可知 n个数只有三个元素 1 2 3 只要使得 2-1==3-2 即可满足条

CSUST 8.5 早训

## Problem A A - Meeting of Old Friends CodeForces - 714A 题意: 解题说明:此题其实是求两段区间的交集,注意要去除掉交集中的某个点. 题解: C++版本一 #include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<string> #include<cmath> int m

codeforces --- Round #250 (Div. 2) A. The Child and Homework

<传送门> 这题就是一个坑,尼玛wa了一大片啊. 自己被hack了,比赛结束后改了又wa两次才过. [题目大意] 其实就是一个猜题小技巧(联系自己初中考试的时候怎么猜题的,这题就好理解多了).这位同学是这样来选答案的:1.如果有一些选项长度至少比其他所有的描述短两倍,或至少超过所有其他的描述的两倍,那么孩子认为这个选择很可能是正确的.2.如果正好满足以上其中一种条件(重点),这个同学就会选择它,否则就选C.给你一个选择题,让你选择出这个同学将会选择的答案. [题目分析] 首先,这个题目就是一个

Codeforces Round #308 (Div. 2)

A. Vanya and Table Vanya has a table consisting of 100 rows, each row contains 100 cells. The rows are numbered by integers from 1 to 100 from bottom to top, the columns are numbered from 1 to 100 from left to right. In this table, Vanya chose n rect

CodeForces - 405C

Unusual Product Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status Description Little Chris is a huge fan of linear algebra. This time he has been given a homework about the unusual square of a square matrix. T

Codeforces Round #261 (Div. 2) E (DP)

E. Pashmak and Graph Pashmak's homework is a problem about graphs. Although he always tries to do his homework completely, he can't solve this problem. As you know, he's really weak at graph theory; so try to help him in solving the problem. You are

CodeForces 540B School Marks

http://codeforces.com/problemset/problem/540/B School Marks Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 540B Description Little Vova studies programming in an elite school. Vova and

CodeForces - 552E Vanya and Brackets

Vanya and Brackets Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Description Vanya is doing his maths homework. He has an expression of form , where x1, x2, ..., xn are digits from 1 to 9, and sign represents either a p

xtu read problem training 3 A - The Child and Homework

The Child and Homework Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 437A64-bit integer IO format: %I64d      Java class name: (Any) Once upon a time a child got a test consisting of multiple-choice