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的算法分享 文章被收录于专栏

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

全部评论

相关推荐

牛客44320985...:你的当务之急是把这个糖的要死的沟槽ide主题改了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# 一张图晒出你司的标语 #
4388次浏览 77人参与
# AI面会问哪些问题? #
28345次浏览 569人参与
# 开放七大实习专项,百度暑期实习值得冲吗 #
15439次浏览 223人参与
# 你的实习产出是真实的还是包装的? #
20455次浏览 343人参与
# 找AI工作可以去哪些公司? #
9446次浏览 251人参与
# 春招至今,你的战绩如何? #
66535次浏览 586人参与
# 厦门银行科技岗值不值得投 #
8124次浏览 188人参与
# 从事AI岗需要掌握哪些技术栈? #
9292次浏览 325人参与
# 中国电信笔试 #
32089次浏览 295人参与
# 你做过最难的笔试是哪家公司 #
34555次浏览 249人参与
# 投递几十家公司,到现在0offer,大家都一样吗 #
341004次浏览 2175人参与
# 哪些公司真双非友好? #
69720次浏览 289人参与
# 阿里笔试 #
179086次浏览 1318人参与
# 机械人避雷的岗位/公司 #
62710次浏览 393人参与
# 小马智行求职进展汇总 #
25141次浏览 80人参与
# 第一份工作一定要去大厂吗 #
14938次浏览 122人参与
# 金三银四,你的春招进行到哪个阶段了? #
22299次浏览 284人参与
# 担心入职之后被发现很菜怎么办 #
291391次浏览 1210人参与
# 为了减少AI幻觉,你注入过哪些设定? #
26288次浏览 310人参与
# 应届生第一份工资要多少合适 #
20697次浏览 86人参与
# HR最不可信的一句话是__ #
6374次浏览 114人参与
# 沪漂/北漂你觉得哪个更苦? #
10098次浏览 194人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务