PAT--1109. Group Photo

28 篇文章 0 订阅

link to problem

题解

模拟怎么排队拍照。
deque模拟向左向右入队..

#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <cstdio>
#include <algorithm>
using namespace std;

struct Student{
    string name;
    int height;
    Student(){}
    Student(string n, int h):name(n), height(h){}
};
vector<Student> vec;
int n, k;

int main(){
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif // ONLINE_JUDGE

    cin >> n >> k;
    string s;
    int h;
    for(int i = 0; i < n; ++i){
        cin >> s >> h;
        vec.push_back(Student(s, h));
    }

    sort(vec.begin(), vec.end(), [](Student s1, Student s2){
         if(s1.height != s2.height) return s1.height > s2.height;
         return s1.name < s2.name;
          });

    int rows = n / k;
    deque<Student> Q;
    int i = 0;
    for(i = 0; i < rows + n % k; ++i){
        if(i & 1) Q.push_front(vec[i]);
        else Q.push_back(vec[i]);
    }
    for(int j = 0; j < Q.size(); ++j){
        if(j) cout << " ";
        cout << Q[j].name;
    }
    cout << endl;

    for(int j = 0; j < k - 1; ++j){
        Q.clear();
        int t = i;
        bool flag = true;
        for(; i < t + rows; ++i){
            if(flag){
                Q.push_back(vec[i]);
                flag = !flag;
            }else{
                Q.push_front(vec[i]);
                flag = !flag;
            }
        }
        for(int x = 0; x < Q.size(); ++x){
            if(x) cout << " ";
            cout << Q[x].name;
        }
        cout << endl;
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值