牛客练习赛7 B购物

题意:


思路:

贪心,

题意换一种理解方式就是:每天要选一个,这一个可以从包括这一天的之前的任意一天选(选过的不能选),再附加考虑 k² 这个条件,每次选花费最小的一个


#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PI acos(-1.0)
#define in freopen("in.txt", "r", stdin)
#define out freopen("out.txt", "w", stdout)
#define kuaidian ios::sync_with_stdio(0);
 
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 300 + 7, maxd = 1e8;
const ll mod = 1e9 + 7;
const int INF = 0x7f7f7f7f;
 
 
 
int n, m;
int a[maxn][maxn];
int k[maxn];
bool vis[maxn][maxn];
 
 
void init() {
    k[0] = 0;
    for(int i = 1; i < maxn; ++i) {
        k[i] = i*i - (i-1)*(i-1);
        //cout << k[i] << endl;
    }
    scanf("%d %d", &n, &m);
    for(int i = 0; i < n; ++i) {
        for(int j = 0; j < m; ++j)
            scanf("%d", &a[i][j]);
        sort(a[i], a[i]+m);
    }
}
 
int work(int i) {
    int ret = INF;
    for(int j = 0; j < m; ++j) {
        if(vis[i][j] == 0) {
            ret = (a[i][j]+k[j+1]);
            break;
        }
    }
    return ret;
}
 
void solve() {
    memset(vis, 0, sizeof vis);
    int ans = 0;
    for(int i = 0; i < n; ++i) {
        int id = i, t = INF;
 
        for(int j = 0; j <= i; ++j) {
            int tt = work(j);
            if(t > tt) {
                id = j;
                t = tt;
            }
        }
        int j = 0;
        while(vis[id][j]) j++;
        vis[id][j] = 1;
        ans += t;
        //cout << id << '+' << endl;
        //cout << t << " === " << endl;
    }
    cout << ans << endl;
}
 
int main() {
    init();
    solve();
    return 0;
}


你可能感兴趣的:(贪心)