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

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

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

时间: 2024-08-30 18:31:22

【PAT A1025】部分正确->全部正确的相关文章

PAT A1025 PAT Ranking(25)

题目描述 Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhejiang University. Each test is supposed to run simultaneously in several places, and the ranklists will be merged immediately after the test. Now

【算法学习记录-排序题】【PAT A1025】PAT Ranking

Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhejiang University. Each test is supposed to run simultaneously in several places, and the ranklists will be merged immediately after the test. Now it i

PAT A1025 pat ranking

有n个考场,每个考场都有若干数量个考生,现给出各个考场中考生的准考证号和分数,要求将所有考生的分数从高到低排序,并输出 #include<iostream> #include<string.h> #include<algorithm> //#include<map> using namespace std; struct Student { char id[15]; //准考证号 int score; //分数 int location_number; //

有趣的正确错误指标--第三方开源--AnimCheckBox

这个很有趣的正确错误指标通过AnimCheckBox实现,下载地址:https://github.com/lguipeng/AnimCheckBox 代码: activity_main.xml: 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 xmlns:

Effective C++:条款18:让接口容易被正确使用,不易被误用

(一) 看下面这个例子: class Date { public: Date(int month, int day, int year); }; 很有可能引起下面这两个错误: (1)他们也许会以错误的次序传递参数,如:Date d(30, 3, 1995); (2)他们可能传递一个无效的月份或天数,如:Date d(2, 30, 1995); 许多像这类客户端错误. 解决方法:简单的外覆(wrapper types)类型来区别天数.月份.和年份,然后于Date构造函数中使用这些类型: struc

第一个python程序-判断登陆用户名和密码是否正确

#setencoding=utf-8 #用户名和密码输入正确,则登陆成功 #用户名正确密码错误,只再输入密码,有3次机会 #错误3次,则把用户名放入lock中 import os,sys #存放用户名和密码的文件是E:\python\user.txt accounts_file = 'E:\\python\\user.txt' #被锁的用户要写入此文件 lock_file = 'E:\\python\\lock.txt' #先读出来放内存中,免得每次输入用户后进行判断都要打开文件 user =f

Android ImageView 正确使用姿势

转载来自http://mp.weixin.qq.com/s?__biz=MzA3NTYzODYzMg==&mid=2653578233&idx=1&sn=aea773c1e815fdef910fba28d765940b&chksm=84b3b1feb3c438e8372850a36bdcb87fdfb1ca793793a7c9598bcc792aabbb0f417b7a32c989&mpshare=1&scene=1&srcid=1117pJi4bq

判断登陆用户名和密码是否正确-记事本存用户名密码

#setencoding=utf-8 #用户名和密码输入正确,则登陆成功 #用户名正确密码错误,只再输入密码,有3次机会 #错误3次,则把用户名放入lock中 import os,sys #存放用户名和密码的文件是E:\python\user.txt accounts_file = 'E:\\python\\user.txt' #被锁的用户要写入此文件 lock_file = 'E:\\python\\lock.txt' #先读出来放内存中,免得每次输入用户后进行判断都要打开文件 user =f

C#语法——await与async的正确打开方式

C#5.0推出了新语法,await与async,但相信大家还是很少使用它们.关于await与async有很多文章讲解,但有没有这样一种感觉,你看完后,总感觉这东西很不错,但用的时候,总是想不起来,或者不知道该怎么用. 为什么呢?我觉得大家的await与async的打开方式不正确. 正确的打开方式 首先看下使用约束. 1.await 只能在标记了async的函数内使用. 2.await 等待的函数必须标记async. 有没有感觉这是个循环?没错,这就是个循环.这也就是为什么大家不怎么用他们的原因.