PAT甲级1109 Group Photo:[C++题解]双指针
生活随笔
收集整理的這篇文章主要介紹了
PAT甲级1109 Group Photo:[C++题解]双指针
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
文章目錄
- 題目分析
- 題目來源
題目分析
來源:acwing
分析:
雙指針:從中點(diǎn)分別往兩側(cè)移動。
AC代碼
#include<bits/stdc++.h> using namespace std; const int N = 1e4+10; struct Person{string id;int h;bool operator<(const Person& t)const{if(h != t.h) return h > t.h;return id < t.id;} }p[N];string line[N]; int main(){int n , k;cin >> n>> k;int num = n/k;for(int i = 0; i < n; i ++) cin>>p[i].id >> p[i].h;sort(p,p+n);//j用來遍歷所有人//i來遍歷行for(int i = 0, j = 0; i<k ;i++){int len = n/k;if(!i) len += n %k; //特判最后一排for(int r = len/2+1, l = r -1; l> 0 || r<= len; l--,r++){if(r<=len) line[r] =p[j++].id;if(l> 0) line[l] = p[j++].id;}cout<<line[1];for(int m = 2; m<=len; m++) cout<<" "<< line[m];cout<<endl;}}題目來源
PAT甲級1109 Group Photo
https://www.acwing.com/problem/content/1601/
總結(jié)
以上是生活随笔為你收集整理的PAT甲级1109 Group Photo:[C++题解]双指针的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PAT甲级1129 Recommenda
- 下一篇: PAT甲级1132 Cut Intege