HDU6486 Flower 【思维】

http://acm.hdu.edu.cn/showproblem.php?pid=6486
图片说明
要让所有数都俩俩差值为0,让N-1一个数-1,对于差值的影响,就是没有-1的那个数进行+1,最大操作次数为mx-1,现在要让每个数与最大值的差值为0,那么需要的操作次数就是sum(mx - a[i]),然后sum(mx - a[i]) <= mx-1,就表示可以,否则不可以

#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <iostream>
#include <map>
#include <set>

#define go(i, l, r) for(int i = (l), i##end = (int)(r); i <= i##end; ++i)
#define god(i, r, l) for(int i = (r), i##end = (int)(l); i >= i##end; --i)
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define debug_in  freopen("in.txt","r",stdin)
#define debug_out freopen("out.txt","w",stdout);
#define pb push_back
#define all(x) x.begin(),x.end()
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pii;
const ll maxn = 1e6+10;
const ll maxM = 1e6+10;
const ll inf_int = 1e8;
const ll inf_ll = 1e17;

template<class T>void read(T &x){
    T s=0,w=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
    x = s*w;
}
template<class H, class... T> void read(H& h, T&... t) {
    read(h);
    read(t...);
}

void pt(){ cout<<'\n';}
template<class H, class ... T> void pt(H h,T... t){ cout<<" "<<h; pt(t...);}

//--------------------------------------------

ll T,N;
ll a[maxn],mx;
int solve(){
    ll sum = 0;
    go(i,1,N) sum += mx - a[i];
    if(sum <= mx) return sum;
    return -1;
}
int main() {
//    debug_in;
//    debug_out;

    read(T);
    while(T--){
        read(N);
        mx = -1;
        for(int i = 1;i<=N;i++) {
            read(a[i]);
            mx = max(mx,a[i]);
        }
        cout<<solve()<<'\n';
    }




    return 0;
}
Ryuichi的算法分享 文章被收录于专栏

分享我的一些算法题解,致力于把题解做好,部分题解可能会采用视频的形式来讲解。

全部评论

相关推荐

行云流水1971:你的简历已经有不错的内容基础,但在岗位匹配度、成果量化、逻辑分层上还有优化空间,我结合产品 / 金融科技类岗位偏好帮你调整: 一、现有问题 & 优化方向 信息冗余:课程 / 学生工作与目标岗位关联弱,可精简; 成果颗粒度不足:部分数据缺少 “对比基准”(比如 “效率提升” 没说之前的情况); 岗位标签弱:产品岗核心能力(如需求闭环、PRD 撰写)体现不够突出。 二、优化后简历(以 “金融科技产品岗” 为例) 教育经历 2023.09-2027.06 郑州轻工业大学(公办一本) | 软件工程 | 本科 核心课程:Java 程序设计、数据库原理、Python(匹配产品岗 “技术理解” 需求) 学习成果:专业核心课 90+,获校级一等奖学金; 学生工作:院学生会主席,统筹 6 场校级活动(覆盖 2000 + 人次),锻炼跨部门协作与项目统筹能力。 实习经历
投了多少份简历才上岸
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务