Codeforces Round #533(Div. 2) A.Salem and Sticks
2024-09-30 02:34:22
链接: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;
}
最新文章
- DataGridView实现各种效果
- ajax 跨域请求时url参数添加callback=?会实现跨域问题
- JavaScript 全局对象
- Charles初体验
- 【转】在web 项目使用了ReportViewer时出错
- loj 1011(状态压缩+记忆化搜索)
- 集成 Apple Pay
- css3 绘制优惠券
- 【linux】输出重定向
- access 2007 vba (亖)
- startActivityForResult案例
- salesforce零基础学习(七十六)顺序栈的实现以及应用
- Vue笔记(props和 mounted)
- Java作业五(2017-10-15)
- Lnmp下pureftpd新建FTP账户权限不足解决方法
- C++智能指针 auto_ptr、shared_ptr、weak_ptr和unique_ptr
- cf1088E Ehab and a component choosing problem (树形dp)
- Django实现瀑布流,组合搜索
- 7款开源ERP系统比较
- 浅谈android中只使用一个TextView实现高仿京东,淘宝各种倒计时
热门文章
- Python对象拷贝——深拷贝与浅拷贝
- 【bzoj2286】[Sdoi2011]消耗战
- Gradients渐变属性
- poj3349 Snowflake Snow Snowflakes —— 哈希表
- 查询oracle数据库中的for update 中锁住的table表sql语句
- Jmeter创建一个简单的http接口用例
- struts2 小例子(教训篇)
- java IO流文件的读写具体实例(转载)
- webrtc 学习资源 http://www.cnblogs.com/lingyunhu/p/3578218.html
- SPOJ MAXOR (分块 || 可持久化字典树 || 异或)(好题)