Codeforces Round #678 (Div. 2)
A_Reorder
A. Reorder
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
For a given array 𝑎 consisting of 𝑛 integers and a given integer 𝑚 find if it is possible to reorder elements of the array 𝑎 in such a way that
equals 𝑚? It is forbidden to delete elements as well as insert new elements. Please note that no rounding occurs during division, for example, 52=2.5.
Input
The first line contains a single integer 𝑡 — the number of test cases (1≤𝑡≤100). The test cases follow, each in two lines.
The first line of a test case contains two integers 𝑛 and 𝑚 (1≤𝑛≤100, 0≤𝑚≤106). The second line contains integers 𝑎1,𝑎2,…,𝑎𝑛 (0≤𝑎𝑖≤106) — the elements of the array.
Output
For each test case print “YES”, if it is possible to reorder the elements of the array in such a way that the given formula gives the given value, and “NO” otherwise.
Example
inputCopy
2
3 8
2 5 1
4 4
0 1 2 3
outputCopy
YES
NO
Note
In the first test case one of the reorders could be [1,2,5]. The sum is equal to
. The brackets denote the inner sum
,while the summation of brackets corresponds to the sum over 𝑖.
大致题意:
经过一番我打字不太好描述不过看题面上的算式还挺好理解的运算,问能不能最终等于m,可以就输出YES,反之NO。
思路分析:
其实就是问和等不等于m啦。
一定要写的话会发现代码块为:
for (int j = 1; j <= n; j++) {
suma += double(a[j]) /double(j)*j;
//所以就是加上a[j]嘛。
}
发个代码:
#include <iostream>
#include <vector>
#include <cstring>
#include <string>
#include <iomanip>
#include <cmath>
#include <map>
#include <stack>
#include <algorithm>
#include <queue>
#include <set>
#include <cstdio>
typedef long long ll;
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
while (T--) {
int a[101] = {0};
int n, m;
double suma = 0;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
sort(a+1, a + n+1);
for (int j = 1; j <= n; j++) {
// cout<<"a[j]="<<a[j]<<" "<<"j="<<j<<" "<<double(a[j])/double(j)<<" ";
suma += a[j];
}
// cout<<endl;
// cout << "suma=" << suma << endl;
if (suma == m) cout << "YES" << endl;
else cout << "NO" << endl;
}
}
反思:
这题出的还不够快,阅读英文体面能力有待增强。
希望这段时间的CF加训后能有所提高!