Codeforces 439D Devu and his Brother(排序)

题目链接:Codeforces 439D  Devu and his Brother

题目大意:Devu和他的哥哥互相深爱着对方,我确信他们是搞基的,为此我还去查了一下Devu是男人名还是女人名,但是后来发现His Brother,所以可以证明,他们就是搞基的。题目很简单,父亲给了他们两个人分别一个数组。但是Devu希望自己最小的数都可以不必哥哥最大的数小,现在对数组中的数有两种操作,一种是加1,一种是减1,问最少进行几次操作可以使得两个数组满足要求。

解题思路:将两个数组合并在一起,然后排序,第m个就是交界值。

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
typedef long long ll;
const int N = 1e5+5;

int n, m;
ll a[N], b[N], c[N*2];

inline ll max(ll a, ll b) {
    return a > b ? a : b;
}

int main () {
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; i++) {
        scanf("%lld", &a[i]);
        c[i] = a[i];
    }

    for (int i = 0; i < m; i++) {
        scanf("%lld", &b[i]);
        c[i+n] = b[i];
    }

    sort(c, c + n + m);

    ll tmp = c[m], ans = 0;

    for (int i = 0; i < n; i++)
        ans += max(0, tmp - a[i]);

    for (int i = 0; i < m; i++)
        ans += max(0, b[i] - tmp);

    printf("%lld\n", ans);
    return 0;
}

Codeforces 439D Devu and his Brother(排序)

时间: 2024-07-31 20:51:40

Codeforces 439D Devu and his Brother(排序)的相关文章

Codeforces 439D Devu and his Brother 三分

题目链接:点击打开链接 = - =曾经的三分姿势不对竟然没有被卡掉,,,太逗.. #include<iostream> #include<string> #include<algorithm> #include<cstdlib> #include<cstdio> #include<set> #include<map> #include<vector> #include<cstring> #inclu

codeforces 439D Devu and Partitioning of the Array(有深度的模拟)

题目 //参考了网上的代码 注意答案可能超过32位 //要达成目标,就是要所有数列a的都比数列b的要小或者等于 //然后,要使最小的要和最大的一样大,就要移动(大-小)步, //要使较小的要和较大的一样大,也是要移动(较大-较小)步 //然后都加在一起就好了 #include<iostream> #include<algorithm> #include<stdio.h> #include<string.h> using namespace std; #def

Codeforces 439C Devu and Partitioning of the Array(模拟)

题目链接:Codeforces 439C Devu and Partitioning of the Array 题目大意:给出n个数,要分成k份,每份有若干个数,但是只需要关注该份的和为奇数还是偶数,要求偶数堆的个数为p.输出方案. 解题思路:首先先将数组按照奇偶排序,也可以分开储存.然后先单独分k-p个奇数,然后后面的就将两个奇数当一个偶数分配,分配过程中计算是否满足,比如说奇数是否成对,以及是否分成了k堆. #include <cstdio> #include <cstring>

Codeforces 439E Devu and Birthday Celebration(计数问题)

题目链接:Codeforces 439E Devu and Birthday Celebration 题目大意:给出q,表示询问的次数,每次询问有n和f,问有多少种分类方法,将n分成f份,并且这f份的最大共约数为1. 解题思路:如果不考虑说最大共约数为1的话,那么问题很简单,就是f个数的和为n的种数C(f?1n?1).所以我们就尽量将问题转化成说f数的和为s的子问题.用容斥原理,总的可能减去公约数不为1的情况,那么公约数不为1的情况就去枚举公约数. #include <cstdio> #inc

codeforces 451E Devu and Flowers

题意:有n个瓶子每个瓶子有 f[i] 支相同的颜色的花(不同瓶子颜色不同,相同瓶子花视为相同) 问要取出s支花有多少种不同方案. 思路: 如果每个瓶子的花有无穷多.那么这个问题可以转化为  s支花分到n个瓶子有多少种方案  用隔板法就能解决 C(s+n-1,n-1) .有限制之后我们可以 用 没限制的去减掉那些违反限制的 如果只有一个瓶子取得花超出上限 那么减去,两个瓶子 要加上(容斥原理) n只有20  就能暴力枚举那些取超过上限f[i]的瓶子并且在这些瓶子至少选出 f[i]+1 支花  统计

Codeforces 451E Devu and Flowers(容斥原理)

题目链接:Codeforces 451E Devu and Flowers 题目大意:有n个花坛.要选s支花,每一个花坛有f[i]支花.同一个花坛的花颜色同样,不同花坛的花颜色不同,问说能够有多少种组合. 解题思路:2n的状态,枚举说那些花坛的花取超过了,剩下的用C(n?1sum+n?1)隔板法计算个数.注意奇数的位置要用减的.偶数的位置用加的.容斥原理. #include <cstdio> #include <cstring> #include <cmath> #in

codeforces 439C Devu and Partitioning of the Array(烦死人的多情况的模拟)

题目 //这是一道有n多情况的烦死人的让我错了n遍的模拟题 #include<iostream> #include<algorithm> #include<stdio.h> #include<string.h> using namespace std; int a[100010],n,k,p; int vis[100010]; int ji=0,ou=0; int main() { memset(vis,0,sizeof(vis)); cin >>

codeforces Gym 100500C C. ICPC Giveaways 排序

Problem C. ICPC GiveawaysTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/attachments Description During the preparation for the ICPC contest, the organizers prepare bags full of giveaways for the contestants. Each bag us

codeforces 479B. Towers C - Exams【排序】

题目:codeforces 479B. Towers 题意:给出一个长度 n 的序列,最多可以有 k 次操作,每次选择一个一个最大的减1,最小的加1,然后问在最多k次操作之后的最小差值. 很简单,练习一下python语法 n,k = map(int,raw_input().split()) l = map(int,raw_input().split()) ans = [] i = 0 while i<k: if(max(l) - min(l)>0): #取最大值 a,b = l.index(m