#include<bits/stdc++.h>
using namespace std;constint N =1e5+10;int n, cnt, a[N];voiddown(int u){int maxx = u;if(u *2<= cnt && a[u *2]< a[maxx]) maxx = u *2;if(u *2+1<= cnt && a[u *2+1]< a[maxx]) maxx = u *2+1;if(maxx != u){swap(a[maxx], a[u]);down(maxx);}}intmain(){
cin >> n ;
cnt = n;for(int i =1; i <= n; i ++) cin >> a[i];for(int i = n /2; i >0; i --)down(i);for(int i =1; i <= n; i ++){
cout << a[1]<<' ';swap(a[1], a[cnt --]);down(1);}return0;}
谁考了第k名
#include<bits/stdc++.h>
using namespace std;int n, k;structinfo{
string s;double scr;}a[105];voidup(int u){if(u ==1)return;if(a[u].scr > a[u >>1].scr){swap(a[u], a[u >>1]);up(u >>1);}}voiddown(int u){int maxx = u;if((u <<1)<= n && a[u <<1].scr > a[maxx].scr) maxx =(u <<1);if((u <<1|1)<= n && a[u <<1|1].scr > a[maxx].scr) maxx =(u <<1|1);if(maxx != u){swap(a[u], a[maxx]);down(maxx);}}intmain(){
cin >> n >> k;for(int i =1; i <= n; i ++){
cin >> a[i].s >> a[i].scr;up(i);}for(int i =1; i < k; i ++){swap(a[1], a[n --]);down(1);}
cout << a[1].s <<' '<< a[1].scr << endl;return0;}
#include<bits/stdc++.h>
using namespace std;constint N =4e5+10;longlong n, a[N], b[N];structinfo{int i, j;longlong val;};//如果返回真,那么结构体 x > y
bool operator >(info x, info y){return x.val > y.val;}
priority_queue <info, vector<info>, greater<info>> q;intmain(){scanf("%lld",&n);for(int i =1; i <= n; i ++)scanf("%lld",&a[i]);for(int i =1; i <= n; i ++)scanf("%lld",&b[i]);for(int i =1; i <= n; i ++){//把a数组的第1个,b数组的第i个放入优先队列
q.push((info){1, i, a[1]+ b[i]});}for(int k =1; k <= n; k ++){
info t = q.top();
q.pop();printf("%lld\n", t.val);//堆顶元素是a[t.i] + b[t.j],//因为:a[t.i] <= a[t.i + 1], //所以:a[t.i] + b[t.j] <= a[t.i + 1] + b[t.j], //所以接下来替代a[t.i] + b[t.j]的应该是 a[t.i + 1] + b[t.j]
q.push((info){t.i +1, t.j, a[t.i +1]+ b[t.j]});}return0;}
超市
#include<bits/stdc++.h>
using namespace std;constint N =1e4+10;int n;structinfo{int p, d;}a[N];
bool cmp(info x, info y){return x.d < y.d;}
priority_queue <int, vector<int>, greater<int>> heap;intmain(){while(cin >> n){for(int i =1; i <= n; i ++){
cin >> a[i].p >> a[i].d;}sort(a +1, a +1+ n, cmp);for(int i =1; i <= n; i ++){
heap.push(a[i].p);if(heap.size()> a[i].d){
heap.pop();}}int ans =0;while(heap.size()){
ans += heap.top();
heap.pop();}
cout << ans << endl;}return0;}
哈夫曼编码1
#include<bits/stdc++.h>
using namespace std;constint N =109;int n;structnode{char ch, code;int w, p, id;}a[N];
bool operator >(node x, node y){if(x.w > y.w)return true;if(x.w == y.w){if(x.ch > y.ch)return true;}return false;}
priority_queue <node, vector<node>, greater<node>> q;intmain(){while(cin >> n){for(int i =1; i <= n; i ++){char ch;int w;
cin >> ch >> w;
a[i]={ch,' ', w,-1, i};
q.push(a[i]);}while(q.size()>1){
node x = q.top(); q.pop();
node y = q.top(); q.pop();
a[++ n]={x.ch,' ', x.w + y.w,-1, n};
a[x.id].p = n;
a[y.id].p = n;
a[x.id].code ='0';
a[y.id].code ='1';
q.push(a[n]);}
q.pop();
n = n /2+1;for(int i =1; i <= n; i ++){
string s ="";int t = a[i].id;while(a[t].p !=-1){
s = a[t].code + s;
t = a[t].p;}
cout << a[i].ch <<':'<< s << endl;}}return0;}
哈夫曼编码2
#include<bits/stdc++.h>
using namespace std;constint N =109;int n;structnode{char ch;int dep, w, p, id;}a[N];
priority_queue <node, vector<node>, greater<node>> q;
bool operator >(node x, node y){if(x.w > y.w)return true;if(x.w == y.w){if(x.ch > y.ch)return true;}return false;}intmain(){while(cin >> n && n !=0){for(int i =1; i <= n; i ++){char ch;int w;
cin >> ch >> w;
a[i]=(node){ch,0, w,-1, i};
q.push(a[i]);}while(q.size()>1){
node x = q.top(); q.pop();
node y = q.top(); q.pop();
a[++ n]=(node){' ',0, x.w + y.w,-1, n};
a[x.id].p = n;
a[y.id].p = n;
q.push(a[n]);}
q.pop();int ans =0;
n = n /2+1;for(int i =1; i <= n; i ++){int t = a[i].id;while(a[t].p !=-1){
a[i].dep +=1;
t = a[t].p;}
ans += a[i].w * a[i].dep;}
cout << ans << endl;}return0;}
花生采摘
#include<bits/stdc++.h>
using namespace std;constint N =30;int m, n, k, t, cnt;structnode{int x, y, num;};
bool operator <(node x, node y){return x.num < y.num;}
priority_queue <node, vector<node>, less<node>> q;intmain(){
cin >> m >> n >> k;for(int i =1; i <= m; i ++){for(int j =1; j <= n; j ++){int p;
cin >> p;
node t ={i, j, p};
q.push(t);//优先队列堆顶的花生数量最多}}
node pre, now;for(int i =1;; i ++){
now = q.top();if(i ==1) pre ={0, now.y,0};//从第0行第now.y列出发
t +=abs(now.x - pre.x)+abs(now.y - pre.y)+1;//曼哈顿距离 + 采摘时间 if(t + now.x <= k) cnt += now.num;//如果来得及回去就采 elsebreak;//否则就回去
pre = now;
q.pop();if(q.empty())break;//采完了就回去 }
cout << cnt << endl;return0;}