賽前寫掛板子錯誤整理
阿新 • • 發佈:2018-11-07
賽前寫掛板子錯誤整理
P3378 【模板】堆
封裝版:
#include<cstdio> #define rg register #define ci const int #define cl const long long int typedef long long int ll; namespace IO { char buf[300]; } template <typename T> inline void qr(T &x) { rg char ch=getchar(),lst=' '; while((ch > '9') || (ch < '0')) lst=ch,ch=getchar(); while((ch >= '0') && (ch <= '9')) x=(x<<1)+(x<<3)+(ch^48),ch=getchar(); if(lst == '-') x=-x; } template <typename T> inline void qw(T x,const char aft,const bool pt) { if(x < 0) {putchar('-');x=-x;} rg int top=0; do { IO::buf[++top]=x%10+'0'; } while(x/=10); while(top) putchar(IO::buf[top--]); if(pt) putchar(aft); } template <typename T> inline T mmax(const T a,const T b) {return a > b ? a : b;} template <typename T> inline T mmin(const T a,const T b) {return a < b ? a : b;} template <typename T> inline T mabs(const T a) {return a < 0 ? -a : a;} template <typename T> inline void mswap(T &a,T &b) { T _temp=a;a=b;b=_temp; } const int maxn = 1000010; template <typename T> struct Heap{ T heap[maxn]; int hcnt; inline bool empty() {return !hcnt;} inline int sz() {return hcnt;} void push(const T &x) { int pos; heap[pos=++hcnt]=x; while(pos != 1) { int fa = pos>>1; if(heap[pos] < heap[fa]) mswap(heap[fa],heap[pos]); pos>>=1; } } void pop() { int pos; heap[pos=1]=heap[hcnt--]; while((pos<<1) <= hcnt) { int s=pos<<1; if((s < hcnt) && (heap[s|1] < heap[s])) s|=1; if(heap[pos] > heap[s]) mswap(heap[pos],heap[s]); pos=s; } } inline T top() { return heap[1]; } }; Heap<int>Q; int n; int main() { qr(n); int a; while(n--) { a=0;qr(a); if(a == 1) { a=0;qr(a);Q.push(a); } else if(a == 2) { qw(Q.top(),'\n',true); } else { Q.pop(); } } return 0; }
未封裝:
#include<cstdio> #define rg register #define ci const int #define cl const long long int typedef long long int ll; namespace IO { char buf[300]; } template <typename T> inline void qr(T &x) { rg char ch=getchar(),lst=' '; while((ch > '9') || (ch < '0')) lst=ch,ch=getchar(); while((ch >= '0') && (ch <= '9')) x=(x<<1)+(x<<3)+(ch^48),ch=getchar(); if(lst == '-') x=-x; } template <typename T> inline void qw(T x,const char aft,const bool pt) { if(x < 0) {putchar('-');x=-x;} rg int top=0; do { IO::buf[++top]=x%10+'0'; } while(x/=10); while(top) putchar(IO::buf[top--]); if(pt) putchar(aft); } template <typename T> inline T mmax(const T a,const T b) {return a > b ? a : b;} template <typename T> inline T mmin(const T a,const T b) {return a < b ? a : b;} template <typename T> inline T mabs(const T a) {return a < 0 ? -a : a;} template <typename T> inline void mswap(T &a,T &b) { T _temp=a;a=b;b=_temp; } const int maxn = 1000010; int n,hcnt; int heap[maxn]; void add(ci); int ask(); void dlt(); int main() { qr(n); int a; while(n--) { a=0;qr(a); if(a == 1) { a=0;qr(a);add(a); } else if(a == 2) { qw(ask(),'\n',true); } else { dlt(); } } return 0; } void add(ci x) { int pos; heap[pos=++hcnt]=x; while(pos != 1) { int fa=pos>>1; if(heap[fa] > heap[pos]) mswap(heap[pos],heap[fa]); pos>>=1; } } inline int ask() {return heap[1];} void dlt() { int pos; heap[pos=1]=heap[hcnt--]; while((pos << 1) <= hcnt) { int s=pos<<1; if((s < hcnt) && (heap[s|1] < heap[s])) s|=1; if(heap[s] < heap[pos]) mswap(heap[pos],heap[s]); pos=s; } }
錯誤一:
刪除函式中
while((pos<<1) <= hcnt)
小於等於寫成小於
錯誤二:
刪除函式中
pos=s;
寫成pos<<=1。(因為可能pos=pos<<1|1)
錯誤三:
刪除函式中
while((pos<<1) <= hcnt)
寫成pos<=hcnt