/* Function: GetFormattedMedian(const multiset<int>& minSet,
 *                              const multiset<int>& maxSet)
 * Usage: GetFormattedMedian(minSet, maxSet);
 * -----------------------------------------------------------------------------
 * Returns a string representation of the median value between the provided 
 * minset and maxset, consistent with the output specifications from the problem 
 * description. 
 */
string GetFormattedMedian(const multiset<int>& minSet,
                          const multiset<int>& maxSet)
{
  stringstream s;
  
  if (minSet.size() > maxSet.size())
    s << fixed << *FindMaxElement(minSet);
  
  else if (minSet.size() < maxSet.size())
    s << fixed << *FindMinElement(maxSet);
  
  else
  {
    /* Cast the numerator as a long to prevent integer overflow when summing. */
    long long sum = (long long)(*FindMaxElement(minSet))
                    + *FindMinElement(maxSet);
    
    /* Now we can ensure the median will hold the correct 32-bit value. */
    double median = (double)(sum / 2.0);
    
    if (median != (int)median)
      s << setprecision(1);
    else
      s << setprecision(0);
    
    /* Ensures that the median will be printed in decimal notation, as opposed 
     * to scientific notation.
     */
    s << fixed << median;
  }
    
  return s.str();
}
void dolnasrednia(int srednia)
{
    multiset<int> ocenycopy = oceny;
    set<int>::iterator itbeg;
    itbeg=ocenycopy.begin();
    while(itbeg!=ocenycopy.end())
    {
        if((*itbeg)<=srednia)
        {
            pair <set<int>::iterator,set<int>::iterator> zasieg;
            zasieg=ocenycopy.equal_range(*itbeg);
            for(set<int>::iterator it=zasieg.first; *it< *zasieg.second;it++)
            {
                dolnasr.insert(*it);
                ocenycopy.erase(it);
            }
            int sumael=0;
            for(set<int>::iterator it=dolnasr.begin();it!=dolnasr.end();it++)
            {
                sumael+=(*it);
            }
            srednia=floor(sumael/dolnasr.size());
            itbeg=ocenycopy.begin();
            continue;
        }
        itbeg++;
    }
    int sumael=0;
    for(set<int>::iterator it=dolnasr.begin();it!=dolnasr.end();it++)
    {
        sumael+=(*it);
    }
    skroc(sumael, dolnasr.size());
}
vector<bool> Mochila(const multiset<Elemento> & elementos, double m) {
    Nodo inic = NodoInicial(elementos, m);
    double C = Greedy01(elementos,m);
    priority_queue<Nodo> LNV;
    LNV.push(inic);
    double s = numeric_limits<double>::min();
    vector<bool> resultado;

    multiset<Elemento>::const_reverse_iterator raux = elementos.rbegin();

    while (!LNV.empty()) {
        Nodo x = (LNV.top());
        LNV.pop();
        if (x.CS >= C) {
            for (unsigned k = 0; k < 2; k++) {
                bool elec = (k==0) ? true : false;
                Nodo y = Generar(x, elec, m,elementos);
                if (y.nivel == elementos.size()-1 && y.valor_actual > s) {
                    s = y.valor_actual;
                    C = (C >= s) ? C : s;
                    resultado = y.tupla;
                }

                else if (y.nivel < elementos.size()-1 && y.CS >= C){
                    C = (C >= y.CI) ? C : y.CI;
                    LNV.push(y);
                }
            }
        }
        ++raux;
    }
    return resultado;
}
Example #4
0
int main(){
   ifstream fin("input.txt");
   ofstream fout("output.txt");

   int n, k;
   long long tmp, t;
   fin>>n>>k;

   for(int i=0; i<n; ++i){
       fin>>tmp;

       if(q.size()==k){
           t=*q.begin(); q.erase(q.begin());
           q.insert(t+tmp);
       } else q.insert(tmp);
   }

   while(q.size()>1) q.erase(q.begin());

   fout<<*q.begin()<<endl;

   fin.close();
   fout.close();

   return 0;
}
 double findMedian() 
 {
     if (Large.size()>Small.size())
         return *Large.begin();
     else
         return (*Large.begin()+*(--Small.end()))*1.0/2.0;
 }
Example #6
0
 void insert(int num)
 {
     if(median == -1) median = num;
     else
     {
         if(num < median)
             small_number_set.insert(num);
         else
             big_number_set.insert(num);
         if(small_number_set.size() + 1 < big_number_set.size())
         {
             small_number_set.insert(median);
             auto median_iter = big_number_set.begin();
             median = *median_iter;
             big_number_set.erase(median_iter);
         }
         else if(small_number_set.size() > big_number_set.size())
         {
             big_number_set.insert(median);
             auto median_iter = prev(small_number_set.end());
             median = *median_iter;
             small_number_set.erase(median_iter);
         }
     }
 }
 // Returns the median of current data stream
 double findMedian() {
     if (first.size()> second.size()) {
         return *(first.rbegin());
     }
     double x = *first.rbegin();
     double y = *second.begin();
     return (x+y)/2;
 }
 void balance() {
     if (right.size() > left.size() + 1) {
         left.insert(*right.begin());
         right.erase(right.begin());
     }
     if (left.size() > right.size()) {
         right.insert(*left.rbegin());
         left.erase(--left.end());
     }
 }
void median_ele()
{
	//cout<<*s1.rbegin()<<endl;
	//printf("%d\n",(*s1.rbegin()));
	dint(*s1.rbegin());
	s1.erase(s1.find(*s1.rbegin()));
	if(s1.size()<s2.size())
	{
		s1.insert(*s2.begin());
		s2.erase(s2.begin());
	}
}
Example #10
0
void
pesquisa(int* vits, multiset<int> &sol, int feed)
{
    //static int cou = 0;
    //fout << cou++ << endl;
    
    
    //imprime_sol(sol);
    /*fout << endl;
    for (int i = 0; i < nVit; i++) {
        fout << vits[i] << " ";
    }
    fout << endl << endl;*/
     
    if (acabado(vits)) {              
       if (sol.size() < solucao.size() || solucao.size() == 0) {          
          solucao = sol;               
       }                   
       else if (sol.size() == solucao.size()) {
            multiset<int>::iterator it = sol.begin(), it2 = solucao.begin();
            
            for ( ; it != sol.end(); it++, it2++) {
                if (*it < *it2) {
                   solucao = sol;
                   break;        
                }    
            }     
       }
       return;                
    }
    else if (feed > nFeeds) {
         return;
    }
    
    int temp[MAXV];
    memcpy(temp, vits, sizeof(int)*nVit);
                  
    aplicaFeed(temp, vits, feed);
    if (melhor_(temp, vits)) {
       multiset<int> nova_sol = sol;               
       nova_sol.insert(feed);               
       pesquisa(temp, nova_sol, feed+1);
    }
    
    memcpy(temp, vits, sizeof(int)*nVit);
    
    multiset<int> nova_sol = sol;
    pesquisa(temp, nova_sol, feed+1);                  
              
              
}
Example #11
0
bool check()
{
    if(tcnt < st.size()) return 0 ;
    scnt=0 ;
    for(int i=0;!st.empty() && i<tcnt;i++)
    {
        auto it=st.upper_bound(tmp[i]) ;
        if(it!=st.begin())
            it-- , st_tmp[scnt++]=*it , st.erase(it) ;
    }
    int sz=st.size() ;
    for(int i=0;i<scnt;i++) st.insert(st_tmp[i]) ;
    return sz==0 ;
}
Example #12
0
void Adjust(int* mid)//调整中位数
{
	if(upper.size()>lower.size())
	{
		lower.insert(*upper.begin());
		upper.erase(upper.begin());
	}
	else if(lower.size()>upper.size()+1)
	{
		upper.insert(*lower.begin());
		lower.erase(lower.begin());
	}
	(*mid)=*lower.begin();
}
void add_ele(int x)
{
	if(s1.size()==s2.size())
	{
		s2.insert(x);
		s1.insert(*s2.begin());
		s2.erase(s2.begin());
	}
	else if(s1.size()>s2.size())
	{
		s1.insert(x);
		s2.insert(*s1.rbegin());
		s1.erase(s1.find(*s1.rbegin()));
	}
}
Example #14
0
void adjust(){
	long long val;

	if(!minset.empty()){

		if(!maxset.empty()){

			if(maxset.size() > minset.size()){
				it = maxset.begin();
				val = *it;
				maxset.erase(it);
				minset.insert(val);
				adjust();
			}

			else if(minset.size() - maxset.size() > 1){
				it = minset.end();
				it--;
				val = *it;
				minset.erase(it);
				maxset.insert(val);
				adjust();
			}
		}

		else{

			if(minset.size() > 1){
				it = minset.end();
				it--;
				val = *it;
				minset.erase(it);
				maxset.insert(val);
				adjust();
			}
		}
	}

	else{
		if(!maxset.empty()){
			it = maxset.begin();
			val = *it;
			maxset.erase(it);
			minset.insert(val);
			adjust();
		}
	}
}
int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> t;
  while(t--){
    S.clear();
    cin >> n;
    for(int i=0;i<n;i++){
      cin >> x;
      it = S.upper_bound(x);
      if(it==S.end()){
        S.insert(x);
      }
      else{
        S.erase(it);
        S.insert(x);
        //(*it) = x;
      }
    }
    cout << S.size() << " ";
    for(multiset<int>::iterator it=S.begin();it!=S.end();it++){
      cout << (*it)  << " ";
    }
    cout << endl;
  }
  return 0;
}
Example #16
0
bool AStar::removeMinimum( multiset<Node>& l,Node n){
    
    multiset<Node>::iterator it = l.begin();
    if(l.size()>=1){
        l.erase(it);
    }
}
int SequentialCoveringWithPreferences::getNumberOfValuesLessOrGreater(multiset<double>& values, double value, bool takeLess)
{
	int cnt = 0;
	for(auto it = values.begin(); it != values.end() && *it < value; it++, cnt++);

	return takeLess ? cnt : values.size() - cnt;
}
Example #18
0
main()
{
    int i,j,n,t,C;
    multiset<data>::iterator now,last;
    scanf("%d",&t);
    for(C=1;C<=t;C++)
    {
        scanf("%d",&n);
        s.clear();
        s.insert((data){-1,1000000001});
        s.insert((data){1000000001,-1});
        if(C>1)puts("");
        printf("Case #%d:\n",C);
        while(n--)
        {
            scanf("%d %d",&i,&j);
            while(1)
            {
                last=s.upper_bound((data){i,j});
                if(j>(now=last--)->b)
                    break;
                s.erase(*now);
            }
            if(j<last->b ||(i==last->a && j==last->b))
                s.insert((data){i,j});
            printf("%d\n",s.size()-2);
        }
    }
}
Example #19
0
int main()
{
    int num;
    while(cin>>num&&num)
    {
        int key;
        vec.clear();
        for(int i=0;i<num;i++)
        {
            cin>>key;
            vec.insert(key);
        }
        int sum=0;
        int x,y,s;
        while(vec.size()>=2)
        {
            multiset<int>::iterator it1=vec.begin();
            x=*it1;
            vec.erase(it1);
            multiset<int>::iterator it2=vec.begin();
            y=*it2;
            vec.erase(it2);
            sum=sum+x+y;
            vec.insert(x+y);
        }
        cout<<sum<<endl;
    }
    return 0;
}
int main() {
    scanf("%d", &ile);
    czas = ile - 1;
    for(int i = 1; i <= ile; i++) {
        scanf("%d",&wcz1);
        wierz[i].czasRozp = wcz1;
        wierz[i].numer = i;
    }
    for(int i = 0; i + 1 < ile; i++) {
        scanf("%d%d", &wcz1, &wcz2);
        wierz[wcz1].lacznik.push_back(&wierz[wcz2]);
        wierz[wcz2].lacznik.push_back(&wierz[wcz1]);
    }
    dfs(1,0);
    for(int i = 1; i <= ile; i++) {
        wierz[i].suma = wierz[i].odl + wierz[i].czasRozp;
        czyByl[i] = false;
    }
    zbior.insert(wierz[1]);
    while(zbior.size() > 0) {
        Wierz nowy = *zbior.begin();
        zbior.erase(zbior.begin());
        czyByl[nowy.numer] = true;
        for(int i = 0; i < nowy.lacznik.size(); i++) {
            if(czyByl[nowy.lacznik[i]->numer] == false) {
                zbior.insert(*wierz[nowy.numer].lacznik[i]);
            }
        }
        wynik = max(wynik, nowy.suma + czas);
        czas--;
    }
    printf("%d",wynik);
    return 0;
}
Example #21
0
void solve(int idx)
{
	if(flag)
		puts("");
	flag=1;
	scanf("%d",&N);
	printf("Case #%d:\n", idx);
	Ms.clear();
	while(N--)
	{
		int L,R;
		scanf("%d %d",&L,&R);
		Point tmp;
		tmp.L=L;tmp.R=R;
		it=Ms.lower_bound(tmp);
		if(it==Ms.begin()||(--it)->R>R)
		{
			Ms.insert(tmp);
			it=Ms.upper_bound(tmp);
			while(it!=Ms.end()&&it->R>=tmp.R)
			{
				Ms.erase(it++);
			}
		}
		printf("%d\n",Ms.size());
	}
}
Example #22
0
int main()
{
	int T;
	scanf("%d",&T);
	for(int kase=1; kase<=T; kase++)
	{
		st.clear();
		int n;
		scanf("%d",&n);
		printf("Case #%d:\n",kase);
		while(n--)
		{
			P t;
			scanf("%d%d",&t.x,&t.y);
			it=st.lower_bound(t);
			if(it==st.begin()||(--it)->y>t.y)
			{
			//printf("OK");
				st.insert(t);
				it=st.upper_bound(t);
				while(it!=st.end()&&it->y>=t.y)
				{
					//printf("%d %d\n",it->x,it->y);
					st.erase(it++);
				}
			}
			printf("%d\n",st.size());
		}
		if(kase!=T)
			printf("\n");
	}
	return 0;
}
Example #23
0
int main()
{
	int n,x;
	while(scanf("%d%d",&n,&x)+1)
	{
		st.clear();
		for(int i=0; i<n; i++)
		{
			int x;
			scanf("%d",&x);
			st.insert(x);
		}
		for(;;)
		{
			it=upper_bound(st.begin(),st.end(),x);
			if(it!=st.end())
			{
				x+=2;
				st.erase(it);
			}
			else
				break;
		}
		printf("%d\n",x+st.size());
	}
	return 0;
}
Example #24
0
inline i64 get(int cnt) {
    if (cnt > (int)S1.size() + (int)S2.size())
        return inf;
    while((int)S1.size() < cnt) {
        int x = *S2.begin();
        S2.erase(S2.begin());
        S1.insert(x);
        sum += x;
    }
    while(cnt < (int)S1.size()) {
        int val = *S1.rbegin();
        sum -= val;
        S1.erase(S1.find(val));
        S2.insert(val);
    }
    return sum;
}
Nodo NodoInicial(const multiset<Elemento> & objs, const double pesoM){
    double  cota_s = GreedyFraccional(pesoM, objs),
            ben_e = Greedy01(objs, pesoM);

    vector<bool> tupla(objs.size(),false);
    Nodo inicial(0, cota_s, ben_e, -1, 0, 0, tupla);

    return inicial;
}
Example #26
0
int main()
{
    //freopen("I.in","r",stdin);
    //freopen("I.out","w",stdout);
    int T;
    scanf("%d", &T);
    for(int cas=1;cas<=T;cas++)
    {
        mst.clear();
        int n, m, k, cnt = 0;
        long long  num;
        scanf("%d%d%d", &n, &m, &k);
        for(int i = 0; i < n; ++i)
        {
            scanf("%lld", &num);
            if(num <= k)
            {
                mst.insert(num);
            }
        }
        while(true)
        {
            int sz = mst.size();
            if(sz < 2)
                break;
            it1 = mst.end();
            it1--;
            long long  fst = *it1;
            mst.erase(it1);
            it2 = mst.lower_bound(min(fst, k - fst));
            if(it2 == mst.end())
                it2--;
            if(it2 == mst.begin() && *it2 > (k - fst))
                continue;
            if(*it2 <= k - fst)
            {
                ans[cnt++] = fst * ( *it2);
                mst.erase(it2);
            }
            else
            {
                it2 --;
                ans[cnt++] = fst * (*it2);
                mst.erase(it2);
            }
        }
        sort(ans, ans + cnt, cmp);
        long long  res = 0;
        for(int i = 0; i < min(m, cnt); ++i)
        {
            res += ans[i];
        }
        printf("CASE #%d: %lld\n",cas,res);
    }
    return 0;
}
 // Adds a number into the data structure.
 void addNum(int num) {
     if (first.empty() || num <= *(first.rbegin()) ) {
         first.insert(num);
     } else {
         second.insert(num);
     }
     
     if (first.size() > second.size() + 1) {
         auto it = first.end();
         it--;
         second.insert(*(it));
         first.erase(it);
     }
     
     if ( first.size() < second.size() ) {
         first.insert(*(second.begin()));
         second.erase(second.begin());
     }
 }
Example #28
0
int main(){
  int n;rit(n);
  for(int l,r;n;--n){
    rit(l,r);
    auto it=st.upper_bound(r);
    if(it!=st.end())st.erase(it);
    st.insert(l);
  }
  printf("%d\n",st.size());
}
		void pushup() {
			if (this == EMPTY)	return ;
			mxv = val;
			if (s.size())
				mxv = max(mxv, *s.rbegin());
			if (ch[0] != EMPTY)
				mxv = max(mxv, ch[0]->mxv);
			if (ch[1] != EMPTY)
				mxv = max(mxv, ch[1]->mxv);
		}
Example #30
0
void adjust() {
	if (righ.size() > lef.size()) {
		int v = *righ.begin();
		righ.erase(righ.begin());
		lef.insert(v);
	}
	else if (lef.size()  > righ.size() + 1) {
		auto mid = lef.end();
		--mid;
		int v = *(mid);
		lef.erase(mid);
		righ.insert(v);
	}
	if (S.size() > 0) {
		auto it = lef.end();
		--it;
		med = *it;
	}
}