Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Re:一样的代码C++能过,G++TLEIn Reply To:一样的代码C++能过,G++TLE Posted by:biran007 at 2009-08-10 01:09:51 > 为啥会这样? > 这种情况经常出现么? > 还有,我的O(nlogk)的要6秒,O(n)代码为啥也跑了5秒? > 数据到底有多么巨大饿…… #include <cstdio> #include <cstdlib> #include <stdexcept> using namespace std; template<class T> class Fuck { public: Fuck(size_t n = 1000005); ~Fuck(); T &front() { if (empty()) { throw runtime_error("Container is empty"); } return *sp; } T &back() { if (empty()) { throw runtime_error("Container is empty"); } return ep == data ? ep[n] : ep[-1]; } const T &front() const; const T &back() const; void push_back(const T &); void push_front(const T &); void pop_back(); void pop_front(); size_t size() const { if (ep < sp) return -(sp - ep) + (n + 1); return ep - sp; } bool empty() const { return sp == ep; } private: size_t n; T *data, *sp, *ep; }; int main() { // snipped } template<class T> Fuck<T>::Fuck(size_t nn) : n(nn), data(reinterpret_cast<T *>(malloc((n + 1) * sizeof(T)))), sp(data), ep(data) {} template<class T> Fuck<T>::~Fuck() { while (!empty()) { pop_back(); } free(data); } template<class T> void Fuck<T>::pop_back() { if (empty()) { throw runtime_error("Container is empty"); } back().~T(); if (ep == data) { ep = &data[n]; } else { --ep; } } template<class T> void Fuck<T>::push_back(const T &value) { if (size() == n) { throw runtime_error("Container is full"); } new(ep++) T(value); if (ep == data + (n + 1)) ep = data; } template<class T> void Fuck<T>::pop_front() { if (empty()) { throw runtime_error("Container is empty"); } front().~T(); ++sp; if (sp == data + (n + 1)) sp = data; } 用STL個deque,寫極都TLE,我換咗個class去,用C++隊,AC,用G++隊一樣TLE Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator