PAT A1109 Group Photo

PAT A1109 Group Photo

PAT A1109 Group Photo_第1张图片

Sample Input:

10 3
Tom 188
Mike 170
Eva 168
Tim 160
Joe 190
Ann 168
Bob 175
Nick 186
Amy 160
John 159

Sample Output:

Bob Tom Joe Nick
Ann Mike Eva
Tim Amy John
  • Wrong :
    PAT A1109 Group Photo_第2张图片
  • 思路 1:
  1. 初始化一个增量数组:0 0 -1 1 -2 2 -3 3 -4 4…用来求每行:从中间开始插入,每次的插入位置:cen+0, cen-1, cen+1, cen-2, cen+2…
  2. 将所有元素从高到低排序,设置一个全局idex代表当前待填入元素,每次选一个人,按行来依次填入结果矩阵(第一行人数单独求:extra)
  3. 对每行元素的填法:先求出中间位置cen,这一行第i个人应填入的位置为:cen+aux[i],当所有人都填入(即,i达的该行人数时)退出循环,重新开始下一行
  • code 1:
#include 
using namespace std;
const int maxn = 10010;
struct Stu{
	string name;
	int high;
}stu[maxn];

bool cmp(Stu a, Stu b){
	return a.high != b.high ? a.high > b.high : a.name < b.name;
}
int auxiliary[2][maxn/2], aux[maxn];
void init(){	//得到aux[] = 0 0 -1 1 -2 2 -3 3 -4 4 -5 5 -6 6  
	for(int i = 0; i < 2; ++i){
		for(int j = 0; j < maxn/2; ++j){
			auxiliary[i][j] = i > 0 ? j : -j;
		}
	}
	int idex = 0;
	for(int i = 0; i < maxn/2; ++i){
		for(int j = 0; j < 2; ++j){ 
			aux[idex++] = auxiliary[j][i];
		}
	}
}
int idex = 0;
string ans[15][1500];	//!!!Wrong 1:注意题目的范围条件!!! 
void writeAns(int rowNum, int colNum){
	int cnt = 1, cen = colNum/2+1;
	while(cnt <= colNum){
        int next = cen + aux[cnt++];
		ans[rowNum][next] = stu[idex++].name;
	}
}
void Output(int rowNum, int colNum){
	for(int i = 1; i <= colNum; ++i){
		if(i==1) cout << ans[rowNum][i];
		else cout << " " << ans[rowNum][i];
	}
}
int main(){
	int num, row;
	scanf("%d %d", &num, &row);
	for(int i = 0; i < num; ++i){
		cin >> stu[i].name >> stu[i].high;
	}
	init();	
	sort(stu, stu+num, cmp);
	int colNum = num/row;
	int extra = num - (row - 1) * colNum;
	for(int i = 1; i <= row; ++i){
		if(i == 1) writeAns(i, extra);
		else writeAns(i, colNum);
	}
	for(int i = 1; i <= row; ++i){
		if(i == 1) Output(i, extra);
		else Output(i, colNum);
		printf("\n");
	}
	return 0;
}
  • 思路 2:使用双端队列deque[row],实现每行反复左右插

  • code:

#include 
using namespace std;
const int maxn = 10010;
struct Stu{
	string name;
	int height;
}stu[maxn];
bool cmp(Stu a, Stu b){
	return a.height != b.height ? a.height < b.height : a.name > b.name;
}
int main(){
	int n, row;
	scanf("%d %d", &n, &row);
	int col = n / row;
	for(int i = 1; i <= n; ++i){
		cin >> stu[i].name >> stu[i].height;
	}
	sort(stu + 1, stu + n + 1, cmp);
	deque<string> dq[row + 1]; 
	int l = 1, r;
	for(int i = row; i > 0; --i){
		r = i == 1 ? n : l + col - 1;
		int j = r;
		while(j >= l){
			if(j >= l){
				dq[i].push_back(stu[j].name);	
				j--;	
			}	
			if(j >= l){
				dq[i].push_front(stu[j].name);
				j--;
			} 
		} 
		l = r + 1;
	}
	for(int i = 1; i <= row; ++i){
		while(!dq[i].empty()){
			string tmp = dq[i].front();
			dq[i].pop_front();
			cout << tmp;
			if(!dq[i].empty()) cout << " ";
		}
		cout << endl;
	}
	return 0;
}
  • T3 code:
#include 
using namespace std;
const int maxn = 10010;
struct Stu
{
    string name;
    int height;
    bool operator < (const Stu &tmp) const
    {
        return height != tmp.height ? height < tmp.height : name > tmp.name;
    }
};
priority_queue<Stu> pq;
int main()
{
    int n, rows;
    scanf("%d %d", &n, &rows);
    for(int i = 0; i < n; ++i)
    {
        Stu tmp;
        cin >> tmp.name >> tmp.height;
        pq.push(tmp);
    }
    deque<string> dq[rows];
    for(int i = 0; i < rows; ++i)
    {
        int cols = n / rows;
        int len = i != 0 ? cols : cols + n % cols;
        for(int j = 0; j < len; ++j)
        {
            if(j % 2 == 0) dq[i].push_back(pq.top().name);
            else dq[i].push_front(pq.top().name);

            pq.pop();
        }
    }
    for(int i = 0; i < rows; ++i)
    {
        for(int j = 0; j < dq[i].size(); ++j)
        {
            cout <<dq[i][j];
            if(j < dq[i].size() - 1) cout << " ";
        }
        cout <<endl;
    }
    return 0;
}

你可能感兴趣的:(PAT,A,sort,模拟)