链接:https://codeforces.com/contest/1105/problem/A

题意:

给n个数,找到一个数t使i(1-n)∑|ai-t| 最小。

ai-t 差距1 以内都满足

思路:

暴力,枚举。

代码:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long LL;
  4. const int MAXN = 1000+5;
  5. int a[MAXN];
  6.  
  7. int main()
  8. {
  9. int n;
  10. scanf("%d",&n);
  11. for (int i = 1;i<=n;i++)
  12. scanf("%d",&a[i]);
  13. int t,cost = 1e6;
  14. for (int i = 1;i<=100;i++)
  15. {
  16. int key = i;
  17. int sum = 0;
  18. for (int j = 1;j<=n;j++)
  19. {
  20. if (a[j] < key-1)
  21. sum += abs(a[j] - (key-1));
  22. if (a[j] > key+1)
  23. sum += abs(a[j] - (key+1));
  24. }
  25. if (sum < cost)
  26. {
  27. cost = sum;
  28. t = key;
  29. }
  30. }
  31. printf("%d %d\n",t,cost);
  32.  
  33. return 0;
  34. }

  

Codeforces Round #533(Div. 2) A.Salem and Sticks的更多相关文章

  1. 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 ...

  2. 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 ...

  3. Codeforces Round #533 (Div. 2)题解

    link orz olinr AK Codeforces Round #533 (Div. 2) 中文水平和英文水平都太渣..翻译不准确见谅 T1.给定n<=1000个整数,你需要钦定一个值t, ...

  4. 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  ...

  5. 贪心 Codeforces Round #297 (Div. 2) C. Ilya and Sticks

    题目传送门 /* 题意:给n个棍子,组成的矩形面积和最大,每根棍子可以-1 贪心:排序后,相邻的进行比较,若可以读入x[p++],然后两两相乘相加就可以了 */ #include <cstdio ...

  6. Codeforces Round #533 (Div. 2) Solution

    A. Salem and Sticks 签. #include <bits/stdc++.h> using namespace std; #define N 1010 int n, a[N ...

  7. 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 ...

  8. Codeforces Round #533 (Div. 2) C.思维dp D. 多源BFS

    题目链接:https://codeforces.com/contest/1105 C. Ayoub and Lost Array 题目大意:一个长度为n的数组,数组的元素都在[L,R]之间,并且数组全 ...

  9. 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 ...

随机推荐

  1. weixin报警脚本

    #!/bin/bash ### script name weixin.sh ### send messages from weixin for zabbix monitor ### jack ### ...

  2. html5--3.7 input元素(6)

    html5--3.7 input元素(6) 学习要点 input元素及其属性 input元素 用来设置表单中的内容项,比如输入内容的文本框,按钮等 不仅可以布置在表单中,也可以在表单之外的元素使用 i ...

  3. juery的跨域请求2

    时间过得好快,又被拉回js战场时, 跨域问题这个伤疤又开疼了. 好在,有jquery帮忙,跨域问题似乎没那么难缠了.这次也借此机会对跨域问题来给刨根问底,结合实际的开发项目,查阅了相关资料,算是解决了 ...

  4. C# 获取QQ好友列表信息的实现

    分析部分 当我们访问QQ空间的时候,大家可以在右侧的发现一个这样的统计信息  当点击这个链接的时候,会跳转到  这样一个URL 这个URl可以管理好友,当然也就能读取到好友  上面我们是在浏览器中的操 ...

  5. R文件报错

    res中drawable中的资源文件进行命名时只能用0-9或者a-z或者-,其他的东西不能使用,也禁止在重命名时开头使用大写字母

  6. 运用Eclipse的Working Set,界面清爽多了

    使用Eclipse的Working Set,界面清爽多了 想必大家的Eclipse里也会有这么多得工程...... 每次工作使用到的项目肯定不会太多...... 每次从这么大数量的工程当中找到自己要使 ...

  7. KNN分类算法--python实现

    一.kNN算法分析 K最近邻(k-Nearest Neighbor,KNN)分类算法可以说是最简单的机器学习算法了.它采用测量不同特征值之间的距离方法进行分类.它的思想很简单:如果一个样本在特征空间中 ...

  8. u3d shader学习笔记1

    促使我学习SHADER的重要原因是希望深入理解3D渲染的机制,在此基础上可以灵活达到某种效果与性能的平衡,开发出具有良好体验的VR应用. 因为VR应用体验的好坏,直接由游戏的帧率决定,而游戏的帧率则受 ...

  9. 共相式GIS

    今天看到SuperMap中一直提到共相式GIS,于是乎搜索一下……SuperMap的共相式怎么理解呢?iServer Java有咋理解呢??? 再谈共相式GIS和ArcObjects:ttp://ww ...

  10. 精通BitmapData

    一.构造方法: var bmd:BitmapData=new BitmapData(200,100,true,0xffff0000); 参数说明: 1.宽 2.高 3.是否使用32位填充位图(包括al ...