【链接】 我是链接,点我呀:)

【题意】

在这里输入题意

【题解】

将数组排序一下。
考虑中位数a[mid]
如果a[mid]==s直接输出0
如果a[mid]s,那么我们把a[mid]还是改成s,然后把1..mid-1这里面比s大的都改成s.这样就能满足要求了。(mid+1..n这一部分都是
大于a[mid]的不用动) (同理也不会改a[mid+1..n]的,因为改动的花费更多

【代码】

  1. #include <bits/stdc++.h>
  2. #define LL long long
  3. #define rep1(i,a,b) for (int i = a;i <= b;i++)
  4. #define rep2(i,a,b) for (int i = a;i >= b;i--)
  5. #define all(x) x.begin(),x.end()
  6. #define pb push_back
  7. #define lson l,mid,rt<<1
  8. #define ri(x) scanf("%d",&x)
  9. #define rl(x) scanf("%lld",&x)
  10. #define rs(x) scanf("%s",x)
  11. #define rson mid+1,r,rt<<1|1
  12. using namespace std;
  13. const double pi = acos(-1);
  14. const int dx[4] = {0,0,1,-1};
  15. const int dy[4] = {1,-1,0,0};
  16. const int N = 2e5;
  17. int n,s,cnt1,cnt2;
  18. int a[N+10];
  19. vector<int> v[2];
  20. int main(){
  21. #ifdef LOCAL_DEFINE
  22. freopen("rush_in.txt", "r", stdin);
  23. #endif
  24. scanf("%d%d",&n,&s);
  25. rep1(i,1,n) scanf("%d",&a[i]);
  26. sort(a+1,a+1+n);
  27. int key = n/2;
  28. key++;
  29. if (a[key]==s){
  30. puts("0");
  31. }else{
  32. long long ans = 0;
  33. if (a[key]<s){
  34. rep1(i,key,n){
  35. if (a[i]<s){
  36. ans += s-a[i];
  37. }
  38. }
  39. }else{
  40. //a[key]>s
  41. rep2(i,key,1){
  42. if (a[i]>s){
  43. ans += a[i]-s;
  44. }
  45. }
  46. }
  47. printf("%lld\n",ans);
  48. }
  49. return 0;
  50. }

【Manthan, Codefest 18 (rated, Div. 1 + Div. 2) B】Reach Median的更多相关文章

  1. 【Manthan, Codefest 18 (rated, Div. 1 + Div. 2) C】Equalize

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] Swap操作显然只能对(i-1,i)执行才有用. 不然直接将i翻转以及j翻转 显然比直接交换更优. 那么现在我们就相当于有两种操作. ...

  2. 【Manthan, Codefest 18 (rated, Div. 1 + Div. 2) A】Packets

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 多重背包的二进制优化. 就是将数量x分成接近log2x份 然后这log2x份能组合成1..x内的所有数字. 从而将多重背包转化成01 ...

  3. Manthan, Codefest 18 (rated, Div. 1 + Div. 2) C D

    C - Equalize #include<bits/stdc++.h> using namespace std; using namespace std; string a,b; int ...

  4. 题解——CF Manthan, Codefest 18 (rated, Div. 1 + Div. 2) T5(思维)

    还是dfs? 好像自己写的有锅 过不去 看了题解修改了才过qwq #include <cstdio> #include <algorithm> #include <cst ...

  5. 题解——CF Manthan, Codefest 18 (rated, Div. 1 + Div. 2) T4(模拟)

    随便模拟下就过了qwq 然后忘了特判WA了QwQ #include <cstdio> #include <algorithm> #include <cstring> ...

  6. 题解——CF Manthan, Codefest 18 (rated, Div. 1 + Div. 2) T3(贪心)

    是一道水题 虽然看起来像是DP,但其实是贪心 扫一遍就A了 QwQ #include <cstdio> #include <algorithm> #include <cs ...

  7. 题解——CF Manthan, Codefest 18 (rated, Div. 1 + Div. 2) T2(模拟)

    题目要求很简单,做法很粗暴 直接扫一遍即可 注意结果会爆int #include <cstdio> #include <algorithm> #include <cstr ...

  8. 题解——CF Manthan, Codefest 18 (rated, Div. 1 + Div. 2) T1(找规律)

    就是找一下规律 但是奈何昨天晚上脑子抽 推错了一项QwQ 然后重新一想 A掉了QwQ #include <cstdio> #include <algorithm> #inclu ...

  9. Codeforces Manthan, Codefest 18 (rated, Div. 1 + Div. 2) D,E

    D. Valid BFS? time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

随机推荐

  1. 相似 nginx 编译时生成函数链表

    下面代码可能须要一定的c/c++基础. 须要有一些函数指针的知识 深度剖析函数指针点击这里 common.h #pragma once typedef int (*pt)(void); void in ...

  2. 2016.04.22,英语,《Vocabulary Builder》Unit 17

    anim, comes from the Latin anima, meaning 'breath' or 'soul'. animism: ['ænɪmɪzəm] n. 泛灵论,精神存在论,神创宇宙 ...

  3. WINDOWS XP 系统显示乱码的解决方法(修改注册表,使用正常字体)

            一位同事的计算机进入WINDOWS XP系统后,电脑里的所有汉字全部显示乱码,很多办公文档无法打开而影响工作.因为第一次遇到这种问题,当然首先是百度解决了,搜索了相关的信息后找到了答案 ...

  4. I NEED A OFFER!(hdoj--1203--01背包)

    I NEED A OFFER! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  5. 委托delegate,Action,Func,Predicate

    C#委托的介绍(delegate.Action.Func.predicate) 委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递.事件是一种特殊的委托. 1.委托的声明 ...

  6. [luogu普及] ---P1032 字串变换

    目的 刷完100AC (最近很不舒服,写博客耗时啊 记录第一个字符串的题目 参考 https://www.luogu.org/blog/user20197/solution-p1032 代码 #inc ...

  7. IEnumerable ICollection IList

  8. sql server 创建索引 超时时间已到

    如下图所示:在现场PR_Product表中添加绯聚焦索引PSCode,点击保存按钮后等了一段时间弹出超时警告!现场这张表的数据量也是特别大的(250+万),但是我本地也是把现场数据库还原了的,一样的数 ...

  9. js原生_获取url键值对

    思路: 1.先对url进行处理,获取 ?后的字符串 postid=10457794&actiontip=保存修改成功') 2. 字符串通过&标识,不同参数转为数组 ["pos ...

  10. 【Oracle】DG中 Switchover 主、备切换

    操作系统:OEL 5.6 数据库版本:Oracle11gR2  11.2.0.4.0 Switchover切换要求主库和备库在数据同步情况下进行,是主备之间的正常切换,主要用于日常维护.灾备演练等.切 ...