P1031 均分纸牌(思维)

题目链接

解题思路

  在最优解的情况下,一堆纸牌肯定是最多只能经过一次的,这样,我们不需要贪心的如何移动才能最优,只需在一堆纸牌只经过一次的情况下需要传递的纸牌数。设(x_i)为第(i)堆纸牌需要传递的牌数,然后这里我们以最左边的一堆做起点,那么对于第(i)堆,它所需要移动的牌数就是前面移动过来的牌数加上现有的牌数与平均值之差。这里(x_i)的正负表示需要向右传递或者需要向左传递,而不需要传递的时候就是0。

代码

#include<set>
#include<map>
#include<list>
#include<cmath>
#include<stack>
#include<queue>
#include<cstdio>
#include<cctype>
#include<string>
#include<vector>
#include<climits>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<sstream>
#include<algorithm>
#define endl '
'
#define rtl rt<<1
#define rtr rt<<1|1
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define maxx(a, b) (a > b ? a : b)
#define minn(a, b) (a < b ? a : b)
#define zero(a) memset(a, 0, sizeof(a))
#define INF(a) memset(a, 0x3f, sizeof(a))
#define NIL(a) memset(a, -1, sizeof(a))
#define IOS ios::sync_with_stdio(false)
#define _test printf("============
")
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> P2;
const double pi = acos(-1.0);
const double eps = 1e-7;
const ll MOD = 19491001;
const int INF = 0x3f3f3f3f;
template<typename T> void read(T &x){
    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
    while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
const int maxn = 1e5+10;
int arr[maxn], x[maxn];
int main() {
    int n, ave = 0; cin >> n;
    for (int i = 1; i<=n; ++i) {
        cin >> arr[i];
        ave += arr[i];
    }
    ave /= n;
    int ans = 0, tmp = 0;
    for (int i = 1; i<=n; ++i) {
        x[i] = x[i-1]+arr[i]-ave;
        if (x[i]) ++ans;
    }
    printf("%d
", ans);
    return 0;
}
原文地址:https://www.cnblogs.com/shuitiangong/p/13130227.html