第三周周赛题

虽然简单但也简单记录一下

题目传送门
You are developing a project to build a new data center. The data center will be a rectangle with an area of exactly n square meters. Each side of the data center must be an integer.

Your goal is to minimize the impact of the external environment on the data center. For this reason, you want to minimize the length of the perimeter of the data center (that is, the sum of the lengths of its four sides).

What is the minimum perimeter of a rectangular data center with an area of exactly n square meters, if the lengths of all its sides must be integers?

Input
The first and only line of the input contains an integer n (1≤n≤105), where n is the area of the data center in square meters.

Output
Print the required minimum perimeter in meters.

Examples
Input
36
Output
24
Input
13
Output
28
Input
1
Output
4
Note
In the first example, the required shape of the data center is 6×6 square. Its area is 36 and the perimeter is 6+6+6+6=24.

In the second example, the required shape of the data center is 1×13 rectangle. Its area is 13 and the perimeter is 1+13+1+13=28.

In the third example, the required shape of the data center is 1×1 square. Its area is 1 and the perimeter is 1+1+1+1=4.

题意:简单

思路:设边长,利用已知去枚举解即可。
做题时:我想得不够缜密,就按照题意枚举,但对于得出的那个结果来说有一代女
我们只需枚举长方形一边的长度,如果面积是这个长度的倍数,那么面积除以该长度就是另一边的长度,然后更新长方形周长最大值并且保证自己所枚举的两边乘积等于面积将即可。
时间复杂度:O(n)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
   
    int n;
    scanf("%d",&n);
    ll sum=999999999999,ans,y;
    for(int i=1;i<=10000;i++)
    {
   
        ll yy=2*n;
        yy=yy/i;
        ans=2*i+yy;
        ll xx=n/i;
        if(ans<sum&&xx*i==n){
   
            sum=ans;
            y=i;
        }
    }
    sum=n/y;
    //cout<<y<<" "<<sum<<endl;
    printf("%lld",2*sum+2*y);
    return 0;
}

错误代码:
错误原因,枚举时候没有保证面积等于n;

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
   
    int n;
    scanf("%d",&n);
    ll sum=999999999999,ans,y;
    for(int i=1;i<=10000;i++)
    {
   
        ans=(2*i*i+2*n)/i;
        if(ans<sum){
   
            sum=ans;
            y=i;
        }
    }
    sum=n/y;
    printf("%lld",2*sum+2*y);
    return 0;
}

题目传送门

You are given an array a1,a2,…,an.

In one operation you can choose two elements ai and aj (i≠j) and decrease each of them by one.

You need to check whether it is possible to make all the elements equal to zero or not.

Input
The first line contains a single integer n (2≤n≤105) — the size of the array.

The second line contains n integers a1,a2,…,an (1≤ai≤109) — the elements of the array.

Output
Print “YES” if it is possible to make all elements zero, otherwise print “NO”.

Examples
Input
4
1 1 2 2
Output
YES
Input
6
1 2 3 4 5 6
Output
NO
Note
In the first example, you can make all elements equal to zero in 3 operations:

Decrease a1 and a2,
Decrease a3 and a4,
Decrease a3 and a4
In the second example, one can show that it is impossible to make all elements equal to zero.

思路
如果总和为奇数,必定不可能构造 否则总和为偶数。
如果存在最大值>总和-最大值,那么必定无解 否则必定有解。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MM=1e6+6;
ll a[MM];
int main()
{
   
    int n;
    scanf("%d",&n);
    ll sum=0,ans=0;;
    for(int i=0; i<n; i++)
    {
   
        scanf("%lld",&a[i]);
        ans+=a[i];
        if(sum<a[i])
            sum=a[i];
    }
    if(ans%2==0&&ans-sum>=sum)//当和为偶数,和减去最大值大于等于最大值
        printf("YES\n");
    else
        printf("NO\n");
    return 0;
}
全部评论

相关推荐

个人背景:学院二本计科专业&nbsp;大二开始实习个人经历:安克创新&nbsp;、理想汽车、字节跳动碎碎念:我做事只有三分钟热度。看到进了大厂的同学,我会羡慕,也会跟着努力上进;但遇到好看的小说,我又会放下手头的事沉迷其中,之前的坚持也就中断了。我有些自卑,总觉得自己学历和外貌都不够好。之前偶然在网上受到关注,我就喜欢上了上网,因为这里有很多人认可我。但我也很在意别人的评价,偶尔看到嘲讽的言论,会触发我的自卑情绪,让我感到愤怒。有时候我会强硬地回怼,有时候又会懦弱地选择无视。我也有虚荣心。不管是拿到安克、理想还是字节的机会,我在分享的时候都会带着这份心思。我会特意强调自己学历不好,是为了衬托出过程的艰难,以此显得自己更厉害。我知道,人往往会炫耀自己缺少的东西,来掩盖内心的空洞。我总想着走捷径,不太喜欢踏踏实实地做事。找实习的时候,我花了更多时间在研究面试技巧上,而不是提升专业能力。我会反复听面试录音分析技巧,看面试教程学习怎么和不同的面试官沟通,还会每天自言自语练习语言表达,同学都觉得我有点奇怪。我的实习生涯里,侥幸和运气占了很大一部分。我总在想,如果有一天我失去了这份幸运,这些特质可能会让我一蹶不振。ps:&nbsp;很多人会问我学习路线和经验&nbsp;但是就像我上面说的&nbsp;我的实习过程靠的很多是关键节点的运气&nbsp;技术上面我可能不如很多人&nbsp;&nbsp;所以请大家理性求助和理性参考我的回答&nbsp;附上我的投递记录
我的offer在哪里...:从去年看到现在,飞升哥就是榜样
我的求职进度条
点赞 评论 收藏
分享
程序员小白条:vibe你只要能cover住面试官问你很多深入的实现和选型方案的原因,以及怎么去检测优化效果的就行了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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