AtCoder Beginner Contest 102

A - Multiple of 2 and N



Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 100100 points

Problem Statement

You are given a positive integer NN. Find the minimum positive integer divisible by both 22 and NN.

Constraints

  • 1≤N≤1091≤N≤109
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN

Output

Print the minimum positive integer divisible by both 22 and NN.


Sample Input 1 Copy

Copy

3

Sample Output 1 Copy

Copy

6

66 is divisible by both 22 and 33. Also, there is no positive integer less than 66 that is divisible by both 22 and 33. Thus, the answer is 66.


Sample Input 2 Copy

Copy

10

Sample Output 2 Copy

Copy

10

Sample Input 3 Copy

Copy

999999999

Sample Output 3 Copy

Copy

1999999998代码:
import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        if(n % 2 == 1)n *= 2;
        System.out.println(n);
    }
}
B - Maximum Difference


Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 200200 points

Problem Statement

You are given an integer sequence AA of length NN. Find the maximum absolute difference of two elements (with different indices) in AA.

Constraints

  • 2≤N≤1002≤N≤100
  • 1≤Ai≤1091≤Ai≤109
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN
A1A1 A2A2 ...... ANAN

Output

Print the maximum absolute difference of two elements (with different indices) in AA.


Sample Input 1 Copy

Copy

4
1 4 6 3

Sample Output 1 Copy

Copy

5

The maximum absolute difference of two elements is A3−A1=6−1=5A3−A1=6−1=5.


Sample Input 2 Copy

Copy

2
1000000000 1

Sample Output 2 Copy

Copy

999999999

Sample Input 3 Copy

Copy

5
1 1 1 1 1

Sample Output 3 Copy

Copy

0代码:
import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int max = 0,min = 1000000000;
        for(int i = 0;i < n;i ++) {
            int d = in.nextInt();
            max = Math.max(max, d);
            min = Math.min(min, d);
        }
        System.out.println(max - min);
    }
}
C - Linear Approximation


Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 300300 points

Problem Statement

Snuke has an integer sequence AA of length NN.

He will freely choose an integer bb. Here, he will get sad if AiAi and b+ib+i are far from each other. More specifically, the sadness of Snuke is calculated as follows:

  • abs(A1−(b+1))+abs(A2−(b+2))+...+abs(AN−(b+N))abs(A1−(b+1))+abs(A2−(b+2))+...+abs(AN−(b+N))

Here, abs(x)abs(x) is a function that returns the absolute value of xx.

Find the minimum possible sadness of Snuke.

Constraints

  • 1≤N≤2×1051≤N≤2×105
  • 1≤Ai≤1091≤Ai≤109
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN
A1A1 A2A2 ...... ANAN

Output

Print the minimum possible sadness of Snuke.


Sample Input 1 Copy

Copy

5
2 2 3 5 5

Sample Output 1 Copy

Copy

2

If we choose b=0b=0, the sadness of Snuke would be abs(2−(0+1))+abs(2−(0+2))+abs(3−(0+3))+abs(5−(0+4))+abs(5−(0+5))=2abs(2−(0+1))+abs(2−(0+2))+abs(3−(0+3))+abs(5−(0+4))+abs(5−(0+5))=2. Any choice of bb does not make the sadness of Snuke less than 22, so the answer is 22.


Sample Input 2 Copy

Copy

9
1 2 3 4 5 6 7 8 9

Sample Output 2 Copy

Copy

0

Sample Input 3 Copy

Copy

6
6 5 4 3 2 1

Sample Output 3 Copy

Copy

18

Sample Input 4 Copy

Copy

7
1 1 1 1 2 3 4

Sample Output 4 Copy

Copy

6先把所有的值减去对应下标的值,之后的序列是求都减去同一个值后,绝对值的和最小,那么就排序,找到中间位置的值,所有的数减去中间位置的值就可以了,中间位置的左边比他小,右边比他大,减去值后中间的变为0,左边的都增加,右边的豆腐减少,保证减少的小于等于增加的即可。代码:
import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int [] s = new int[n];
        long d = 0;
        for(int i = 0;i < n;i ++) {
            s[i] = in.nextInt();
            s[i] -= i + 1;
        }
        Arrays.sort(s);
        for(int i = 0;i < n;i ++) {
            d += Math.abs(s[i] - s[n / 2]);
        }
        System.out.println(d);
    }
}
D - Equal Cut


Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 600600 points

Problem Statement

Snuke has an integer sequence AA of length NN.

He will make three cuts in AA and divide it into four (non-empty) contiguous subsequences B,C,DB,C,D and EE. The positions of the cuts can be freely chosen.

Let P,Q,R,SP,Q,R,S be the sums of the elements in B,C,D,EB,C,D,E, respectively. Snuke is happier when the absolute difference of the maximum and the minimum among P,Q,R,SP,Q,R,S is smaller. Find the minimum possible absolute difference of the maximum and the minimum among P,Q,R,SP,Q,R,S.

Constraints

  • 4≤N≤2×1054≤N≤2×105
  • 1≤Ai≤1091≤Ai≤109
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN
A1A1 A2A2 ...... ANAN

Output

Find the minimum possible absolute difference of the maximum and the minimum among P,Q,R,SP,Q,R,S.


Sample Input 1 Copy

Copy

5
3 2 4 1 2

Sample Output 1 Copy

Copy

2

If we divide AA as B,C,D,E=(3),(2),(4),(1,2)B,C,D,E=(3),(2),(4),(1,2), then P=3,Q=2,R=4,S=1+2=3P=3,Q=2,R=4,S=1+2=3. Here, the maximum and the minimum among P,Q,R,SP,Q,R,Sare 44 and 22, with the absolute difference of 22. We cannot make the absolute difference of the maximum and the minimum less than 22, so the answer is 22.


Sample Input 2 Copy

Copy

10
10 71 84 33 6 47 23 25 52 64

Sample Output 2 Copy

Copy

36

Sample Input 3 Copy

Copy

7
1 2 3 1000000000 4 5 6

Sample Output 3 Copy

Copy

999999994如果是排着去找这三个位置,是O(n^3)的,可以采用折半枚举的技巧,先找到第二个位置,然后两边各自分为两块,用l和r标记,两块的差距尽可能小即可,随着i的右移,lr只需要初始化一次,然后跟着移动即可,i的移动必然会打破原来的平衡,lr只能继续右移,不需要从最初端开始,不然仍然超时。java代码:
import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        long [] s = new long[n + 1];
        for(int i = 1;i <= n;i ++) {
            s[i] = in.nextLong();
            s[i] += s[i - 1];
        }
        int l = 1,r = 3;
        long ans = s[n];
        for(int i = 2;i < n - 1;i ++) {
            long last = s[n];
            while(l < i) {
                long d = Math.abs(s[i] - s[l] * 2);
                if(d > last) break;
                else last = d;
                l ++;
            }
            l --;
            last = s[n];
            while(r < n) {
                long d = Math.abs(s[n] + s[i] - s[r] * 2);
                if(d > last) break;
                else last = d;
                r ++;
            }
            r --;
            long max = Math.max(Math.max(Math.max(s[l], s[i] - s[l]), s[r] - s[i]), s[n] - s[r]);
            long min = Math.min(Math.min(Math.min(s[l], s[i] - s[l]), s[r] - s[i]), s[n] - s[r]);
            ans = Math.min(max - min, ans);
        }
        System.out.println(ans);
    }
}

c++代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#define Max 200005
using namespace std;
typedef long long LL;
int main() {
    int n;
    int a[Max];
    LL sum[Max] = {0};
    scanf("%d",&n);
    for(int i = 1;i <= n;i ++) {
        scanf("%d",&a[i]);
        sum[i] = sum[i - 1] + a[i];
    }
    LL ans = sum[n];
    int l = 1,r = 3;
    for(int i = 2;i <= n - 2;i ++) {
        LL last = sum[n];
        while(l < i) {
            LL d = abs(sum[i] - sum[l] * 2);
            if(d > last) break;
            else last = d;
            l ++;
        }
        l --;
        last = sum[n];
        while(r < n) {
            LL d = abs(sum[n] + sum[i] - sum[r] * 2);
            if(d > last) break;
            else last = d;
            r ++;
        }
        r --;
        LL up = max(max(max(sum[l],sum[i] - sum[l]),sum[r] - sum[i]),sum[n] - sum[r]);
        LL down = min(min(min(sum[l],sum[i] - sum[l]),sum[r] - sum[i]),sum[n] - sum[r]);
        ans = min(ans,up - down);
    }
    printf("%lld",ans);
}

原文地址:https://www.cnblogs.com/8023spz/p/9452315.html

时间: 2024-10-09 22:11:59

AtCoder Beginner Contest 102的相关文章

AtCoder Beginner Contest 103 D(贪心)

AtCoder Beginner Contest 103 D 题目大意:n个点,除第n个点外第i与第i+1个点有一条边,给定m个a[i],b[i],求最少去掉几条边能使所有a[i],b[i]不相连. 按右端点从小到大排序,如果当前选的去掉的边在区间内,那么符合条件,否则ans++,并贪心地把去掉的边指向右端点,因为前面的区间都满足条件了,所以要去掉的边要尽量向右移使其满足更多的区间. 1 #include <iostream> 2 #include <cstdio> 3 #incl

AtCoder Beginner Contest 136

AtCoder Beginner Contest 136 Contest Duration : 2019-08-04(Sun) 20:00 ~ 2019-08-04(Sun) 21:40 Website: AtCoder BC-136 后面几题都挺考思考角度D. C - Build Stairs 题目描述: 有n座山从左到右排列,给定每一座山的高度\(Hi\),现在你可以对每座山进行如下操作至多一次:将这座山的高度降低1. 问是否有可能通过对一些山进行如上操作,使得最后从左至右,山的高度呈不下降

AtCoder Beginner Contest 154 题解

人生第一场 AtCoder,纪念一下 话说年后的 AtCoder 比赛怎么这么少啊(大雾 AtCoder Beginner Contest 154 题解 A - Remaining Balls We have A balls with the string S written on each of them and B balls with the string T written on each of them. From these balls, Takahashi chooses one

AtCoder Beginner Contest 155 简要题解

AtCoder Beginner Contest 155 A:签到失败,WA一次. int main() { int a, b, c; cin >> a >> b >> c; if(a == b && b == c) cout << "No"; else if(a == b || a == c || b == c) cout << "Yes"; else cout << &quo

AtCoder Beginner Contest 152 - F - Tree and Constraints (容斥定理+树上路径的性质)

AtCoder Beginner Contest 152 - F - Tree and Constraints (容斥定理+树上路径的性质) We have a tree with NN vertices numbered 11 to NN. The ii-th edge in this tree connects Vertex aiai and Vertex bibi. Consider painting each of these edges white or black. There ar

【ATcoder】AtCoder Beginner Contest 161 题解

题目链接:AtCoder Beginner Contest 161 原版题解链接:传送门 A - ABC Swap 这题太水,直接模拟即可. 1 #include <iostream> 2 using namespace std; 3 int main() { 4 int a, b, c; 5 cin >> a >> b >> c; 6 swap(a, b); 7 swap(a, c); 8 cout << a << " &

AtCoder Beginner Contest 115 题解

题目链接:https://abc115.contest.atcoder.jp/ A Christmas Eve Eve Eve 题目: Time limit : 2sec / Memory limit : 1024MB Score : 100 points Problem Statement In some other world, today is December D-th. Write a program that prints Christmas if D=25, Christmas E

AtCoder Beginner Contest 106 ABCD

A - Garden Problem Statement There is a farm whose length and width are A yard and B yard, respectively. A farmer, John, made a vertical road and a horizontal road inside the farm from one border to another, as shown below: (The gray part represents

AtCoder Beginner Contest 121 题解

题目链接:https://atcoder.jp/contests/abc121 A White Cells 分析:题目数据规模很小,直接暴力修改都可以.或者可以推出公式. 代码: 1 #include <iostream> 2 #include <cstdio> 3 4 using namespace std; 5 6 int main() 7 { 8 int a[25][25] = {0}; 9 int H, W, h, w; 10 scanf("%d %d"