Codeforces Round #533(Div. 2) A.Salem and Sticks
链接:https://codeforces.com/contest/1105/problem/A
题意:
给n个数,找到一个数t使i(1-n)∑|ai-t| 最小。
ai-t 差距1 以内都满足
思路:
暴力,枚举。
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 1000+5;
int a[MAXN]; int main()
{
int n;
scanf("%d",&n);
for (int i = 1;i<=n;i++)
scanf("%d",&a[i]);
int t,cost = 1e6;
for (int i = 1;i<=100;i++)
{
int key = i;
int sum = 0;
for (int j = 1;j<=n;j++)
{
if (a[j] < key-1)
sum += abs(a[j] - (key-1));
if (a[j] > key+1)
sum += abs(a[j] - (key+1));
}
if (sum < cost)
{
cost = sum;
t = key;
}
}
printf("%d %d\n",t,cost); return 0;
}
Codeforces Round #533(Div. 2) A.Salem and Sticks的更多相关文章
- Codeforces Round #533 (Div. 2) A. Salem and Sticks(暴力)
A. Salem and Sticks time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #533 (Div. 2) A. Salem and Sticks(枚举)
#include <bits/stdc++.h> using namespace std; int main() { int n;cin>>n; int a[n];for(in ...
- Codeforces Round #533 (Div. 2)题解
link orz olinr AK Codeforces Round #533 (Div. 2) 中文水平和英文水平都太渣..翻译不准确见谅 T1.给定n<=1000个整数,你需要钦定一个值t, ...
- Codeforces Round #297 (Div. 2)C. Ilya and Sticks 贪心
Codeforces Round #297 (Div. 2)C. Ilya and Sticks Time Limit: 2 Sec Memory Limit: 256 MBSubmit: xxx ...
- 贪心 Codeforces Round #297 (Div. 2) C. Ilya and Sticks
题目传送门 /* 题意:给n个棍子,组成的矩形面积和最大,每根棍子可以-1 贪心:排序后,相邻的进行比较,若可以读入x[p++],然后两两相乘相加就可以了 */ #include <cstdio ...
- Codeforces Round #533 (Div. 2) Solution
A. Salem and Sticks 签. #include <bits/stdc++.h> using namespace std; #define N 1010 int n, a[N ...
- Codeforces Round #533 (Div. 2) 部分题解A~D
A. Salem and Sticks 题目描述 Salem gave you n n n sticks with integer positive lengths a1,a2,…,an a_1, a ...
- Codeforces Round #533 (Div. 2) C.思维dp D. 多源BFS
题目链接:https://codeforces.com/contest/1105 C. Ayoub and Lost Array 题目大意:一个长度为n的数组,数组的元素都在[L,R]之间,并且数组全 ...
- Codeforces Round #533 (Div. 2) C. Ayoub and Lost Array 【dp】
传送门:http://codeforces.com/contest/1105/problem/C C. Ayoub and Lost Array time limit per test 1 secon ...
随机推荐
- beego 导入一个普通的包都会执行init方法,如果是struct就不会执行
default.go package controllers import ( "beego-test/models" "beego-test/service" ...
- avf_showspectrum.c:112: undefined reference to `av_rdft_end
下面还有一堆错,是由于ffmpeg库没编好,重新编好即可
- datagrid 行号问题综合
1.datagrid 左侧行号设置宽度 : 到 easyui.css 中修改 .datagrid-cell-rownumber 中 width 的宽度.
- linux安装netcat 运行udp服务器
liunx下安装netcat 1.下载安装包 wget https://sourceforge.net/projects/netcat/files/netcat/0.7.1/netcat-0.7.1. ...
- hdu-3018 Ant Trip(欧拉路径)
题目链接: Ant Trip Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Python ip与数字的转换方式
例子:IP:192.168.1.10 方法一: In [1]: bin(192)Out[1]: '0b11000000' In [2]: bin(168)Out[2]: '0b10101000' In ...
- jquery跨域3
这两天用 Jquery 跨域取数据的时候,经常碰到 invalid label 这个错误,十分的郁闷,老是取不到服务器端发送回来的 json 值, 一般跨域用到的两个方法为:$.ajax 和$.get ...
- Windows_Program_Via_C_Translate_Win32编程的背景知识/基础知识_包括基本输入输出机制介绍
Some Basic Background Story of The Win32 APIs Win32 API背景故事/背景知识 The Win32 application programming i ...
- 【旧文章搬运】关于NtUserBuildHwndList的一点记录~
原文发表于百度空间,2011-04-07========================================================================== 该函数与r ...
- 用于生成交易统计时间戳(常配合echarts走势图使用)
<?php //获取交易统计时间戳 时间段内每小时 public function getPayCountTimeHours($start_date,$end_date){ $data = ar ...