【Codeforces Global Round 14】-C. Phoenix and Towers-堆模拟
作者:互联网
题目:
思路:
这个题很像之前做过一个向篮子里面丢方块看最少能丢几层的题
我们分析样例模拟入塔过程
每次放进去一个块都想让所有塔的极差在一定范围内
所以我们建一个堆存放所有的塔高
走h数组
每次把当前方块丢到最低的塔中来稳固极差并记录当前丢到了哪个塔里面
如果丢进去极差>k,就NO
否则走完后按顺序输出每个方块丢到了哪个塔
代码:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <unordered_map>
#include <stack>
#include <string>
#include <vector>
#define IOS ios::sync_with_stdio(false)
#define rep1(i, a, n) for (int i = a; i <= n; i++)
#define rep2(i, a, n) for (int i = a; i >= n; i--)
#define each_cass(cass) for(cin >> cass; cass; cass--)
#define lowbit(x) (x&-x)
#define mm(a, b) memset(a, b, sizeof(a))
#define SP system("pause")
#define Chivas int main()
#define gcd(a,b) __gcd(a,b)
#define Regal return 0
typedef long long ll;
const int INF = 0x7FFFFFFF;
const double G = 10;
const double eps = 1e-6;
const double PI = acos(-1.0);
using namespace std;
struct node{
ll sum;//塔高
ll id;//塔编号
friend bool operator < (node a,node b){
return a.sum > b.sum;
}
};
inline void solve(){
priority_queue<node> pq;//存塔
ll n, m, k;
map<ll, ll> mp;//存每个块对应的塔的编号
cin >> n >> m >> k;
for(int i = 0; i < m; i ++) pq.push({0, i + 1});//先给每个塔的sum初始化为0
for(int i = 0; i < n; i ++){
ll x; cin >> x;
node cur = pq.top();//每次往塔高最小的填块
pq.pop();
cur.sum += x;
ll cursum = cur.sum;//当前sum
ll minsum = pq.top().sum;//塔高最小的塔的sum
mp[i] = cur.id;//对应块编号为塔高最小的塔的id
pq.push(cur);//存进去
if(cursum > minsum + k){//如果极差超过了就不行
cout << "NO" << endl;
return;
}
}
cout << "YES" << endl;
for(int i = 0; i < n; i ++){
cout << mp[i] << " ";//输出每个块对应的塔的编号
}
cout << endl;
}
Chivas{
IOS;
int cass;
each_cass(cass){
solve();
}
Regal;
}
标签:pq,14,Phoenix,Towers,sum,cass,include,ll,define 来源: https://blog.csdn.net/SnopzYz/article/details/116375757