icpc 2019 word final A题 思路
题意
两排物品摆放,每个物品有价格和高度两个属性
要求:
- each前排高度小于后排
- 行内价格从左到右不下降
solution
首先每行按价格排序
每行将价格相同的放进buffer
。
固定buffer.size()
小的那个然后用另一个buffer
贪心的取同等价格可以取的高度里最大的。
buffer
空了适时补充
“能犯的错误都犯了一遍”
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 7;
struct Node{
int idx, price, height;
Node (){}
Node (int id, int p, int h):idx(id), price(p), height(h){}
friend bool operator < (const Node &a,const Node &b){
return a.height < b.height;
}
void print(){
printf("Node %d: p = %d, h = %d\n", idx, price, height);
}
}fro[N], bac[N];
bool cmp (const Node &a,const Node &b){
return a.price < b.price;
}
int n;
multiset <Node> fro_buffer, bac_buffer;
multiset <Node> :: iterator it;
vector <int> ans_fro, ans_bac;
void add_buffer(multiset <Node> &buffer, Node* arr, int &idx){
//printf("add_buffer:\n");
for (;idx <= n;){
buffer.insert(arr[idx++]);
//printf("\tadd(idx = %d) size = %d: ", idx-1, buffer.size()); arr[idx-1].print();
if (arr[idx].price != arr[idx-1].price) return;
}
}
bool fro_find_bac(){
Node x = *fro_buffer.begin();
ans_fro.push_back(x.idx);
fro_buffer.erase(fro_buffer.begin());
it = bac_buffer.upper_bound(x);
if (it == bac_buffer.end()) return false;
//printf("Back %d: p = %d, h = %d\n", it->idx, it->price, it->height); x.print();
ans_bac.push_back(it->idx);
bac_buffer.erase(it);
return true;
}
bool bac_find_fro(){
Node x = *bac_buffer.begin();
ans_bac.push_back(x.idx);
bac_buffer.erase(bac_buffer.begin());
it = fro_buffer.lower_bound(x);
if (it == fro_buffer.begin()) return false;
it = prev(it);
//x.print(); printf("Front%d: p = %d, h = %d\n", it->idx, it->price, it->height);
ans_fro.push_back(it->idx);
fro_buffer.erase(it);
return true;
}
bool work(){
fro_buffer.clear();
bac_buffer.clear();
int fro_idx = 1, bac_idx = 1;
ans_fro.clear();
ans_bac.clear();
for (int i = 1; i <= n; i++){
//printf("size: %d : %d\n", bac_buffer.size(), fro_buffer.size());
if (bac_buffer.empty()){
add_buffer(bac_buffer, bac, bac_idx);
}
if (fro_buffer.empty()){
add_buffer(fro_buffer, fro, fro_idx);
}
//printf("size: %d : %d\n", bac_buffer.size(), fro_buffer.size());
if (fro_buffer.size() < bac_buffer.size()){
if (!fro_find_bac()) return false;
}else{
if (!bac_find_fro()) return false;
}
}
return true;
}
int main(){
//freopen("in.txt", "r", stdin);
for (; ~scanf("%d", &n);){
for (int i = 1; i <= n; i++) fro[i].idx = bac[i].idx = i;
for (int i = 1; i <= n; i++) scanf("%d", &bac[i].price);
for (int i = 1; i <= n; i++) scanf("%d", &bac[i].height);
for (int i = 1; i <= n; i++) scanf("%d", &fro[i].price);
for (int i = 1; i <= n; i++) scanf("%d", &fro[i].height);
sort(fro + 1, fro + n + 1, cmp);
sort(bac + 1, bac + n + 1, cmp);
if (!work()){
puts("impossible");
}else{
for (int i = 0; i < n; i++){
printf("%d%c", ans_bac[i], i==n-1?'\n':' ');
}
for (int i = 0; i < n; i++){
printf("%d%c", ans_fro[i], i==n-1?'\n':' ');
}
}
}
return 0;
}
kblack 牛逼