链接: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;
}

  

最新文章

  1. DataGridView实现各种效果
  2. ajax 跨域请求时url参数添加callback=?会实现跨域问题
  3. JavaScript 全局对象
  4. Charles初体验
  5. 【转】在web 项目使用了ReportViewer时出错
  6. loj 1011(状态压缩+记忆化搜索)
  7. 集成 Apple Pay
  8. css3 绘制优惠券
  9. 【linux】输出重定向
  10. access 2007 vba (亖)
  11. startActivityForResult案例
  12. salesforce零基础学习(七十六)顺序栈的实现以及应用
  13. Vue笔记(props和 mounted)
  14. Java作业五(2017-10-15)
  15. Lnmp下pureftpd新建FTP账户权限不足解决方法
  16. C++智能指针 auto_ptr、shared_ptr、weak_ptr和unique_ptr
  17. cf1088E Ehab and a component choosing problem (树形dp)
  18. Django实现瀑布流,组合搜索
  19. 7款开源ERP系统比较
  20. 浅谈android中只使用一个TextView实现高仿京东,淘宝各种倒计时

热门文章

  1. Python对象拷贝——深拷贝与浅拷贝
  2. 【bzoj2286】[Sdoi2011]消耗战
  3. Gradients渐变属性
  4. poj3349 Snowflake Snow Snowflakes —— 哈希表
  5. 查询oracle数据库中的for update 中锁住的table表sql语句
  6. Jmeter创建一个简单的http接口用例
  7. struts2 小例子(教训篇)
  8. java IO流文件的读写具体实例(转载)
  9. webrtc 学习资源 http://www.cnblogs.com/lingyunhu/p/3578218.html
  10. SPOJ MAXOR (分块 || 可持久化字典树 || 异或)(好题)