Beispiel #1
0
int main(){
#ifndef ONLINE_JUDGE
  freopen("in","r",stdin);
#endif
  scanf("%d",&n);
  int maxScore=0,minScore=2*n;
  For(i,n)scanf("%d",&a[i]);
  For(i,n)scanf("%d",&b[i]);

  sort(b+1,b+n+1);
  For(i,n)st.insert(a[i]);
  for(int i=n; i; i--){
    it=st.upper_bound(b[i]);
    if(it!=st.end()){
      maxScore+=2;
      st.erase(it);
      mark[i]=1;
    }
  }
  For(i,n)if(!mark[i]){
    it=st.lower_bound(b[i]);
    if(it!=st.end()){
      maxScore++;
      st.erase(it);
    }
  }

  st.clear();
  memset(mark,0,sizeof(mark));
  sort(a+1,a+n+1);
  For(i,n)st.insert(b[i]);
  for(int i=n; i; i--){
    it=st.upper_bound(a[i]);
    if(it!=st.end()){
      minScore-=2;
      st.erase(it);
      mark[i]=1;
    }
  }
  For(i,n)if(!mark[i]){
    it=st.lower_bound(a[i]);
    if(it!=st.end()){
      minScore--;
      st.erase(it);
    }
  }
  printf("%d %d\n",maxScore,minScore);
  return 0;
}
Beispiel #2
0
int main()
{
	scanf("%d", &t);
	while(t--)
	{
		y.clear();
		scanf("%d", &n);
		for(int i = 0; i < n; ++i)
			scanf("%d%d", &a[i].h, &a[i].w);
		sort(a, a + n);
		for(int i = 0; i < n; ++i)
			scanf("%d%d", &b[i].h, &b[i].w);
		sort(b, b + n);
		ans = 0;
		for(int i = 0, j = 0; i < n; ++i)
		{
			while(j < n && b[j].h <= a[i].h)
				y.insert(b[j++].w);
			if(y.empty())
				continue;
			multiset<int>::iterator it = y.upper_bound(a[i].w);
			if(it != y.begin())
			{
				y.erase(--it);
				++ans;
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}
Beispiel #3
0
int main(){
	int T, n;
	scanf("%d", &T);
	while( T-- ){
		scanf("%d", &n);
		for(int i=0 ; i < n ; ++i)
			scanf("%d%d", &a[i].h, &a[i].w);
		for(int i=0 ; i < n ; ++i)
			scanf("%d%d", &b[i].h, &b[i].w);
		sort( a, a+n, cmp );
		sort( b, b+n, cmp );
		st.clear();
		int ans = 0;
		for(int i=0,j=0 ; i < n ; ++i){
			while( j < n && a[i].h >= b[j].h ){
				st.insert(b[j].w);
				j++;
			}
			it = st.upper_bound(a[i].w);
			if( it != st.begin() && !st.empty() ){
				--it;
				st.erase(it);
				ans++;
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}
Beispiel #4
0
int main () {
    scanf("%d %d", &n, &k);

    for (int i = 0; i < n; i++) {
        scanf("%d %d", x+i, y+i);
        p[i] = i;
    }

    int res = 0;
    sort(p, p+n, cmp_t);
    for (int i = 0; i < k; i++)
        s.insert(0);

    for (int i = 0; i < n; i++) {
        it = s.upper_bound(x[p[i]]);
        if (it == s.begin())
            continue;

        --it;
        res++;

        s.erase(it);

        s.insert(y[p[i]]);
    }

    printf("%d\n", res);
    
}
Beispiel #5
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());
	}
}
Beispiel #6
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);
        }
    }
}
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;
}
Beispiel #8
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;
}
int main(){
    int n, m, x;
    while( scanf("%d%d", &n, &m) != EOF ){
        st.clear();
        for(int i=0 ; i < n ; ++i){
            scanf("%d", &x);
            st.insert(x);
        }
        for(int i=0 ; i < m ; ++i)
            scanf("%d", &a[i].d);
        for(int i=0 ; i < m ; ++i)
            scanf("%d", &a[i].p);
        if( m < n ){
            puts("No"); continue;
        }
        sort( a, a+m, cmp );
        LL ans = 0;
        for(int i=0 ; i < m ; ++i){
            it = st.upper_bound( a[i].d );
            if( it != st.begin() ){
                it--;
                st.erase(it);
                ans += a[i].p;
                if( st.empty() ) break;
            }
        }
        if( st.empty() ) printf("%I64d\n", ans);
        else puts("No");
    }
    return 0;
}
int main() {
  int n, m, p, b; scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i) {
    scanf("%d%lld", &A[i].x, &A[i].t);
    ++A[i].t;
    A[i].idx = i;
  }
  sort(A, A + n, [&] (Frog A, Frog B) {return A.x > B.x;});
  for (int i = 0; i < n; ++i) root -> set(A[i].x, min(A[i].x + A[i].t, (LL)INF), i);
  while (m--) {
    scanf("%d%d", &p, &b);
    int frog = root -> at(p);
    if (frog == -1) mos.insert({p, b});
    else {
      A[frog].t += b;
      ++A[frog].mos_count;
      auto it = mos.lower_bound({A[frog].x, 0});
      while (it != mos.end() and it -> first < A[frog].x + A[frog].t) {
        A[frog].t += it-> second;
        ++A[frog].mos_count;
        mos.erase(it);
        it = mos.upper_bound({A[frog].x, 0});
      }
      root -> set(A[frog].x, min(A[frog].x + A[frog].t,(LL) INF), frog);
    }
  }
  sort(A, A + n, [&] (Frog A, Frog B) {return A.idx < B.idx;});
  for (int i = 0; i < n; ++i) printf("%d %lld\n", A[i].mos_count, A[i].t - 1);
}
Beispiel #11
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());
}
Beispiel #12
0
int main(){
    int n;cin>>n;
    while(n--){
        int l,r;cin>>l>>r;
        auto it=st.upper_bound(r);
        if(it!=st.end())st.erase(it);
        st.insert(l);
    }
    cout<<st.size()<<endl;
    return 0;
}
Beispiel #13
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 ;
}
Beispiel #14
0
int main()
{
    while (1)
    {
        int n;
        scanf("%d",&n);
        if (n==0)
            break;
        for (int i=1;i<=n;i++)
            scanf("%d%d",&a[i].capacity,&a[i].time);
        int m;
        scanf("%d",&m);
        for (int i=1;i<=m;i++)
        {
            int x,y;
            scanf("%d%d:%d",&b[i].capacity,&x,&y);
            b[i].time=(x-14)*60+y;
        }
        m++;
        b[m].capacity=0;
        b[m].time=1<<30;
        sort(a+1,a+n+1,cmp);
        sort(b+1,b+m+1,cmp);
        int now=1;
        s.clear();
        for (int i=1;i<=m;i++)
        {
            while (now<=n && a[now].time<=b[i].time)
                s.insert(a[now++]);
            set <state>::iterator k=s.upper_bound(b[i]);
            if (k==s.begin())
                continue;
            s.erase(--k);
        }
        int sum=0;
        for (set <state>::iterator k=s.begin();k!=s.end();k++)
            sum+=k->capacity;
        static int id=0;
        printf("Trial %d: %d %d\n\n",++id,s.size(),sum);
    }
    return(0);
}
Beispiel #15
0
void dfs(int root) {
    if(k / a[root] > 1e9) {
        re += st.size();
    } else {
    //    cout << "size=" << st.size() << endl;
        multiset<int>::iterator it = st.upper_bound( (int)(k / a[root]) );
        if(it == st.end()) {
            re += st.size();
        } else {
            re += sp.rank( *it );
        }
    }
    st.insert( a[root] );
    sp.insert( a[root] );
    for(int i = 0; i < mp[root].size(); ++i) {
        dfs( mp[root][i] );
    }
    sp.del( a[root] );
    st.erase( st.find(a[root] ) );
} 
Beispiel #16
0
void calc(int mxCost) {
    if(!mxCost) return;

    while(idx < n && c[idx].cost <= mxCost) {
        active.insert(c[idx]);
        idx ++;
    }

    while(!grass.empty()) {
        PII gr = grass.top();
        grass.pop();
        it = active.upper_bound(gr);
        if(it != active.end()) {
            ans += mxCost;
            active.erase(it);
        }
        else break;
    }

    while(!grass.empty())
        grass.pop();
}
Beispiel #17
0
void erase_interval(int x, int y){
    x = *(apples.lower_bound(x));
    multiset<int>::iterator it = apples.upper_bound(x);
    vector<int> res;
    while(y && it != apples.end()){
	if(*it > x + b)break;
	add_apple(max(*it - b, 0), *it, root, -1);
	res.push_back(*it);
	apples.erase(it++);
	y--;
    }
    it = apples.lower_bound(x);
    while(y--){
	add_apple(max(*it - b, 0), *it, root, -1);
	cout << *it << ' ';
	apples.erase(it++);
    }
    vector<int>::iterator vit = res.begin();
    while(vit != res.end())
	cout << *vit++ << ' ';
    cout << endl;
}
Beispiel #18
0
	bool check_photo(int t, int phi, int lambda) const {
		auto dlt = dist(pos[t], make_pair(phi, lambda));
		phi = dlt.first;
		lambda = dlt.second;
		if (abs(phi) > d || abs(lambda) > d)
			return false;
		auto p = make_pair(t, make_pair(phi, lambda));
		auto it = taken.upper_bound(p);
		if (it != taken.end()){
			int tt = it->first - t;
			if (abs(phi - it->second.first) > 1ll * w * tt)
				return false;
			if (abs(lambda - it->second.second) > 1ll * w * tt)
				return false;
		}
		--it;
		int tt = t - it->first;
		if (abs(phi - it->second.first) > 1ll * w * tt)
			return false;
		if (abs(lambda - it->second.second) > 1ll * w * tt)
			return false;
		return true;
	}
Beispiel #19
0
main()
{
    int n ; scanf("%d",&n) ;
    for(int i=1;i<=n;i++) scanf("%d",&h[i]) ;
    for(int i=1;i<n;i++)
    {
        int x,y ; scanf("%d%d",&x,&y) ;
        v[x].push_back(y) ;
        v[y].push_back(x) ;
    }
    dfs0(1,-1,h[1]) ;

    int k ; scanf("%d",&k) ;
    for(int i=1,x;i<=k;i++) scanf("%d",&x) , st.insert(x) ;
    for(int i=k+1;i<=n;i++) st.insert(0) ;
    for(int i=1;i<=n;i++) cave[i]=(P){i,hm[i]} ;
    sort(cave+1,cave+n+1) ;

    LIM=-1 ;
    for(int i=1;i<=n;i++)
    {
        auto it=st.upper_bound(cave[i].hm) ;
        if(it!=st.begin()) st.erase(--it) ;
        else if(LIM==-1) LIM=cave[i].hm ;
    }
    if(st.empty()) {printf("0\n") ; return 0;}
    if(!dfs(1,-1,INF)) {printf("-1\n") ; return 0 ;}

    int l=0 , r=INF ;
    while(r-l>1)
    {
        int mid=(r+l)/2 ;
        if(dfs(1,-1,mid)) r=mid ;
        else l=mid ;
    }
    printf("%d\n",r) ;
}
Beispiel #20
0
int main(void){
    freopen("painting.in", "r", stdin);
    freopen("painting.out", "w", stdout);
    scanf("%d", &N);
    for(int i = 0; i < N; ++i)
	for(int j = 0; j < 4; ++j)
	    scanf("%d", &a[i][j]);

    for(int i = 0; i < N; ++i){
	x.push_back(make_pair(a[i][0], make_pair(i, true)));
	x.push_back(make_pair(a[i][2], make_pair(i, false)));
    }
    sort(x.begin(), x.end());

    y.insert(make_pair(INF, true));
    int ans = 0;
    for(int i = 0; i < x.size(); ++i){
	int index = x[i].second.first;
	int flag = x[i].second.second;
	if(flag){
	    bool D = (*y.upper_bound(make_pair(a[index][3], false))).second;
	    if(D){
		ans++;
		y.insert(make_pair(a[index][1], true));
		y.insert(make_pair(a[index][3], false));
		in[index] = true;
	    }
	}else{
	    if(in[index]){
		y.erase(y.find(make_pair(a[index][1], true)));
		y.erase(y.find(make_pair(a[index][3], false)));
	    }
	}
    }
    printf("%d\n", ans);
    return 0;
}
int main(){

    int i,j,k,l,test,t=1;

    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

    scanf("%d",&test);

    while(test--){

        scanf("%d",&n);

        set1.clear();

        printf("Case #%d:\n",t++);

        int li,ci;
        for(i=1;i<=n;i++){
            scanf("%d %d",&li,&ci);
            pii now=mp(li,ci);

            if(set1.find(now)!=set1.end()){
                set1.insert(now);
                printf("%d\n",set1.size());
                continue;
            }

            //printf("\n%d %d\n",li,ci);

            while(set1.size()){
                it=set1.upper_bound(now);
                //printf("it= %d-%d\n ",(*it).uu,(*it).vv);
                if(it==set1.end()){
                    break;
                }
                else if((*it).vv>=ci){
                    set1.erase(it);
                }
                else break;
            }


			it=set1.upper_bound(now);

			if(it==set1.begin()){
				set1.insert(now);
			}
			else{
				it--;
				if((*it).vv>ci){
					set1.insert(now);
				}
			}

            for(it=set1.begin();it!=set1.end();it++){
            //    printf("%d-%d  ",(*it).uu,(*it).vv);
            }
            //puts("");
            printf("%d\n",set1.size());
            continue;
        }

        if(test) puts("");
    }

    return 0;
}
int main()
{
    while(~scanf("%d%d%d%d",&w,&h,&n,&m))
    {
        for(int i=1;i<=n;i++)
        {
            int x1,y1,x2,y2;
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);

            square[i].l = min(x1,x2);
            square[i].r = max(x1,x2);
            square[i].b = min(y1,y2);
            square[i].t = max(y1,y2);
        }

        V.clear();

        for(int i=1;i<=n;i++)
        {
            V.push_back(Node(square[i].t,square[i].b,1,square[i].l));
            V.push_back(Node(square[i].t,square[i].b,-1,square[i].r+1));
        }
        V.push_back(Node(1,h,1,w+1));

        sort(V.begin(),V.end());

        int xx = 0,pre = -1;
        long long ans = 0;
        S.clear();
        S.insert(setNode(0,0));
        S.insert(setNode(h+1,h+1));
        int width = h-m+1 > 0 ? h-m+1 : 0;

        while(xx < V.size())
        {
            while(xx+1 < V.size() && V[xx].pos == V[xx+1].pos) xx++;

            ans += (long long)width * (long long)(V[xx].pos - (pre == -1 ? 1 : V[pre].pos));
            for(int i=pre+1;i<=xx;i++)
            {
                if(V[i].kind == 1)
                {
                    multiset<setNode>::iterator It1 = S.upper_bound(setNode(V[i].top,V[i].bot));
                    multiset<setNode>::iterator It2 = --It1;++It1;
                    width -= It1->bot - It2->top - m > 0 ? It1->bot - It2->top - m : 0;
                    width += It1->bot - V[i].top - m > 0 ? It1->bot - V[i].top - m : 0;
                    width += V[i].bot - It2->top - m > 0 ? V[i].bot - It2->top - m : 0;
                    S.insert(setNode(V[i].top,V[i].bot));
                }
                else if(V[i].kind == -1)
                {
                    multiset<setNode>::iterator It1 = S.find(setNode(V[i].top,V[i].bot));if(It1 == S.end())while(1);
                    multiset<setNode>::iterator It2 = --It1;++It1;
                    multiset<setNode>::iterator It3 = ++It1;--It1;
                    width -= It3->bot - It1->top - m > 0 ? It3->bot - It1->top - m : 0;
                    width -= It1->bot - It2->top - m > 0 ? It1->bot - It2->top - m : 0;
                    width += It3->bot - It2->top - m > 0 ? It3->bot - It2->top - m : 0;
                    S.erase(It1);
                }
            }
            pre = xx;
            xx++;
        }
        //-------------------------------------------
        if(m == 1) goto Print;

        V.clear();

        for(int i=1;i<=n;i++)
        {
            V.push_back(Node(square[i].r,square[i].l,1,square[i].b));
            V.push_back(Node(square[i].r,square[i].l,-1,square[i].t+1));
        }
        V.push_back(Node(1,w,1,h+1));

        sort(V.begin(),V.end());

        xx = 0,pre = -1;
        S.clear();
        S.insert(setNode(0,0));
        S.insert(setNode(w+1,w+1));
        width = w-m+1 > 0 ? w-m+1 : 0;

        while(xx < V.size())
        {
            while(xx+1 < V.size() && V[xx].pos == V[xx+1].pos) xx++;

            ans += (long long)width * (long long)(V[xx].pos - (pre == -1 ? 1 : V[pre].pos));
            for(int i=pre+1;i<=xx;i++)
            {
                if(V[i].kind == 1)
                {
                    set<setNode>::iterator It1 = S.upper_bound(setNode(V[i].top,V[i].bot));
                    set<setNode>::iterator It2 = --It1;++It1;
                    width -= It1->bot - It2->top - m > 0 ? It1->bot - It2->top - m : 0;
                    width += It1->bot - V[i].top - m > 0 ? It1->bot - V[i].top - m : 0;
                    width += V[i].bot - It2->top - m > 0 ? V[i].bot - It2->top - m : 0;
                    S.insert(setNode(V[i].top,V[i].bot));
                }
                else if(V[i].kind == -1)
                {
                    set<setNode>::iterator It1 = S.find(setNode(V[i].top,V[i].bot));
                    set<setNode>::iterator It2 = --It1;++It1;
                    set<setNode>::iterator It3 = ++It1;--It1;
                    width -= It3->bot - It1->top - m > 0 ? It3->bot - It1->top - m : 0;
                    width -= It1->bot - It2->top - m > 0 ? It1->bot - It2->top - m : 0;
                    width += It3->bot - It2->top - m > 0 ? It3->bot - It2->top - m : 0;
                    S.erase(It1);
                }
            }
            pre = xx;
            xx++;
        }
        Print:printf("%lld\n",ans);
    }
    return 0;
}
Beispiel #23
0
 /** Return the number of hits in the past 5 minutes.
     @param timestamp - The current timestamp (in seconds granularity). */
 int getHits(int timestamp) {
     auto begin = ss.lower_bound(timestamp-300+1>0? timestamp-300+1:0);
     auto end = ss.upper_bound(timestamp);
     return distance(begin, end);
 }
Beispiel #24
0
int main()
{
 lg i,s1[99],s2[99],j,x,sm,cn,y;
 scanf("%lld%lld%lld",&n,&a,&b);

 n1=n/2;//pahle array ko hi likh  lelete hai n1 pahle array me members ki  count hai 
 n2=n-n1;
 for(i=0;i<n1;i++)
 scanf("%lld",&s1[i]);

 for(i=0;i<n2;i++)
 scanf("%lld",&s2[i]);//chalo dusra aarya inputees

 //For first array
//chalo cool hai
 x1=pow(2,n1);
 
 
 for(i=0;i<x1;i++)
 {
   sm=0;

   for(j=0;j<n1;j++)
     if((i&(1<<j))>0)
       sm+=s1[j];
 
  os.insert(sm);
          
 }

/*cout<<"Printtign first array\n";
for(i=0;i<x1;i++)
cout<<a1[i]<<"  ";
cout<<endl;
*///pahla arra complete

//for second array
 x2=pow(2,n2);
 sm=0;

 for(i=0;i<x2;i++)
 {
   sm=0;

   for(j=0;j<n2;j++)//bhai second array n1 se suru hokar n tak jaega
    if((i&(1<<j))>0)
     sm+=s2[j];
 
 ts.insert(sm);         
 }

//chalo dusra bhi insert hoke stored ho gae


cn=0;
p=os.begin();
for(i=0;i<x1;i++)
{
  
  y=*p;
  p++;

 lb=ts.lower_bound(a-y);

 up=ts.upper_bound(b-y);
 up--;

 if( lb!=os.end() && up!=os.end() )
  { 
     pi=up;
     pi++;
     while(lb!=pi)
     {lb++;cn++;}
    
  }
 else if(lb==os.end() && up!=os.end() )
   {
      while(up!=os.begin() )
      {up--;cn++;}
       cn++;//bcz ek valye chhut rahi hai
   }
 else if(lb!=os.end() && up==os.end() )
   {
       while(lb!=os.end() )
      {lb++;cn++;}
   }

}


printf("%lld\n",cn);
return 0;
}
Beispiel #25
0
int main(){
    while (~scanf("%d%d",&n,&m)){
        S.clear();
        for (int i=1;i<=n;i++){
            scanf("%d",&a[i]);
            S.insert(a[i]);
        }
        if (n==1){
            printf("1\n%d\n",a[1]);
            continue;
        }
        if (n==2){
            if (a[1]+a[2]>m)
                printf("2\n%d %d\n",a[1],a[2]);
            else
                printf("1\n%d %d\n",a[1],a[2]);
            continue;
        }
        memset(bi,true,sizeof(bi));
        sort(a+1,a+n+1);
        b[2]=a[n];
        multiset<int>::iterator it;
        /*
        it=S.lower_bound(b[2]);
        S.erase(it);

        it=S.upper_bound(m-b[2]);
        if (it==S.end()) --it;
        b[1]=(*it);
        S.erase(it);

        for (int i=2;i<n;++i){
            it=S.upper_bound(m-b[i]);
            if (it==S.end()) --it;
            b[i+1]=(*it);
            S.erase(it);
        }
        */
        bool flag=true;
        for (int i=1;i<=n;i++)
        {
            if (flag)
            {
                if (i<n)
                {
                    it=S.end();it--;
                    b[i+1]=(*it);
                    S.erase(it);
                    it=S.upper_bound(m-b[i+1]);
                    if (it==S.end()) it=S.begin();
                    b[i]=(*it);
                    S.erase(it);
                    i++;
                    if (b[i-1]+b[i]<=m)
                        flag=true;
                    else flag=false;
                }
                else
                {
                    it=S.begin();
                    b[i]=(*it);
                    S.erase(it);
                }
            }
            else
            {
                it=S.upper_bound(m-b[i-1]);
                if (it==S.end())
                {
                    it=S.begin();
                    flag=true;
                }
                else flag=false;
                b[i]=(*it);
                S.erase(it);
            }
        }
        int ans=0;
        b[n+1]=0;
        for (int i=1;i<=n;++i){
            if (b[i]+b[i+1]<=m) i++;
            ans++;
        }
        printf("%d\n",ans);
        for (int i=1;i<=n;++i) printf("%d%c",b[i],i==n?'\n':' ');
    }
    return 0;
}