码迷,mamicode.com
首页 > 其他好文 > 详细

【PAT A1025】部分正确->全部正确

时间:2019-10-14 19:52:23      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:模拟   ocr   algo   NPU   cstring   end   soc   pre   out   

#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <fstream>
#include <iostream>
#include <string.h>
#include <cstring>
#include <vector>
using namespace std;
#define maxN 105
#define manK 305
#define manS 30010

struct Stu {
    char regist[16];
    int socre;
    int finalRank;
    int locationNumber;
    int localRank;
    Stu() {}
    Stu(char _regist[], int _socre, int _locationNumber) {
        strcpy(this->regist,_regist);
        this->socre =_socre;
        this->locationNumber = _locationNumber;
    }
}static students[manS];

static int N;
static int K;
static int numS;

bool cmp(Stu stu1, Stu stu2) {
    if(stu1.socre != stu2.socre) return stu1.socre > stu2.socre;
    else return strcmp(stu1.regist,stu2.regist) < 0;
}

void groupSort(int K) {
    sort(students + (numS-K), students + numS, cmp);
    Stu *stu = students + (numS-K);
    stu->localRank = 1;
    for (int i = 1; i < K; i++) {
        Stu* preStu = students + (numS-K+i-1);
        Stu* stu = students + (numS-K+i);
        if(stu->socre == preStu->socre) stu->localRank = preStu->localRank;
        else stu->localRank = i+1;
    }
    return;
}

void fileInput() {
    ifstream fin;
    fin.open("/home/zzz/input.txt",ios::in);
    fin >> N;
    numS = 0;
    for (int i = 0; i < N; i++) {
        fin >> K;
        for (int j = 0; j < K; j++) {
            char _regist[16];
            int _score;
            fin >> _regist >> _score;
            students[numS] = Stu(_regist, _score, i+1);
            numS++;
        }
        groupSort(K);
    }
    return;
}

void stdInput() {
    cin >> N;
    numS = 0;
    for (int i = 0; i < N; i++) {
        cin >> K;
        for (int j = 0; j < K; j++) {
            char _regist[16];
            int _score;
            cin >> _regist >> _score;
            students[numS] = Stu(_regist, _score, i + 1);
            numS++;
        }
        groupSort(K);
    }
    return;
}

int main() {
    stdInput();
    cout << numS << endl;
    sort(students, students + numS, cmp);
    Stu* stu = students;
    stu->finalRank = 1;
    printf("%s %d %d %d\n", stu->regist, stu->finalRank, stu->locationNumber, stu->localRank);
    for (int i = 1; i < numS; i++) {
        Stu* stu = students + i;
        Stu* preStu = students + i - 1;
        if(stu->socre == preStu->socre) stu->finalRank = preStu->finalRank;
        else stu->finalRank = i+1;
        printf("%s %d %d %d\n", stu->regist, stu->finalRank, stu->locationNumber, stu->localRank);
    }
    return 0;
}

我把manS放到30005部分正确,把它开到30010即全部正确、、、见了贵了

刷题感悟:

  • 全局变量尽量加上static关键词
  • 结构体数组尽量使用指针
  • 渐渐往高级题目走的时候,不要仍然局限在模拟思维——怎么说,怎么做,学会开动脑筋多加思考,更灵活地解决问题
  • “分而治之”的思想
    • 分组排序是全体排序的子问题
    • 说白了就是一个一模一样的问题,就是处理问题的“域”不一样

【PAT A1025】部分正确->全部正确

标签:模拟   ocr   algo   NPU   cstring   end   soc   pre   out   

原文地址:https://www.cnblogs.com/huangming-zzz/p/11673487.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!