acm-中位数问题-acwing(货仓选址、均分纸牌、糖果传递、七夕祭)

中位数问题

acwing 104 货仓选址

思路

取坐标的中位数,作为仓库的位置。

证明

IMG_0235

代码
#include <bits/stdc++.h>
#define endl "\n"

using namespace std;

const int maxn = 100010;
int a[maxn];
int res;

int main(){
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int n; cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
    }

    sort(a + 1, a + 1 + n);

    int mid = 1 + n >> 1;

    for(int i = 1; i <= n; i++){
        res += abs(a[mid] - a[i]);
    }

    cout << res << endl;
    
    return 0;
}

acwing 1536 均分纸牌

思路

如果有解 -> 总牌数能被整除

从最左边开始

若第一堆牌的数量不足平均数 -> 向第二堆借相应的牌数,直到平均数

若第一堆牌的数量大于平均数 -> 给第二堆牌相应的牌数,直到平均数

以此类推,直到最后一堆牌, 其数量也一定是平均数

代码
#include <bits/stdc++.h>
#define endl "\n"

using namespace std;

const int maxn = 110;
int n, sum, ans;
int a[maxn];

int main(){
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin >> n;
    
    for(int i = 1; i <= n; i++){
        cin >> a[i];
        sum += a[i];
    }

    int avg = sum / n;

    for(int i = 1; i <= n; i++){
        if(a[i] != avg) a[i + 1] += a[i] - avg, ans ++;
    }
    
    cout << ans << endl;
    return 0;
}

acwing 1208 翻硬币 (非中位数问题,但与上题贪心思想类似)

思路

对一个字符串,从前向后遍历。如有不一致,则将该位置与该位置之后以为的字符反转。

代码
#include <bits/stdc++.h>
#define endl "\n"

using namespace std;

string s1, s2;
int cnt;

char change(char s){
    if(s == 'o') return '*';
    else return 'o';
}

int main(){
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

    cin >> s1 >> s2;

    for(int i = 0; i < s1.length(); i++){
        if(s1[i] != s2[i]){
            cnt ++;
            s1[i] = change(s1[i]);
            s1[i + 1] = change(s1[i + 1]);
        }
    }
    
    cout << cnt;

    return 0;
}

acwing 122 糖果传递

思路 + 证明

IMG_0237

代码
#include <bits/stdc++.h>
#define endl "\n"
typedef long long ll;
using namespace std;
const int maxn = 1e6 + 10;

ll a[maxn], s[maxn], c[maxn];
int n, ans;
ll sum;

int main(){
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
        s[i] = s[i - 1] + a[i];
    }

    ll avg = s[n] / n;

    for(int i = 1; i <= n; i++){
        c[i - 1] = i * avg - s[i];
    }


    nth_element(c, c + n / 2, c + n);
    ll res = 0;
    for(int i = 0; i < n; i++){
        res += abs(c[i] - c[n / 2]);
    }
    
    cout << res;
    return 0;
}

acwing 105 七夕祭

思路

对于每一行的元素,我们可以求出他们的行和。而在行和中首尾元素相连,这就转化为糖果传递问题。

又因为,我们一次移动两个数,仅仅会影响其单一行或单一列的目标,而不会同时影响一行和一列,所以我们单独处理两个。如果合法,那么就两个答案加起来,如果单个合法,那么就处理单个就好了。

代码
#include <bits/stdc++.h>
#define endl "\n"
typedef long long ll;

using namespace std;

const int maxn = 1e5 + 10;
int n, m;
int row[maxn], col[maxn], s[maxn], c[maxn];

ll work(int a[], int n){ //环形均分糖果问题
    for(int i = 1; i <= n; i++){
        s[i] = s[i - 1] + a[i];
    }

    if(s[n] % n) return -1;

    int avg = s[n] / n;

    c[1] = 0;
    for(int i = 2; i <= n; i++){
        c[i] = s[i - 1] - (i - 1) * avg;
    }

    sort(c + 1, c + n + 1);
    ll res = 0;
    for(int i = 1; i <= n; i++) res += abs(c[i] - c[(n + 1) / 2]);

    return res;
}

int main(){
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    
    int t; cin >> n >> m >> t;
    
    while(t--){
        int x, y; cin >> x >> y;
        row[x] ++, col[y] ++;
    }

    ll r = work(row, n);
    ll c = work(col, m);

    if(r != -1 && c != -1) cout << "both " << r + c;
    else if(r != -1) cout << "row " << r;
    else if(c != -1) cout << "column " << c;
    else cout << "impossible";

    return 0;
}

acwing 106 动态中位数

思路

IMG_0239

代码
#include <bits/stdc++.h>
#define endl "\n"
using namespace std;

int main(){
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    
    int t; cin >> t;
    while(t--){
        int m, n; cin >> m >> n;
        cout << m << " " << (n + 1) / 2  << endl;

        priority_queue<int> down;
        priority_queue<int, vector<int>, greater<int>> up;

        int cnt = 0;
        for(int i = 1; i <= n; i++){
            int x; cin >> x;
            if(down.empty() || x <= down.top()) down.push(x);
            else up.push(x);

            if(down.size() > up.size() + 1) up.push(down.top()), down.pop();
            if(up.size() > down.size()) down.push(up.top()), up.pop();

            if(i % 2){
                cout << down.top() << " ";
                if( ++ cnt % 10 == 0) cout << endl;
            }
        }
        if(cnt % 10) cout << endl;
    }

    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wzx_1210

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值