Codeforces1436 A. Reorder

2024-04-15 23:38
文章标签 reorder codeforces1436

本文主要是介绍Codeforces1436 A. Reorder,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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 𝑖.

思路:
等价于所有数的和

#include <cstdio>
#include <cstring>
#include <algorithm>using namespace std;int main() {int T;scanf("%d",&T);while(T--) {int n,m;scanf("%d%d",&n,&m);int sum = 0;for(int i = 1;i <= n;i++) {int x;scanf("%d",&x);sum += x;}if(sum == m) printf("YES\n");else printf("NO\n");}return 0;
}

这篇关于Codeforces1436 A. Reorder的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/907240

相关文章

Leetcode154: Reorder List

Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do this in-place without altering the nodes' values. For example, Given {1,2,3,4}, reorder it to {

微软实习生测试题题目1 : String reorder

For this question, your program is required to process an input string containing only ASCII characters between ‘0’ and ‘9’, or between ‘a’ and ‘z’ (including ‘0’, ‘9’, ‘a’, ‘z’). Your program shoul

99.Reorder List-重排链表(中等题)

重排链表 题目 给定一个单链表L: L0→L1→…→Ln-1→Ln, 重新排列后为:L0→Ln→L1→Ln-1→L2→Ln-2→… 必须在不改变节点值的情况下进行原地操作。样例 给出链表 1->2->3->4->null,重新排列后为1->4->2->3->null。题解 1.辅助栈 先遍历一遍链表,将节点依次入栈,再将指针移到到头结点结合依次弹栈操作就可以获取L0→Ln,L1→Ln

379.Reorder array to construct the minimum number-将数组重新排序以构造最小值(中等题)

将数组重新排序以构造最小值 题目 给定一个整数数组,请将其重新排序,以构造最小值。样例 给定 [3, 32, 321],通过将数组重新排序,可构造 6 个可能性数字: 3+32+321=332321 3+321+32=332132 32+3+321=323321 32+321+3=323213 321+3+32=321332 321+32+3=321323 其中,最小值为 32

ERROR in [eslint] reorder to top import/first

情景再现:在react开发的时候,导入组件、函数时报错:Import in body of module; reorder to top import/first … 原因:在import语句前有变量声明 解决: 变量的声明,要放在import之后 // 错误示例import { Button, Form, Input, Select, Space, message} from 'antd

leetcode 143 Reorder List 单链表

Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do this in-place without altering the nodes' values. For example, Given {1,2,3,4}, reorder it to {1,4

leetcode143~Reorder List

Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do this in-place without altering the nodes’ values. For example, Given {1,2,3,4}, reorder it to {1,4

LeetCode OJ:Reorder List

Reorder List   Total Accepted: 4205  Total Submissions: 22851 My Submissions Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do this in-place

143. Reorder List(Leetcode每日一题-2020.10.20)

Problem Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You may not modify the values in the list’s nodes, only nodes itself may be changed. Example1 Given 1

leetcode-m-Reorder List

143. Reorder List Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do this in-place without altering the nodes’ values. For example, Given {1,2,3,4},