Example #1
0
	int pop(){
		if(l.size() == 0) return INT_MIN;
		if(l.back() == min_list.back()) min_list.pop_back();
		int temp = l.back();
		l.pop_back();
		return temp;
	}
Example #2
0
void Andrew() {
  list<point>::reverse_iterator rit;
  point a;
  sort(pts.begin(),pts.end(),sup);
  U.clear();
  L.clear();
  for (int i=0; i<n; i++) {
    while (L.size()>=2) {
      rit = L.rbegin();
      a = *rit;
      rit++;
      if (det(a,*rit,pts[i])>=0) {
	L.pop_back();
      }
      else break;
    }
    L.push_back(pts[i]);
  }
  for (int i=0; i<n; i++) {
    while (U.size()>=2) {
      rit = U.rbegin();
      a = *rit;
      rit++;
      if (det(a,*rit,pts[i])<=0) {
	U.pop_back();
      }
      else break;
    }
    U.push_back(pts[i]);
  }
  U.pop_front();
  L.pop_back();
}
Example #3
0
void sameBarProfitCheck(list<openEntry> &openLedger, list<profitEntry> &profitLedger, const int ID, int qty, int &openPosition, double &minMax)
{
	if (openAvg == 0)
	{
		// Is there a profit on the bar of the trade? 
		// Short signal - check LOW
		if ((qty < 0) && (barsInPtr[ID + 1 + shiftLow] < barsInPtr[ID + 1 + shiftOpen] - PROFIT_TGT))
		{
			// We have a profit on the same observation.  Move the entry in the profit ledger
			moveProfitLedger(profitLedger, ID, qty, barsInPtr[ID + 1 + shiftOpen] - PROFIT_TGT);
			openPosition = openPosition - qty;
			openLedger.pop_back();
		}
		// Long signal - check HIGH
		else if ((qty > 0) && (barsInPtr[ID + 1 + shiftHigh] > barsInPtr[ID + 1 + shiftOpen] + PROFIT_TGT))	
		{
			// We have a profit on the same observation.  Put entry in the profit ledger
			moveProfitLedger(profitLedger, ID, qty, barsInPtr[ID + 1 + shiftOpen] + PROFIT_TGT);
			openPosition = openPosition - qty;
			openLedger.pop_back();
		} 
		else
		{
			openPosition = openPosition + qty;
		}
	}
	else
	{

		// Check same bar using new average
		// Requires minMax already updated !!
		newAvgChk(openLedger, profitLedger, ID, openPosition, minMax);

	}
}
Example #4
0
void GenPermutation(vector<string> &prefix_set, int start, list<string> &result, int freq, int suffix_len)
{
    if(start==prefix_set.size()-1)
    {
	result.push_back(prefix_set[start]);
	if(result.size()<=suffix_len)
	    return;
	
	list<string>::iterator it;
	printf("{");
	for(it=result.begin(); it!=result.end(); it++)
	    printf("%s ",it->c_str());
	printf("}#%d \n",freq);

	result.pop_back();
	if(result.size()>suffix_len)
	{
	    printf("{");
	    int i;
	    for(i=0, it=result.begin(); it!=result.end(); i++,it++)
	    {
		printf("%s",it->c_str());
		if(i!=result.size()-1)
		    printf(",");
	    }
	    printf("}#%d\n",freq);
	}
        return;
    }

    result.push_back(prefix_set[start]);
    GenPermutation(prefix_set, start+1, result, freq, suffix_len);
    result.pop_back();
    GenPermutation(prefix_set, start+1, result, freq, suffix_len);
}
Example #5
0
void split(string path, list<string> & lists){
	size_t first = 0;
	while( first != string::npos && first < path.size()){
		first = path.find_first_of('/', first);
		size_t second = path.find_first_of('/', first +1);
		if( second != string::npos){
			string sub =  path.substr(first+1, second -first-1);
			if(sub == ".."){
				if( lists.size() > 0)
					lists.pop_back();
			}else if(sub == "." || sub == ""){

			}else {
				lists.push_back(sub);
			}
		}else{
			string sub =  path.substr(first+1);
			if(sub == ".."){
				if( lists.size() > 0)
					lists.pop_back();
			}else if(sub == "." || sub == ""){

			}else {
				lists.push_back(sub);
			}
			break;
		}
		first = second;
	}
}
Example #6
0
int main()
{
	int n;
	while(scanf("%d%d%d",&n,&lMin,&lMax)!=EOF){
		Max.clear();
		Min.clear();
		int len=0;
		int s=0;
		for(int i=0;i<n;i++){
			scanf("%d",&num[i]);
			while(!Max.empty()&&num[i]>=num[Max.back()]){
				Max.pop_back();
			}
			while(!Min.empty()&&num[i]<=num[Min.back()]){
				Min.pop_back();
			}
			Max.push_back(i);
			Min.push_back(i);
			int dif=num[Max.front()]-num[Min.front()];
			while(dif>lMax){
				s++;
				if(Min.front()<s) Min.pop_front();
				if(Max.front()<s) Max.pop_front();
				dif=num[Max.front()]-num[Min.front()];
			}
			int ll=i-s+1;
			if(dif>=lMin&&ll>len) len=ll;
		}
		printf("%d\n",len);
	}
}
list<int> sum(list<char> a, list<char> b) {
    int carry= 0;
    int sum = 0;
    int k = 0;
    list<int> result;
    
    // find longest number
    addLeadingZeros(a, b);
    
    // sum
    for( int l = a.size() - 1; 0 <= l; l--) {
        
        // first op
        char n = a.back();
        int first = n-'0';//string to int
        a.pop_back();
        
        // second op
        n = b.back();
        int second = n - '0';// string to int
        b.pop_back();
        
        sum = first + second + carry;
        result.push_front(sum%10);
        carry = sum / 10;
        
    }
    
    // add carry as most significant digit
    if(carry != 0) {
        result.push_front(carry);
    }
    
    return result;
}
Example #8
0
inline void loopdelr(int n){
    if (f.back()>n) f.back()-=n;
    else {
        n-=q.back();
        q.pop_back();
        f.pop_back();
        if(n!=0) loopdelr(n);
    }
}
Example #9
0
File: 0.Demo.C Project: hoa84/sight
int fibScopeLinks(int a, scope::scopeLevel level, list<int>& stack, 
                  map<list<int>, anchor>& InFW, 
                  map<list<int>, anchor>& InBW, 
                  map<list<int>, anchor>& OutFW,
                  map<list<int>, anchor>& OutBW,
                  bool doFWLinks) {
  stack.push_back(a); // Add this call to stack
  list<int> stackNoTop = stack; stackNoTop.pop_front();
  //dbg << "stack="; for(list<int>::iterator i=stack.begin(); i!=stack.end(); i++) dbg << *i << " "; dbg << endl;
  //dbg << "stackNoTop="; for(list<int>::iterator i=stackNoTop.begin(); i!=stackNoTop.end(); i++) dbg << *i << " "; dbg << endl;
  
  // Each recursive call to fibScopeLinks generates a new scope at the desired level. To reduce the amount of text printed, we only 
  // generate scopes if the value of a is >= verbosityLevel
  scope s(txt()<<"fib("<<a<<")", 
            (InFW.find(stackNoTop)!=InFW.end()? InFW[stackNoTop]: anchor::noAnchor),
            level);
  OutBW[stack] = s.getAnchor();
  
  if(a==0 || a==1) { 
    dbg << "=1."<<endl;
    if(doFWLinks) {
      anchor fwLink;
      fwLink.linkImg("Forward link"); dbg << endl;
      OutFW[stack] = fwLink;
    }

    if(InBW.find(stackNoTop)!=InBW.end()) { 
      InBW[stackNoTop].linkImg("Backward link"); dbg<<endl;
    }
    
    //cout << "link="<<dbg.linkTo(linkScopes[stack], "go")<<endl;
    stack.pop_back(); // Remove this call from stack
    return 1;
  } else {
    int val = fibScopeLinks(a-1, level, stack, InFW, InBW, OutFW, OutBW, doFWLinks) + 
              fibScopeLinks(a-2, level, stack, InFW, InBW, OutFW, OutBW, doFWLinks);
    dbg << "="<<val<<endl;
    
    if(doFWLinks) {
      anchor fwLink;
      fwLink.linkImg("Forward link"); dbg << endl;
      OutFW[stack] = fwLink;
    }
    if(InBW.find(stackNoTop)!=InBW.end())
    { InBW[stack].linkImg("Backward link"); dbg<<endl; }
    
    //cout << "link="<<dbg.linkTo(linkScopes[stack], "go")<<endl;
    stack.pop_back(); // Remove this call from stack
    return val;
  }
}
Example #10
0
	int pop(){
		if(l.size() == 0) return INT_MIN;
		int temp = l.back().back();
		l.back().pop_back();
		if(l.back().size() == 0) l.pop_back();
		return temp;
	}
Example #11
0
void StartFunc()
{
    while(!blocklist.empty())
    {
        delete blocklist.front();
        blocklist.pop_front();
    }
    while(!statlist.empty()) statlist.pop_back();

    for(int i = 0; i < 50; i++)
    {
        CBlock *tmp = new CBlock(hge, fnt, frame);

        if(i < 5) 
        {
            tmp->SetNow(5 - i);
            tmp->SetNext(5 - i + 1);
        }

        blocklist.push_back(tmp);
    }

    started = true, end = false, dropping = false;
    timer = 0.0f, right = 0, wrong = 0, droppingTimer = 0.0f;
}
Example #12
0
void sigint_handler(int sig)
{
    printf("Start delete clients. Total: %d\n", clientList.size() );
    while (!clientList.empty())
    {
        Client* cln=clientList.back();

        void* res;
        pthread_cancel(cln->thread);
        pthread_join(  cln->thread, &res);

        if (res == PTHREAD_CANCELED)
            printf("thread was canceled from sig_handler\n");

        shutdown(cln->socket, SHUT_RDWR);
        close(cln->socket);
        delete cln;
        clientList.pop_back();
    }

    close(s);

    list<char*>::iterator it=messageList.begin();
    for ( ; it!=messageList.end(); ++it)
        free(*it);

    close(s);
    exit(0);
}
Example #13
0
BOOL Do(HANDLE hFile)
{
	char buf[128];
	DWORD wr;

	EnterCriticalSection(&m_cs);
	while ( !lstSend.empty() )
	{
		string& str = lstSend.back();
		if ( !WriteFile( hFile, str.c_str(), str.length(), &wr, NULL ) ||
			wr != str.length() )
		{
			printf("Write error, %d\n", GetLastError());
		}
		lstSend.pop_back();
	}
	LeaveCriticalSection(&m_cs);

	DWORD rd;
	//printf("r");
	if( !ReadFile(hFile, buf, 127, &rd, NULL ))
	{
		printf("Read failed. System error %d", GetLastError() );
		return FALSE;
	}
	//printf(rd>0?"R":"0");
	if ( rd > 0 )
	{
		buf[rd] = 0;
		m_ws.Send( buf );
	}


	return TRUE;
}
Example #14
0
void search(int frog[N], list<int> solution)
{
    for (int i=1; i<=N; i++)
    {
        if (frog[i]==0)
            continue;
        else
        {
            int tmp;
            for (int len=1; len<=2; len++)
                if (canJump(frog, i, len))
                {
                    tmp = frog[i];
                    jump(frog, i, len, 1);
                    solution.push_back(i);
                    if (isOK(frog))
                        show(solution) ;

                    search(frog, solution);
                    jump(frog, i+len*tmp, len, -1);
                    solution.pop_back();
                }
        }
    }

}           
Example #15
0
bool search(
    int current,
    int target,
    list<int> &paths,
    map<int, set<int> > &edges)
{
  if (current == target) {
    return true;
  }

  set<int>::iterator it = edges[current].begin();
  set<int>::iterator end = edges[current].end();
  while (it != end) {
    paths.push_back(*it);

    bool found = search(*it, target, paths, edges);
    if (found) {
      return true;
    }

    paths.pop_back();
    ++it;
  }

  return false;
}
Example #16
0
bool backSumaPart(list<int> part1, list<int> part2,
		  int sumaPart1, int sumaPart2)
{
  if(part2.empty())
    return false;
  else if (sumaPart1 == sumaPart2) {
    mostrar_solucion(part1); mostrar_solucion(part2);
    return true;
  }
  bool sol = false;
  bool pode = false;
  list<int>::iterator i = part2.begin();
  while(!sol && !pode && i != part2.end()) {
    list<int>::iterator tmp_it = i;
    int v = (*i);
    tmp_it++;
    part2.erase(i);
    part1.push_back(v);
    if (poda(sumaPart1, sumaPart2, v))
      pode = true;
    else 
      sol = backSumaPart(part1, part2, sumaPart1 + v, sumaPart2 - v);
    part1.pop_back();
    part2.insert(tmp_it, v);
    i = tmp_it;
  }
  return sol;
}
//方法是可行的
void print(int n,int m,list<int>& path)
{
	if(n<1)
		return ;
	if(m<0)
		return  ;
	if(m==0)
	{
		for(list<int>::iterator iv=path.begin();iv!=path.end();++iv)
			cout<<*iv<<" ";
		cout<<endl;
		return ;
	}
	else //m>0
	{
		//n算和n不算都是
		//1.n算
		bool flag=false;
		path.push_back(n);
		print(n-1,m-n,path);
		path.pop_back();

		print(n-1,m,path);//n不算
	}
}
bool HandDetector::isHand(Eigen::Vector4f & handCenter) 
{
	static list<Eigen::Vector4f> points;
	static Eigen::Vector4f sum(0.0f,0.0f,0.0f,1.0f);

	//Add the handCenter
	sum += handCenter;
	points.push_front(handCenter);

	//Queue must be full first
	if (points.size() < params.listSize)
		return false;

	//Delete the oldest point
	sum -= points.back();
	points.pop_back();


	//Calculate mean and std
	Eigen::Vector4f mean = sum / LIST_SIZE;

	//Calculate var
	Eigen::Vector4f var(0.0f, 0.0f, 0.0f, 1.0f);
	BOOST_FOREACH(Eigen::Vector4f point, points) 
	{
		var += (point - mean).cwiseProduct(point - mean);
	}
Example #19
0
 void participle::_part(list<part_attr *> &cur, const char *str, long &pos) {
     if (str[pos] == 0) {
         long value = _envalue(cur);
         
         list<pair<long,list<part_attr*>>>::iterator i = m_parting.begin();
         while (i != m_parting.end() && (*i).first > value) {
             ++i;
         }
         
         if (i != m_parting.begin()) {
             --i;
         }
         m_parting.insert(i, pair<long, list<part_attr*>>(value, cur));
     } else {
         list<part_attr*> ps = g_part_knowledge->match(str + pos);
         
         if (ps.empty()) {
             ++pos;
             _part(cur, str, pos);
         } else {
             for (part_attr* pa : ps) {
                 cur.push_back(pa);
                 pos += pa->str().length();
                 
                 _part(cur, str, pos);
                 
                 pos -= pa->str().length();
                 cur.pop_back();
             }
         }
     }
 }
Example #20
0
        void set(int key, int value)
        {
            if(m_map.find(key) == m_map.end())
            {
                //delete the back one if reach capacity
                if(m_capacity == m_list.size())
                {
                    cacheNode tmp = m_list.back();    
                    m_list.pop_back();
                    m_map.erase(tmp.key);
                }
                // insert new one into the head
                cacheNode node(key, value); 
                m_list.push_front(node);
                m_map[key]= m_list.begin();
            }
            else
            {
                //move the node to head of double list
                list<cacheNode>::iterator it = m_map[key];
                m_list.splice(m_list.begin(), m_list, it);
                //update value
                m_list.begin()->val = value;
                
            }

        }
void recursion(int board, int index) {
	if (board == 0) {
		if (cur < minVaule) {
			minVaule = cur;
			sequence = sequenceTemp;
		}
		return;
	}

	if (index < BOARD_SIZE * BOARD_SIZE) {

		//current not change
		int row = index / BOARD_SIZE;
		int column = index % BOARD_SIZE;

		recursion(board, index + 1);

		int temp = board;
		//current change
		cur++;
		board = board ^ mask[index];
		sequenceTemp.push_back(index);
		recursion(board, index + 1);

		cur--;
		board =  temp;
		sequenceTemp.pop_back();
	}

}
Example #22
0
void doans(int r, int ld, int rd, int uplimit,list<int>& sol) {
    if (r != uplimit) {
        /** 还没摆满 */

        /** 取能放的位置 */
        int pos = uplimit & ~(r | ld | rd);

        /** 若还有位置如00001011表示能放5、7、8位 */
        while (pos != 0) {
            /** 取低位 */
            int p = pos & (~pos + 1);

            /** 更新位置 */
            pos -= p;

            /** 答案入 */
            sol.push_back(p);

            /** 更新禁手并进行下一层更新 */
            doans(r + p, (ld + p) << 1, (rd + p) >> 1, uplimit,sol);

            /** 答案出 */
            sol.pop_back();
        }
    }
// 借助辅助内存,实现高效算法,保存访问的路径,即两个链表
bool getNodePath(TreeNode* root, TreeNode* node, list<TreeNode*>& path)
{
	if (root == NULL)
	{
		return false;
	}
	
	path.push_back(root);
	
	if (root == node)
	{
		return true;
	}
	
	bool found = false;
	
	// 很多孩子节点,故需要while一个一个遍历,依次递归访问
	vector<TreeNode*>::iterator it = root->children.begin();
	while (!found && it < root->children.end())
	{
		found = getNodePath(*it, node, path);
		it++;
	}
	
	if (!found)
	{
		path.pop_back();
	}	
	
	return found;
}
Example #24
0
int solve_equation (list < double > & p, vector < double > & x)
{
    if (p.size () == 0)    // 0 = 0
        return - 1;
    if (eq (p.front (), 0))    // a = 0
    {
        p.pop_front ();
        return solve_equation (p, x);
    }
    if (p.size () == 1)
        return 0;
    if (eq (p.back (), 0))    // pn = 0
    {
        p.pop_back ();
        int flag = solve_equation (p, x);
        p.push_back (0.000);
        int i;
        forn (i, x.size ())
            if (eq (x[i], 0.000))
                return 1;
            else
                if (x[i] > 0)
                    break;
        x.insert (x.begin () + i, 0.000);
        return 1;
    }
Example #25
0
void search(int root, int d, int n)
{
  int l = path.size();
  int last = path.back();
  vector<int> &conn = connections[last];

  if (l == d) {
    if (find(conn.begin(), conn.end(), root) != conn.end()) {
      when_circle_found(n);
    }

    return;
  }

  vector<int>::iterator it = conn.begin();
  while (it != conn.end()) {
    int c = *it;
    if (c > root && !used[c]) {
      path.push_back(c);
      used[c] = true;

      search(root, d, n);

      path.pop_back();
      used[c] = false;
    }

    ++it;
  }
}
Example #26
0
bool dfs(int cnt_len, int len) {
   if(stick.empty()) 
      return (cnt_len == len);
   if(cnt_len == len) {
      int vlu = stick.back();
      stick.pop_back();
      yeah = dfs(vlu, len);
      stick.push_back(vlu);
      if(yeah) {
         cout << vlu << " ";
         return 1;
      }
   }      
   else if(cnt_len < len) {
      for(cnt = stick.begin(); cnt != stick.end();) {
         if(cnt_len + *cnt > len) break;
         int vlu = *cnt;
         cnt = stick.erase(cnt);
         yeah = dfs(cnt_len + vlu, len);
         stick.insert(cnt, vlu);
         if(yeah) {
            cout << vlu << " ";
            return 1;
         }
      }
   }
   return 0;
}
Example #27
0
void subset(int arr[], int size, int left, int index, list<int> &l, vector<vector <int> > &B, int n, bool &flag){
    if(left==0){
	vector< vector <int> > A;
	for(list<int>::iterator it=l.begin(); it!=l.end() ; ++it)
		A.push_back(B[*it]);
	if(cover(B,A,n)==true){
		string s = SSTR(n) + ".txt";
		ofstream f(s.c_str());
		for(int r=0;r<A.size();r++){
			for(int j=0;j<A[r].size();j++){
				f<<A[r][j]<<" ";
			}
			f<<endl;
		}
		f.close();
		flag=true;
	}
	A.clear();
	return;
    }
    if(flag==true)
	return;
    for(int i=index; i<size;i++){
        l.push_back(arr[i]);
        subset(arr,size,left-1,i+1,l,B,n,flag);
        l.pop_back();
    }
}
	static bool matchEnding(string& s, list<string>& elements, const string& p)
	{
		const string& last = elements.back();
		size_t lastLength = last.length();

		if (p.substr(p.length() - lastLength, lastLength) == last)
		{
			size_t strLength = s.length();

			if (strLength < lastLength)
			{
				return false;
			}

			string suffix = s.substr(strLength - lastLength);

			for (size_t i = 0; i < lastLength; i++)
			{
				if (suffix[i] == last[i] || last[i] == '?')
				{
					continue;
				}

				return false;
			}

			s.erase(strLength - lastLength, lastLength);
			elements.pop_back();
		}

		return true;
	}
int main() {
	int n, elm, p1, p2;
	cin >> n;
	for (int i = 0; i < n; ++i) {
		cin >> elm;
		l.push_back(elm);
	}
	bool turn = 1;
	int mx;
	p1=p2=0;
	while (!l.empty()) {
		if (l.front() > l.back()) {
			mx = l.front();
			l.pop_front();
		} else {
			mx = l.back();
			l.pop_back();
		}
		if (turn)
			p1 += mx;
		else
			p2 += mx;
		turn ^= 1;
	}
	cout << p1 << " " << p2;
	return 0;
}
Example #30
0
void main()
{
    int t,n,k;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&k);
		sum[0]=0;
		for(int i=1;i<=n;i++){
			scanf("%d",&num[i]);
			sum[i]=sum[i-1]+num[i];
		}
		for(int i=1;i<=n;i++)
			sum[n+i]=sum[n+i-1]+num[i];
        int max=-0x7fffffff,temp=0;
        int start=0,end=0;
		Min.clear();Min.push_back(0);
        for(int i=1;i<=2*n;i++)
        {
			if(i-Min.front()>k) Min.pop_front();
			temp=sum[i]-sum[Min.front()];
			if(temp>max){max=temp;start=Min.front()+1;end=i;}
			while(!Min.empty()&&sum[i]<=sum[Min.back()])
				Min.pop_back(); 
			Min.push_back(i);
        }
        printf("%d %d %d\n",max,start,(end-1)%n+1);
    }
}