算法入门刷题笔记Day1 - C - A problem of sorting - D - Triangle Partition

写在前面

好久没更新公众号和博客了,因为最近在研究新的方向,所以很少发文。
笔者接触编程只有一年,这一年间主要研究启发式算法在运筹学中的应用。但是由于编程基础薄弱,在进一步研究复杂运筹学问题时发现基础算法不过关导致写出的代码运行速度很慢,因此很苦恼。所以决定这个暑假补习一下基础算法,主要是刷一些简单的ACM入门题。偶尔会发一些刷题笔记(偶尔!)。和作者有类似目标的同学可以一起交流共勉!

目前在看的教程:
北京理工大学ACM冬季培训课程
算法竞赛入门经典/刘汝佳编著.-2版

课程刷题点
Virtual Judge

下面继续Day1。(上传密码xzmtql)

第二天刷题习惯了一点,但还是遇到很多奇奇怪怪的事情。。。等明天考完试放假了,好好刷一刷。。。哎。。。

Day1-C - A problem of sorting

本来刷了两道题,以为自己明白一点输入输出了,结果这题还是被输入输出搞了一下。

在这里插入图片描述

简单的说就是输入人名+生日年份,按年龄排序。乍一看和A一样,所以写的时候就换了用pair写(上次用的struct)。结果提交WA。再仔细一看,哦,人名可以有space,那直接cin就有问题了。要考查的应该是getline、cin.getline。(这个北理第一讲的学长还特地提到过)

把getline和cin.getline都写了一遍想起来以前查过string转int,可以用int atoi(const char *nptr); str.c_str转string 为char 型数组(我突然发现自己不知道markdown怎么打出两个星号)。大概是这样:

string str;
getline(cin, data);
int num = atoi(data.c_str());

然后第二个坑点,getline()前面如果用了cin读取一行的数据,后面的回车符不读取,getline()的时候就会读取。所以cin或者scanf后要加一个cin.get()。

白花花的时间就浪费在这些没打好的基础上啊!!!

再记一下cin.getline得到的是char*,可以通过strlen得到长度。

Mark一下完整代码:

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

// #define LOCAL

bool comp(pair<string, int>p1, pair<string, int>p2)
{
    return p1.second > p2.second;
}

int main()
{
#ifdef LOCAL
	freopen("data.in", "r", stdin);
	// freopen("data.out", "w", stdout);
#endif
	int caseNr;
    scanf("%d", &caseNr);
    
	for(int i = 0; i < caseNr; i++)
	{
		int peopleNr;
        scanf("%d", &peopleNr);
        cin.get();
        pair<string, int> people[105];

		for (int j = 0; j < peopleNr; j++)
		{
            string name;
            int birthday = 0;
            string data;
            getline(cin, data);
            name = data.substr(0, data.length() - 5);
            for(int k = data.length() - 4; k < data.length() - 1; k++)
            {
                birthday += data[k] - '0';
                birthday *= 10;
            }
            birthday += data[data.length() - 1] - '0';
            
			people[j] = make_pair(name, birthday);
		}

		sort(people, people + peopleNr, comp);

		for (int j = 0; j < peopleNr; j++)
			cout << people[j].first << endl;
	}

	return 0;
}

Day1- D - Triangle Partition

没啥大坑,讲道理应该一遍过的,但不知道为什么WA,哎。
先放着吧,懒得继续找原因了。我发现刷这些题是真的考验细节、耐心,以前高中刷数学高联以为已经很恶了,写运筹代码的时候也觉得哪个够恶了,但都没这个恶。

由衷崇拜搞ACM、NOI这类比赛的大佬。

在这里插入图片描述

又是提到sort题。给好3n个三点不共线的点,要求划分出n个三点集,构成的三角形互不相交。

那不又是字典序吗。我发现自从知道了字典序后,这个东西出现的频率很高啊。

可能要注意一下坐标的范围,不过我查了一下10910^9好像没有越int的界。

完整代码:

#include <iostream>
#include <algorithm>
using namespace std;

#define LOCAL

struct Node
{
    long long x, y;
    int id;

    Node(){
        id = 0;
        x = 0;
        y = 0;
    }

    bool operator < (Node n){
        if(this->x != n.x)
            return this->x < n.x;
        else
            return this->y < n.y;
    }
}nodes[1005];

int main()
{
#ifdef LOCAL
	freopen("data.in", "r", stdin);
	// freopen("data.out", "w", stdout);
#endif
	int t;
    scanf("%d", &t);
    
	while(t--)
	{
        int n = 0;
        scanf("%d", &n);
        n *= 3;
        for(int j = 0; j < n; j++){
            cin >> nodes[j].x >> nodes[j].y;
            nodes[j].id = j + 1;
        }

        sort(nodes, nodes + n);

		for (int j = 0; j < n; j += 3)
			printf("%d %d %d\n", nodes[j].id, nodes[j + 1].id, nodes[j + 2].id);
	}

	return 0;
}

结语

感觉这样刷有点点慢了。。。不过也是因为没彻底放暑假,等放假调到一天4-5题左右看看效果。。。
继续放一下这个系列的github

posted @ 2020-06-28 21:44  舟寒丶  阅读(51)  评论(0编辑  收藏  举报