寒假训练第一天(01/20)

寒假训练第一天(01/20)

开始集训的第一天,学会了stl(小皮毛),努力的转向C++;

头文件

C++的头文件比C多好多!

#include<bits/stdc++.h>//万能头文件\
#include<stdio.h>
#include<cstdio>//和#include<stdio.h>差不多
#include<cmath>//由#include<math.h>转换而来
#include<iostream>//c++的基础头文件
#include<map>
#include<queue>//queue先进先出
#include<stack>//stack先进后出
#include<string>//包装了std的新C++头文件
#include<cstring>//包装了std的就C头文件
#include<string.h>//旧的C头文件
#include<vector>//顾名思义就是容器
#include<algorithm>//C++的标准模版库

Stack

先进后出,着实是个好玩意。头文件就套进去 #include< stack >

stack<int> q;
q.empty()//堆栈为空则返回真
q.pop()//移除栈顶元素
q.push(item)//在栈顶的元素item
q.size()//返回栈中元素数目
q.top()//返回栈顶元素

String

这个就和C语言里面的char有点小不一样,String是动态字符串,都用不着你给他规定大小
用法嘛,有这么几个

string s1;//生成空字符串s1
string s2="123";
string s3("123");
string s4(s,'a');//s4="aaaaa"
string s5("123456",3);//s5="123"
string s6("123456",2,4);//s6="2345"
string s7=s2+','+s3;//s7="123,123"
s.~string();//销毁所有字符,释放内存

说到string,当然还有substr函数

string s1("Hello");
string s2=s1.substr(2,3);//s2="llo"  从第2+1位开始往后3位
string s3=s1.substr();//s3="Hello"  
string s4=s1.substr(3);//s4="lo" 从第3+1位一直到最后

还有还有

s2.clear();//清空数组
s2.append("155");//s2=“155”
s2.push_back('4');//在尾部添加一个字符
int p=s2.find("12");//找到位置,找不到返回-1
p=s2.find("1");
p=s2.find("1",p+1);//找到下一个1

queue

与stack成敌对,先进先出
这个差差不多,上一道题目看看。

看病要排队这个是地球人都知道的常识。
不过经过细心的0068的观察,他发现了医院里排队还是有讲究的。0068所去的医院有三个医生(汗,这么少)同时看病。而看病的人病情有轻重,所以不能根据简单的先来先服务的原则。所以医院对每种病情规定了10种不同的优先级。级别为10的优先权最高,级别为1的优先权最低。医生在看病时,则会在他的队伍里面选择一个优先权最高的人进行诊治。如果遇到两个优先权一样的病人的话,则选择最早来排队的病人。

现在就请你帮助医院模拟这个看病过程。

Input
输入数据包含多组测试,请处理到文件结束。
每组数据第一行有一个正整数N(0<N<2000)表示发生事件的数目。
接下来有N行分别表示发生的事件。
一共有两种事件:
1:“IN A B”,表示有一个拥有优先级B的病人要求医生A诊治。(0<A<=3,0<B<=10)
2:“OUT A”,表示医生A进行了一次诊治,诊治完毕后,病人出院。(0<A<=3)

Output
对于每个"OUT A"事件,请在一行里面输出被诊治人的编号ID。如果该事件时无病人需要诊治,则输出"EMPTY"。
诊治人的编号ID的定义为:在一组测试中,"IN A B"事件发生第K次时,进来的病人ID即为K。从1开始编号。

Sample Input
7
IN 1 1
IN 1 2
OUT 1
OUT 2
IN 2 1
OUT 2
OUT 1
2
IN 1 1
OUT 1
Sample Output
2
EMPTY
3
1
1
用了优先队列

#include<cstdio>
#include<iostream>
#include<stack>
#include<string>
#include<map>
#include<queue>
#include<algorithm>
#include<string.h>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
struct aaa{
	int num,id;
}c;
bool operator<(const aaa &x,const aaa &y){
	if(x.num==y.num)return x.id>y.id;
	else return x.num<y.num;
}
int main(){
	int n,a,b;
	char aa[10];
	while(~scanf("%d",&n)){
		int k=1;
		priority_queue<aaa>q[10];
		while(n--){
			scanf("%s%d",aa,&a);
			if(strcmp(aa,"IN")==0){
				scanf("%d",&b);
				c.num=b;c.id=k++;
				q[a].push(c);
			}
			else{
				if(!q[a].empty()){
					c=q[a].top();q[a].pop();
					printf("%d\n",c.id);
				}
				else printf("EMPTY\n");
			}
		}
	}
	return 0;
}

看到这里知道这是一个好东西了吧,这里有几个基本操作可以看看

#include<queue>
queue<string> q;
q.back()//返回最后一个元素
q.empty()//当队列为空则返回真
q.front()//返回第一个元素
q.pop()//删除第一个元素
q.push()//在某位加入一个元素
q.size()//返回列表中元素的个数

map

说到queue,当然少不了map。map十分灵活,可以map<string,string> ,也可以套娃 map<string,map<string,string>> 当然有些时候你得在俩’<'间加个空格,有些ide他不行

Contest time again! How excited it is to see balloons floating around. But to tell you a secret, the judges’ favorite time is guessing the most popular problem. When the contest is over, they will count the balloons of each color and find the result.

This year, they decide to leave this lovely job to you.
Input
Input contains multiple test cases. Each test case starts with a number N (0 < N <= 1000) – the total number of balloons distributed. The next N lines contain one color each. The color of a balloon is a string of up to 15 lower-case letters.

A test case with N = 0 terminates the input and this test case is not to be processed.
Output
For each case, print the color of balloon for the most popular problem on a single line. It is guaranteed that there is a unique solution for each test case.
Sample Input
5
green
red
blue
red
red
3
pink
orange
pink
0
Sample Output
red
pink

#include<cstdio>
#include<iostream>
#include<stack>
#include<string>
#include<map>
#include<algorithm>
using namespace std;
int main(){
	int n;
	char name[22];
	while(cin>>n){
		if(!n)break;
		map<string,int> a;
		for(int i=0;i<n;i++){
			cin>>name;
			a[name]++;
		}
		map<string,int>::iterator b,c;
		b=c=a.begin();
		for(;b!=a.end();b++){
			if(b->second>c->second)c=b;
		}
		cout<<c->first<<endl;
	}
	return 0;
} 

做了这个例题,我感觉自己也掌握了不少,我还整理了一份基操

map<string,int> mp;
mp.begin()//返回指向map头部的迭代器
mp.clear()//删除所有元素
mp.count()//返回指定元素出现的次数
mp.empty()//如果map为空则返回true
mp.end()//返回指向map末尾的迭代器
mp.equal_range()//返回特殊条目的迭代器对
mp.erease()//删除一个元素
mp.find()//查找一个元素
mp.get_allocator()//返回map的配置器
mp.insert()//插入元素
mp.key_comp()//返回比较元素key的函数
mp.lower_bound()//返回键值>=给定元素的第一个位置
mp.max_size()//返回课容纳的最大元素个数
mp.rbegin()//返回一个指向map尾部的逆向迭代器
mp.rend()//返回一个指向map头部的逆向迭代器
mp.size()//返回map中元素的个数
mp.swap()//交换两个map
mp.upper_bound()//返回键值>给定元素的第一个位置
mp.value_comp()//返回 比较元素value的函数

vector

它可以容纳许多类型的数据,如若干个整数,所以称其为容器。vector 是C++ STL的一个重要成员,使用它时需要包含头文件:#include< vector >

#include<vector>
vector<int>a;
vector<int>b(2);
vector<int>c({1,2,3,4,5});
vector<int>d(c);
a.assign(b.begin(), b.begin()+3);//b为向量,将b的0~2个元素构成的向量赋给a
a.assign(4,2); //是a只含4个元素,且每个元素为2
a.back(); //返回a的最后一个元素
a.front(); //返回a的第一个元素
a[i]; //返回a的第i个元素,当且仅当a[i]存在2013-12-07
a.clear(); //清空a中的元素
a.empty(); //判断a是否为空,空则返回ture,不空则返回false
a.pop_back(); //删除a向量的最后一个元素
a.erase(a.begin()+1,a.begin()+3); /*删除a中第1个(从第0个算起)到第2个元素,也就是说删除的元素从
a.begin()+1算起(包括它)一直到a.begin()+3(不包括它)*/
a.push_back(5); //在a的最后一个向量后插入一个元素,其值为5
a.insert(a.begin()+1,5); /*在a的第1个元素(从第0个算起)的位置插入数值5,如a为1,2,3,4,插入元素后
为1,5,2,3,4*/
a.insert(a.begin()+1,3,5); //在a的第1个元素(从第0个算起)的位置插入3个数,其值都为5
a.insert(a.begin()+1,b+3,b+6); /*b为数组,在a的第1个元素(从第0个算起)的位置插入b的第3个元素到第5
个元素(不包括b+6),如b为1,2,3,4,5,9,8,插入元素后为1,4,5,9,2,3,4,5,9,8*/
a.size(); //返回a中元素的个数;
a.capacity(); //返回a在内存中总共可以容纳的元素个数
a.resize(10); //将a的现有元素个数调至10个,多则删,少则补,其值随机
a.resize(10,2); //将a的现有元素个数调至10个,多则删,少则补,其值为2
a.reserve(100); /*将a的容量(capacity)扩充至100,也就是说现在测试a.capacity();的时候返回值是100.
这种操作只有在需要给a添加大量数据的时候才显得有意义,因为这将避免内存多次容量扩充操作(当a的容量不足时
电脑会自动扩容,当然这必然降低性能) */
a.swap(b); //b为向量,将a中的元素和b中的元素进行整体性交换
a==b; //b为向量,向量的比较操作还有!=,>=,<=,>,<

最后

还有一点零零碎碎的东西

using namespace std;
const int MAXN=1e5+10;
const int inf=0x3f3f3f3f;
memset(x,inf,sizeof x);
memset(x,0,sizeof x);
memset(x,-1,sizeof x);
for(auto it=s2.begin();it!=s2.end();it++){
		(*is)='6';
		cout<<(*it)<<endl;
     } 
for(int i=0;i<s2.length();i++){
		cout<<i<<s2[i]<<endl;
     }
sort(x+1;x+1+n);//左闭右开
sort(x+1,x+1+n,cmp);
reverse(x+1;x+1+n);//翻转
next_permutation(x+1,x+1+n);//字典序全排列
int a=5,b=2;swap(a,b);//交换
clock_t st=clock();
inline double now_time(){
       return (double)(clock() - st) / CLOCKS_PER_SEC ;
    }//运行时间

Ok 今天就先到这里,寒假的集训第一天,学的还是蛮多的!!!!!
在这拜一拜 今天一定要得到46姐

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值