# 10月26日 CF#678 (Div. 2)强化训练 A_Reorder

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 ∑𝑛𝑖=1∑𝑛𝑗=𝑖𝑎𝑗𝑗
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 (11+22+53)+(22+53)+(53)=8
. 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加训后能有所提高!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值