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
3
Sample Output 1 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
10
Sample Output 2 Copy
10
Sample Input 3 Copy
999999999
Sample Output 3 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
4
1 4 6 3
Sample Output 1 Copy
5
The maximum absolute difference of two elements is A3−A1=6−1=5A3−A1=6−1=5.
Sample Input 2 Copy
2
1000000000 1
Sample Output 2 Copy
999999999
Sample Input 3 Copy
5
1 1 1 1 1
Sample Output 3 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
5
2 2 3 5 5
Sample Output 1 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
9
1 2 3 4 5 6 7 8 9
Sample Output 2 Copy
0
Sample Input 3 Copy
6
6 5 4 3 2 1
Sample Output 3 Copy
18
Sample Input 4 Copy
7
1 1 1 1 2 3 4
Sample Output 4 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
5
3 2 4 1 2
Sample Output 1 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
10
10 71 84 33 6 47 23 25 52 64
Sample Output 2 Copy
36
Sample Input 3 Copy
7
1 2 3 1000000000 4 5 6
Sample Output 3 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] = {};
scanf("%d",&n);
for(int i = ;i <= n;i ++) {
scanf("%d",&a[i]);
sum[i] = sum[i - ] + a[i];
}
LL ans = sum[n];
int l = ,r = ;
for(int i = ;i <= n - ;i ++) {
LL last = sum[n];
while(l < i) {
LL d = abs(sum[i] - sum[l] * );
if(d > last) break;
else last = d;
l ++;
}
l --;
last = sum[n];
while(r < n) {
LL d = abs(sum[n] + sum[i] - sum[r] * );
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);
}
AtCoder Beginner Contest 102的更多相关文章
- AtCoder Regular Contest 102
AtCoder Regular Contest 102 C - Triangular Relationship 题意: 给出n,k求有多少个不大于n的三元组,使其中两两数字的和都是k的倍数,数字可以重 ...
- AtCoder Beginner Contest 100 2018/06/16
A - Happy Birthday! Time limit : 2sec / Memory limit : 1000MB Score: 100 points Problem Statement E8 ...
- AtCoder Beginner Contest 052
没看到Beginner,然后就做啊做,发现A,B太简单了...然后想想做完算了..没想到C卡了一下,然后还是做出来了.D的话瞎想了一下,然后感觉也没问题.假装all kill.2333 AtCoder ...
- AtCoder Beginner Contest 053 ABCD题
A - ABC/ARC Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Smeke has ...
- AtCoder Beginner Contest 136
AtCoder Beginner Contest 136 题目链接 A - +-x 直接取\(max\)即可. Code #include <bits/stdc++.h> using na ...
- AtCoder Beginner Contest 137 F
AtCoder Beginner Contest 137 F 数论鬼题(虽然不算特别数论) 希望你在浏览这篇题解前已经知道了费马小定理 利用用费马小定理构造函数\(g(x)=(x-i)^{P-1}\) ...
- AtCoder Beginner Contest 076
A - Rating Goal Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Takaha ...
- AtCoder Beginner Contest 079 D - Wall【Warshall Floyd algorithm】
AtCoder Beginner Contest 079 D - Wall Warshall Floyd 最短路....先枚举 k #include<iostream> #include& ...
- AtCoder Beginner Contest 064 D - Insertion
AtCoder Beginner Contest 064 D - Insertion Problem Statement You are given a string S of length N co ...
随机推荐
- 使用OpenGL进行Mandelbrot集的可视化
Mandelbrot集是哪一集?? Mandelbrot集不是哪一集!! 啊不对-- Mandelbrot集是哪一集!! 好像也不对-- Mandelbrot集是数集!! 所以--他不是一集而是数集? ...
- 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则该路径不能再进入该格子。 例如 a b c e s f c s a d e e 矩阵中包含一条字符串"bccced"的路径,但是矩阵中不包含"abcb"路径,因为字符串的第一个字符b占据了矩阵中
// test20.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...
- LeetCode108_Convert SortedArray to BinarySearchTree(将有序数组转成二叉排序树) Java题解
题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST ...
- Unity3D研究院编辑器之重写Hierarchy的右键菜单
Hierarchy视图中选择一个游戏对象以后通过右键可以打开一个unity默认菜单,一般情况下都可以满足我们,但是我想真对某些特殊的游戏对象而展开特殊的菜单.如下图所示,比如这样: 代码: using ...
- iOSPOI检索详细总结
iOS百度地图路径规划和POI检索详细总结 路径规划.png 百度地图的使用 百度地图API的导入网上说了许多坑,不过我遇到的比较少,这里就放两个比较常见的吧.坑一: 奥联WIFI_xcodeproj ...
- 【BZOJ4966】总统选举 线段树+随机化
[BZOJ4966]总统选举 Description 黑恶势力的反攻计划被小C成功摧毁,黑恶势力只好投降.秋之国的人民解放了,举国欢庆.此时,原秋之国总统因没能守护好国土,申请辞职,并请秋之国人民的大 ...
- EasyNVR无插件IPC摄像机直播方案前端构建之:区分页面是自跳转还是分享依据
区分分享还是跳转 对于前端一些页面的展示,通常有两种方式:通过入口链接一步步进入,或是通过分享链接直接进入:对于这两种方式的区别是什么?在进行前端书写时又应该如何处理? 以EasyNVR为例来进行说明 ...
- 【CISCO强烈推荐】生成树 《路由协议》 卷一二 拥塞:网络延迟 阻塞:进程中 MTU QS:服务质量 OSPF RIP ISIS BGP 生成树 《路由协议》 卷一二
协议 CP/IP路由技术第一卷 作 者 (美)多伊尔,(美)卡罗尔
- 【题解】P4247 [清华集训]序列操作(线段树修改DP)
[题解]P4247 [清华集训]序列操作(线段树修改DP) 一道神仙数据结构(DP)题. 题目大意 给定你一个序列,会区间加和区间变相反数,要你支持查询一段区间内任意选择\(c\)个数乘起来的和.对1 ...
- POJ3984 迷宫问题【水BFS】
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u011775691/article/details/28050277 #include <cs ...