Leetcode刷题笔记24:201912-1-2(加速输入输出 | STL:low_bound()二分查找函数 | const修饰类成员函数 | STL:sort() | 带LAZY标记的线段树)

本文介绍了C++中提高输入输出效率的方法,包括使用`ios::sync_with_stdio(false)`和`cin.tie(nullptr)`。讲解了STL中的`lower_bound`、`upper_bound`和`binary_search`函数的用途,并展示了如何使用它们。同时,讨论了`const`修饰类成员函数的重要性,以及`sort`函数在排序数组时的应用。文章还提供了两个具体编程问题的解决方案,分别是寻找包含7的数字和评估垃圾箱周围格子的分数。通过实例展示了如何利用这些技巧和函数解决问题。
摘要由CSDN通过智能技术生成
第二十一天 2021-4-3 备战CSP
刷题模块:CCF 201912-1-2
每题必须制作多个测试程序:正常输入测试程序逻辑,极大输入测试数据类型定义
CSP:养成习惯,O(1)的参数都定义为long long类型

一、cin.tie与sync_with_stdio加速输入输出

加入如下代码加速输入输出

#include<iostream>
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
}

C++的输出入cin/cout和scanf/printf谁比较快?

二、STL:low_bound | upper_bound

lower_bound(起始地址,结束地址,要查找的数值) 返回的是数值 第一个 出现的位置。
upper_bound(起始地址,结束地址,要查找的数值) 返回的是 第一个大于待查找数值 出现的位置。
binary_search(起始地址,结束地址,要查找的数值)  返回的是是否存在这么一个数,是一个bool值。

在这里插入图片描述

void main()
{
    vector<int> t;
    t.push_back(1);
    t.push_back(2);
    t.push_back(3);
    t.push_back(4);
    t.push_back(6);
    t.push_back(7);
    t.push_back(8);

    
    int low=lower_bound(t.begin(),t.end(),5)-t.begin();
    int upp=upper_bound(t.begin(),t.end(),5)-t.begin();
    cout<<low<<endl;
    cout<<upp<<endl;

    
    system("pause");
}

在这里插入图片描述
C++ lower_bound 与 upper_bound 函数

三、const修饰类成员函数

const 修饰类成员函数,其目的是防止成员函数修改被调用对象的值,如果我们不想修改一个调用对象的值,所有的成员函数都应当声明为 const 成员函数。
C++ const 关键字小结
C++的const类成员函数

bool operator == (const coord& coord2) const{
		return x==coord2.x && y==coord2.y;
	}

四、STL:sort

Sorts the elements in the range [first,last) into ascending order.

The elements are compared using operator< for the first version, and comp for the second.

注意前闭后开区间
若要排序arrry[20];
则sort(array,array+20)
array+20指向最后一个元素位置的后一个位置

sort - C++ Reference

五、201912-1

一遍过,简单
注意仔细观察规律

#include<bits/stdc++.h>
using namespace std;
int n;
int c[4];
int num;

void init(){
	num=1;
	for(int i=0;i<4;i++) c[i]=0;
}

bool isSeven(int num){
	if(num%7==0) return true;
	while(num!=0){
		if(num%10==7) return true;
		num=num/10;
	}
	return false;
}

int main(){
	freopen("in.txt","r",stdin);
	init();
	
	cin>>n;
	for(int i=0;i<n;i++){
		while(isSeven(num)){c[(num-1)%4]++;num++;} 
		num++; 
	}
	
	for(int i=0;i<4;i++){
		cout<<c[i]<<endl;
	}
	
	return 0;
}

六、201912-2

一遍过,使用时间复杂度O(n^2)的暴力搜索

#include<bits/stdc++.h>
#include<vector>

using namespace std;

typedef struct{
	int x;
	int y;
}P;

int n;
int score[5];
vector<P> h;

void init(){
	for(int i=0;i<n;i++) score[i]=0;
}

bool trash(int x,int y){
	for(int i=0;i<h.size();i++){
		if(h[i].x==x && h[i].y==y) return true;
	}
	return false;
}

bool check(int x,int y){
	if(trash(x-1,y) && trash(x,y-1) && trash(x+1,y) && trash(x,y+1)) return true;
	else return false;
}

void calcu(int x,int y){
	int count=0;
	if(trash(x+1,y+1)) count++;
	if(trash(x+1,y-1)) count++;
	if(trash(x-1,y+1)) count++;
	if(trash(x-1,y-1)) count++;
	score[count]++;
}

int main(){
	init();
	freopen("in.txt","r",stdin);
	cin>>n;
	for(int i=0;i<n;i++){
		P p;
		cin>>p.x>>p.y;
		h.push_back(p);
	}
	for(int i=0;i<n;i++){
		if(check(h[i].x,h[i].y)) calcu(h[i].x,h[i].y);
	}
	
	for(int i=0;i<5;i++){
		cout<<score[i]<<endl;
	}
	return 0;
}
贴任神代码,各种神仙操作

初始化列表
运算符重载
排序
二分查找

#include <iostream>
#include <algorithm>

using namespace std;

struct coord {
    long long x, y;
    coord(){}
    coord(long long x, long long y) :x(x), y(y) {}
    bool operator == (const coord& coord2) const {
        return x == coord2.x && y == coord2.y;
    }
    bool operator < (const coord& coord2) const {
        if (x != coord2.x)return x < coord2.x;
        return y < coord2.y;
    }
}arr[10005];
int n;
int XArr[] = {0,0,1,-1,1,1,-1,-1};
int YArr[] = {1,-1,0,0,1,-1,1,-1};
int stat[5];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n;
    for (int i = 0; i < n; i++)
        cin >> arr[i].x >> arr[i].y;
    sort(arr, arr + n);
    for (int i = 0; i < n; i++) {
        int cnter = 0;
        for (int j = 0; j < 4; j++) {
            coord tmp(arr[i].x + XArr[j], arr[i].y + YArr[j]);
            if (*lower_bound(arr, arr + n, tmp) == tmp)
                cnter++;
        }
        if (cnter != 4)continue;
        cnter = 0;
        for (int j = 4; j < 8; j++) {
            coord tmp(arr[i].x + XArr[j], arr[i].y + YArr[j]);
            if (*lower_bound(arr, arr + n, tmp) == tmp)
                cnter++;
        }
        stat[cnter]++;
    }
    for (int i = 0; i < 5; i++) {
        cout << stat[i] << "\n";
    }
}

原文链接

七、202012-5

CSP202012-5 星际旅行 满分详解
太帅了!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值