第三週周賽題

雖然簡單但也簡單記錄一下

題目傳送門
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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章