Problem:
Problem C: Yanagi's Comic
Yanagi is a high school student, and she is called "Hime" by her boyfriend who is a ninja fanboy. She likes picture books very much, so she often writes picture books by herself.
She always asks you to make her picture book as an assistant.
Today, you got asked to help her with making a comic.
She sometimes reads her picture books for children in kindergartens.
Since Yanagi wants to let the children read,
she wants to insert numbers to frames in order.
Of course, such a task should be done by the assistant, you.
You want to do the task easily and fast, so you decide to write a program to output the order of reading frames based on the positions of the frames.
Write a program to print the number of order for each frame.
The comics in this problem satisfy the following rules.
The each comic is drawn on a sheet of rectangle paper.
You can assume that the top side of the paper is on the x-axis.
And you can assume that the right-hand side of the paper is on the y-axis.
The positive direction of the x-axis is leftward.
The positive direction of the y-axis is down direction.
The paper is large enough to include all frames of the comic.
In other words, no area in a frame is out of the paper.
The area of each frame is at least one.
All sides of each frame are parallel to the x-axis or the y-axis.
The frames can't overlap one another.
However the frames may share the line of side.
You must decide the order to read
K
, where
K
is a set of frames, with the following steps.
Step 1
:You must choose an unread frame at the right-most position in
K
.
        If you find such frames multiple, you must choose the frame at the top position among them.
        If you can't find any unread frames, you stop reading
K
.
        The frame you chose is "Starting Frame".
        In step 1, you assume that the position of a frame is the top-right point of the frame.
Step 2
:You should draw a line from the right-bottom point of "Starting Frame" to leftward horizontally.
        If the line touch the right-hand side of other frame, you should stop drawing.
        If the line is on the side of other frame, you should not stop drawing.
        If the line touch the edge of paper, you should stop drawing.
Step 3
:The set of the unread frames which are above the line ( they may be the bottom-side on the line ) drawn at Step 2 is
L
.
        And you should read
L
.
Step 4
: Go back to Step 1.
Input
The input consists of multiple datasets.
The last dataset is followed by a line containing one zero.
You don't have to process this data.
Each datasets is in the following format.
n
x
01
y
01
x
11
y
11
x
02
y
02
x
12
y
12
....
x
0n
y
0n
x
1n
y
1n
All numbers of the datasets are integer.
They are separated by a space.
The first line of a dataset contains one integer.
n
is the number of frames of the comic.
x
0i
,
y
0i
,
x
1i
,
y
1i
are two non-neighboring points of
i
th frame in the line.
Constraints
0 <
n
< 10
(
x
0i
!=
x
1i
) and (
y
0i
!=
y
1i
)
0 ≤
x
0i
,
y
...(truncated)

Submitted solutions:

code_snippet: #include<iostream>
#include<vector>
#include<string>
#include<cstring>
#include<iomanip>
#include<algorithm>

using namespace std;

#define debug(x) cout<< #x <<" : "<< x << endl;
#define repi(i, x, n) for(int i = x; i < n; i++)
#define rep(i, n) repi(i, 0, n);
#define int long long

const int INF = (1<<30);
const int MOD = (int)1e9 + 7;

const int MAX = 510;

#define x0 first.first.first
#define y0 first.first.second
#define x1 first.second.first
#define y1 first.second.second

int con;
vector<vector<int>> field;
vector<int> ans;

void solve(vector<pair<pair<pair<int,int>,pair<int,int>>,int>> p){
    if(p.size() == 0) return;
    
    sort(p.begin(), p.end());

    ans.push_back(p[0].second);

    // cout<<"id =  "<<p[0].second<<" size"<<p.size()<<" "<<p[0].x0<<" "<<p[0].y0<<" "<<p[0].x1<<" "<<p[0].y1<<endl;

    int num1 = p[0].x0, num2 = p[0].x1;
    
    for(int i = p[0].x1+1; i < MAX; i++){
        if(i == 501 || field[p[0].y1][i] != 0 && field[p[0].y1-1][i] != 0 && field[p[0].y1][i] == field[p[0].y1-1][i]){
            // cout<<"i = "<<i<<" "<<p[0].y1<<" [][] "<<field[p[0].y1+1][i]<<" "<<field[p[0].y1][i]<<endl;
            num2 = i-1;
            break;
        }
    }

    vector<pair<pair<pair<int,int>,pair<int,int>>,int>> b, c;
    for(int i = 1; i < p.size(); i++){
        // cout<<"i = "<<i<<" "<<num1<<" "<< p[i].x0<<" "<<p[i].x1<<" "<<num2<<"   <><> "<<p[i].y1<<" <= "<<p[0].y1<<endl;
        if(num1 <= p[i].x0 && p[i].x1 <= num2 &&p[i].y1 <= p[0].y1) b.push_back(p[i]);
        else c.push_back(p[i]);
    }

    solve(b);
    solve(c);
    
}

signed main(){
    int t = 0;

    while(true){
        vector<pair<pair<pair<int,int>,pair<int,int>>,int>> p;
        int n;

        

        cin>>n;

        if(!n) break;

        if(t++) cout<<endl;
        p.resize(n);
        
        ans.clear();
        //ns.resize(n+1);
        
        field.clear();
        field.resize(MAX, vector<int>(MAX, 0));
    
        for(int i = 0; i < n; i++){
            cin>>p[i].x0>>p[i].y0>>p[i].x1>>p[i].y1;

            p[i].second =  i+1;
            if(p[i].x0 > p[i].x1){
                swap(p[i].first.first, p[i].first.second);
            }

            if(p[i].y0 > p[i].y1){
                swap(p[i].y0, p[i].y1);
            }

        
            for(int j = p[i].y0; j < p[i].y1; j++){
                for(int k = p[i].x0; k < p[i].x1; k++){
                    field[j][k] = i+1;
                }
            }
        }

        solve(p);

        vector<int> test(ans);

        for(int i = 0; i < ans.size(); i++){
            test[ans[i]-1] = i;
        }
        for(int i = 0; i < ans.size(); i++){
               cout<<test[i]+1<<endl;
        }
        //cout<<endl;
    }
   

    return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 4916
final_rank: 5

code_snippet: #include<iostream>
#include<vector>
#include<string>
#include<cstring>
#include<iomanip>
#include<algorithm>

using namespace std;

#define debug(x) cout<< #x <<" : "<< x << endl;
#define repi(i, x, n) for(int i = x; i < n; i++)
#define rep(i, n) repi(i, 0, n);
#define int long long

const int INF = (1<<30);
const int MOD = (int)1e9 + 7;

const int MAX = 510;

#define x0 first.first.first
#define y0 first.first.second
#define x1 first.second.first
#define y1 first.second.second

int con;
vector<vector<int>> field;
vector<int> ans;

void solve(vector<pair<pair<pair<int,int>,pair<int,int>>,int>> p){
    if(p.size() == 0) return;
    
    sort(p.begin(), p.end());

    ans.push_back(p[0].second);

    // cout<<"id =  "<<p[0].second<<" size"<<p.size()<<" "<<p[0].x0<<" "<<p[0].y0<<" "<<p[0].x1<<" "<<p[0].y1<<endl;

    int num1 = p[0].x0, num2 = p[0].x1;
    
    for(int i = p[0].x1+1; i < MAX; i++){
        if(i == 501 || field[p[0].y1][i] != 0 && field[p[0].y1-1][i] != 0 && field[p[0].y1][i] == field[p[0].y1-1][i]){
            // cout<<"i = "<<i<<" "<<p[0].y1<<" [][] "<<field[p[0].y1+1][i]<<" "<<field[p[0].y1][i]<<endl;
            num2 = i-1;
            break;
        }
    }

    vector<pair<pair<pair<int,int>,pair<int,int>>,int>> b, c;
    for(int i = 1; i < p.size(); i++){
        // cout<<"i = "<<i<<" "<<num1<<" "<< p[i].x0<<" "<<p[i].x1<<" "<<num2<<"   <><> "<<p[i].y1<<" <= "<<p[0].y1<<endl;
        if(num1 <= p[i].x0 && p[i].x1 <= num2 &&p[i].y1 <= p[0].y1) b.push_back(p[i]);
        else c.push_back(p[i]);
    }

    solve(b);
    solve(c);
    
}

signed main(){
    int t = 0;

    while(true){
        vector<pair<pair<pair<int,int>,pair<int,int>>,int>> p;
        int n;

        

        cin>>n;

        if(!n) break;

        if(t++) cout<<endl;
        p.resize(n);
        
        ans.clear();
        //ns.resize(n+1);
        
        field.clear();
        field.resize(MAX, vector<int>(MAX, 0));
    
        for(int i = 0; i < n; i++){
            cin>>p[i].x0>>p[i].y0>>p[i].x1>>p[i].y1;

            p[i].second =  i+1;
            if(p[i].x0 > p[i].x1){
                swap(p[i].first.first, p[i].first.second);
            }

            if(p[i].y0 > p[i].y1){
                swap(p[i].y0, p[i].y1);
            }

        
            for(int j = p[i].y0; j < p[i].y1; j++){
                for(int k = p[i].x0; k < p[i].x1; k++){
                    field[j][k] = i+1;
                }
            }
        }

        solve(p);

        vector<int> test(ans);

        for(int i = 0; i < ans.size(); i++){
            test[ans[i]-1] = i;
        }
        for(int i = 0; i < ans.size(); i++){
               cout<<test[i]+1<<endl;
        }
        //cout<<endl;
    }
   

    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 4936
final_rank: 4

code_snippet: //08
#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;

int o[10],r;
int g[502][501];

struct K{
  int l,r,t,b,n;
  bool operator<(K a)const{
    if(l!=a.l){
      return l<a.l;
    }else{
      return t<a.t;
    }
  }
};


void dfs(vector<K> v){
  if(v.empty())return ;
  int x=min_element(v.begin(),v.end())-v.begin();
  o[v[x].n]=++r;
  int y=v[x].b;
  int b;
  for(b=v[x].r;b<500;b++){
    if(g[y][b]==g[y-1][b]&&g[y][b]!=-1)break;
  }
  vector<K> n[2];
  for(int i=0;i<v.size();i++){
    if(i!=x){
      n[v[i].b<=y&&v[i].r<=b].push_back(v[i]);
    }
  }
  dfs(n[1]);
  dfs(n[0]);
}
  
int main(){
  bool ns=false;
  for(int n;cin>>n,n;){
    vector<K> v(n);
    fill(g[0],g[502],-1);
    for(int i=0;i<n;i++){
      v[i].n=i;
      cin>>v[i].l>>v[i].t>>v[i].r>>v[i].b;
      if(v[i].l>v[i].r){
	swap(v[i].l,v[i].r);
      }
      if(v[i].t>v[i].b){
	swap(v[i].t,v[i].b);
      }
      for(int k=v[i].t;k<v[i].b;k++){
	for(int j=v[i].l;j<v[i].r;j++){
	  g[k][j]=i;
	}
      }
    }
    r=0;
    dfs(v);
    if(ns){
      cout<<endl;
    }
    ns=true;
    for(int i=0;i<n;i++){
      cout<<o[i]<<endl;
    }
  }
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 2200
final_rank: 3

code_snippet: #include <iostream>
#include <complex>
#include <set>
#include <vector>
#define x(p) p.real()
#define y(p) p.imag() 

namespace std{
	bool operator < (const complex<int> &a,const complex<int> &b){
		return x(a) != x(b) ? x(a) < x(b) : y(a) < y(b);
	}
};
using namespace std;
typedef complex<int> P;
typedef pair<P,int> node;

P size[10];
P pos[10];

vector<int> used;

void search(set<node> K){
	while(!K.empty()){
		node get = *K.begin();
		K.erase(get);
		used.push_back(get.second);
		y(get.first) += y(size[get.second]);
		while(x(get.first) != 501){
			bool f = false;
			for(set<node>::iterator it = K.begin(); it != K.end() ; ++it){
				if( y((*it).first) < y(get.first) && y(get.first) < y((*it).first) + y(size[(*it).second]) && x((*it).first) == x(get.first) ){
					f = true; break;
				}
			}
			if(f) break;
			x(get.first)++;
		}
		set<node> R;
		for(set<node>::iterator it = K.begin(); it != K.end() ; ++it){
			if( y(get.first) >= y((*it).first) + y(size[(*it).second]) && x((*it).first)+x(size[(*it).second]) <= x(get.first) ){
				R.insert(*it);
			}
		}
		for(set<node>::iterator it = R.begin(); it != R.end() ; ++it)
			K.erase(*it);
		search(R);
	}
}
int main(){
	int n;
	int cnt = 0;
	while(cin >> n , n){
		used.clear();
		if(cnt++)cout << endl;
		for(int i = 0 ; i < n ; i++){
			int x1 , x2 , y1 , y2;
			cin >> x1 >> y1 >> x2 >> y2;
			if(x1 > x2) swap(x1,x2);
			if(y1 > y2) swap(y1,y2);
			pos[i]  = P(x1,y1);
			size[i] = P(x2-x1,y2-y1);
		}
		set<node> L;
		
		for(int i = 0 ; i < n ; i++)
			L.insert(make_pair(pos[i],i));
		
		search(L);
		
		int view[10] = {0};
		for(int i = 0 ; i < n ; i++)
			view[used[i]] = i+1;
		for(int i = 0 ; i < n ; i++)
			cout << view[i] << endl;
	}
}
accuracy: 1
time_ms: 10
memory_kb: 0
final_rank: 1

code_snippet: #include <iostream>
#include <complex>
#include <set>
#include <vector>
#define x(p) p.real()
#define y(p) p.imag() 

namespace std{
	bool operator < (const complex<int> &a,const complex<int> &b){
		return x(a) != x(b) ? x(a) < x(b) : y(a) < y(b);
	}
};
using namespace std;
typedef complex<int> P;
typedef pair<P,int> node;

P size[10];
P pos[10];

vector<int> used;

set<node> search(set<node> K){
	if(K.empty()) return K;
	node get = *K.begin();
	K.erase(get);
	used.push_back(get.second);
	y(get.first) += y(size[get.second]);
	while(x(get.first) != 501){
		bool f = false;
		for(set<node>::iterator it = K.begin(); it != K.end() ; ++it){
			if( y((*it).first) < y(get.first) && y(get.first) < y((*it).first) + y(size[(*it).second]) && x((*it).first) == x(get.first) ){
				f = true; break;
			}
		}
		if(f) break;
		x(get.first)++;
	}
	set<node> R;
	for(set<node>::iterator it = K.begin(); it != K.end() ; ++it){
		if( y(get.first) >= y((*it).first) + y(size[(*it).second]) && x((*it).first)+x(size[(*it).second]) <= x(get.first) ){
			R.insert(*it);
		}
	}
	for(set<node>::iterator it = R.begin(); it != R.end() ; ++it){
		K.erase(*it);
	}
	while(R.size() != 0){
		R = search(R);
	}
	return K;
}
int main(){
	int n;
	int cnt = 0;
	while(cin >> n , n){
		used.clear();
		if(cnt++)cout << endl;
		for(int i = 0 ; i < n ; i++){
			int x1 , x2 , y1 , y2;
			cin >> x1 >> y1 >> x2 >> y2;
			if(x1 > x2) swap(x1,x2);
			if(y1 > y2) swap(y1,y2);
			pos[i]  = P(x1,y1);
			size[i] = P(x2-x1,y2-y1);
		}
		set<node> L;
		for(int i = 0 ; i < n ; i++)
			L.insert(make_pair(pos[i],i));
		while(L.size() != 0){
			L = search(L);
		}
		int view[10] = {0};
		for(int i = 0 ; i < n ; i++)
			view[used[i]] = i+1;
		for(int i = 0 ; i < n ; i++)
			cout << view[i] << endl;
	}
}
accuracy: 1
time_ms: 10
memory_kb: 0
final_rank: 1