codeforces 1866A. Ambitious Kid

题目如下
A. Ambitious Kid
time limit per test1 second
memory limit per test256 megabytes
Chaneka, Pak Chanek's child, is an ambitious kid, so Pak Chanek gives her the following problem to test her ambition.

Given an array of integers [𝐴1,𝐴2,𝐴3,…,𝐴𝑁]. In one operation, Chaneka can choose one element, then increase or decrease the element's value by 1. Chaneka can do that operation multiple times, even for different elements.

What is the minimum number of operations that must be done to make it such that 𝐴1×𝐴2×𝐴3×…×𝐴𝑁=0?

Input
The first line contains a single integer 𝑁 (1≤𝑁≤105).

The second line contains 𝑁 integers 𝐴1,𝐴2,𝐴3,…,𝐴𝑁 (−105≤𝐴𝑖≤105).

Output
An integer representing the minimum number of operations that must be done to make it such that 𝐴1×𝐴2×𝐴3×…×𝐴𝑁=0

题目大意
题目要求是通过每次对数组中的任意元素进行加一或者减一, 来使得𝐴1×𝐴2×𝐴3×…×𝐴𝑁=0,求出最少的操作数,

题目分析
因为任何数乘以0都为0,所以我们要达到最小操作数只要对其中绝对值最小的最接近0的一直进行操作直到出现0

代码

点击查看代码
#include <stdio.h>
#include <cmath>

int main(){
    int n;
    scanf("%d",&n);
    long arr[200005];
    for(int i = 0; i < n; i++){
        scanf("%ld",&arr[i]);
    }
    long min = abs(arr[0]);
    for(int i = 1; i < n; i++){
       arr[i] = abs(arr[i]);
       if(arr[i] < min)
           min = arr[i];
    }
    printf("%ld",min);
    return 0;
}
posted @ 2025-07-02 21:16  sirro1uta  阅读(9)  评论(0)    收藏  举报