STL训练赛
A - JiaoZhu and SC
#include <bits/stdc++.h>
#define int long long
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define reps(i, a, b) for (int i = (a); i < (b); i++)
#define per(i, a, b) for (int i = (b); i >= (a); i--)
#define BUFF ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
const int N = 2e5 + 10;
const int INF = 0x3f3f3f3f;
inline int read()
{
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9')
{
if (ch == '-')
f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
{
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
map<string, char> a;
signed main()
{
//freopen("in.txt","r",stdin);
BUFF;
int n, m;
cin >> n >> m;
while (n--)
{
string s;
char xx;
cin >> s >> xx;
a[s] = xx;
}
while (m--)
{
string xx, yy;
cin >> xx >> yy;
if (a[xx] == 'T')
{
if (a[yy] == 'T')
printf("End in a draw!");
else if (a[yy] == 'P')
printf("TianT Wins!");
else if (a[yy] == 'Z')
printf("XiaoM Wins!");
}
else if (a[xx] == 'Z')
{
if (a[yy] == 'T')
printf("TianT Wins!");
else if (a[yy] == 'P')
printf("XiaoM Wins!");
else if (a[yy] == 'Z')
printf("End in a draw!");
}
else if (a[xx] == 'P')
{
if (a[yy] == 'T')
printf("XiaoM Wins!");
else if (a[yy] == 'P')
printf("End in a draw!");
else if (a[yy] == 'Z')
printf("TianT Wins!");
}
puts("");
}
return 0;
}
B - 萌萌哒十五酱的衣服~
#include<bits/stdc++.h>
#define int long long
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define reps(i,a,b) for(int i=(a);i<(b);i++)
#define per(i,a,b) for(int i=(b);i>=(a);i--)
#define BUFF ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
const int N=2e5+10;
const int INF=0x3f3f3f3f;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int mod=1e6;
multiset<int>m;
signed main()
{
//freopen("in.txt","r",stdin);
BUFF;
int n;
int ans=0;//最后的答案
while(cin>>n)
{
m.clear();
int a,b;
ans=0;
int temp=-1;//这个是为了判断是衣服还是裤子而设立的
rep(i,1,n)
{
cin>>a>>b;
if(m.empty())//判断循环是不是第一次进行的
{
m.insert(b);
temp=a;//这个时候把中间值变成第一次的类型
continue;
}
if(temp==a)//如果这次输入的类型和上次一的是一样的,那么就继续插入
{
m.insert(b);
}
else//如果跟上一次的类型不一样了,开始查找
{
// multiset<int>::iterator it1;
// multiset<int>::iterator it2;
auto it1=m.lower_bound(b);//找第一个大于等于b的数字的位置
if(it1==m.end())//如果到了最后也没有找到这个元素的话,那么我们就只能找他的前一个元素
{
it1--;
ans=(ans+abs(*it1-b))%mod;//这步是算出具体的值
m.erase(it1);//删除这个已经确定了的元素
}
else if(it1==m.begin())//开头和解尾要特殊的考虑一下
{
ans=(ans+abs(*it1-b))%mod;
m.erase(it1);
}
else//剩下的就是在中间的,在中间的找到的时候要考虑他的前一个和他自己,看看哪一个更符合题
{
auto it2=it1;
it2--;
if(abs(*it2-b)<=abs(*it1-b))
{
ans=(ans+abs(*it2-b))%mod;
m.erase(it2);
}
else
{
ans=(ans+abs(*it1-b))%mod;
m.erase(it1);
}
}
}
}
cout<<ans<<endl;
}
return 0;
}
C - 逃课的孩子
#include<bits/stdc++.h>
#define int long long
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define reps(i,a,b) for(int i=(a);i<(b);i++)
#define per(i,a,b) for(int i=(b);i>=(a);i--)
#define BUFF ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
const int N=2e5+10;
const int INF=0x3f3f3f3f;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
map<string,bool>a;
signed main()
{
//freopen("in.txt","r",stdin);
BUFF;
int n,m;
while(cin>>n>>m)
{
a.clear();
while(n--)
{
string s;
cin>>s;
a[s]=true;
}
while(m--)
{
string s;
cin>>s;
if(a[s])
{
cout<<"yes\n";
}
else
{
cout<<"no\n";
}
}
}
return 0;
}
D - 店长终极推荐
#include<bits/stdc++.h>
#define int long long
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define reps(i,a,b) for(int i=(a);i<(b);i++)
#define per(i,a,b) for(int i=(b);i>=(a);i--)
#define BUFF ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
const int N=2e5+10;
const int INF=0x3f3f3f3f;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
map<string,int>a;
signed main()
{
int T;
while(cin>>T)
{
getchar();
if(T==0)break;
while(T--)
{
//getchar();
a.clear();
string s;
getline(cin,s);
int maxx=-1;
for(int i=0;i<s.size()-1;i++)
{
string temp="";
temp+=s[i];
temp+=s[i+1];
a[temp]++;
maxx=max(maxx,a[temp]);
}
map<string,int>::iterator it;
for(it=a.begin();it!=a.end();it++)
{
if(it->second==maxx)
{
cout<<it->first<<'\n';
break;
}
}
}
cout<<'\n';
}
return 0;
}
//这个最后说一句就是,这个iOS加速流不知道为什么一遇到字符串的问题他就会出现错误,慎重使用,我也很迷
E - 数字去重和排序II
#include <bits/stdc++.h>
#define int long long
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define reps(i, a, b) for (int i = (a); i < (b); i++)
#define per(i, a, b) for (int i = (b); i >= (a); i--)
#define BUFF ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
const int N = 2e5 + 10;
const int INF = 0x3f3f3f3f;
inline int read()
{
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9')
{
if (ch == '-')
f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
{
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
set<int> a;
signed main()
{
int n;
while (~scanf("%lld",&n))
{
a.clear();
while (n--)
{
int xx;
xx = read();
a.insert(xx);
}
printf("%lld\n",a.size());
for (auto it = a.begin(); it != a.end(); it++)
{
if (it != a.begin())
printf(" ");
printf("%lld", *it);
}
printf("\n");
}
return 0;
}
- 如果你需要高效的随即存取,而不在乎插入和删除的效率,使用vector
- 如果你需要大量的插入和删除,而不关心随即存取,则应使用list
- 如果你需要随即存取,而且关心两端数据的插入和删除,则应使用deque。
F - Train Problem I
#include <bits/stdc++.h>
#define int long long
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define reps(i, a, b) for (int i = (a); i < (b); i++)
#define per(i, a, b) for (int i = (b); i >= (a); i--)
#define BUFF ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
const int N = 2e5 + 10;
const int INF = 0x3f3f3f3f;
inline int read()
{
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9')
{
if (ch == '-')
f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
{
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
stack<char> a;
signed main()
{
int n;
string s1, s2;
while (cin >> n >> s1 >> s2)
{
while (!a.empty())//由于栈的这个数据结构没有clear函数,所以这么清空
a.pop();
string ans = "";//这和事用来存进站还是出战的那个操作
int num = 0;//字符串的光标指向
bool flag = true;//用来判断最后成功与否,看看能不能实现
reps(i, 0, n)
{
while (a.empty() && num < n || !a.empty() && s2[i] != a.top() && num < n)
//两种情况,第一种情况就是当这个栈是空的,也就是说现在的这个火车站里面没有存有火车
//第二种情况也就是这个火车站现在不是空的,里面是有火车的,然后现有的火车的第一个不是相对应应该出来的火车
//而为什么之所以是num<n呢?我的理解就是字符串从0 到n嘛我是从字符串考虑的
{
ans += '1';//这个就是进站
a.push(s1[num]);//然后把第一个字符串也就是代表的火车进站
num++;//字符串向后移一位
}
if (a.top() != s2[i])//入过栈头不符合说明这个队列就是不符合的了,不用担心有情况漏判,仔细看前面的循环的条件就能明白了
{
flag = false;//确定了这个不可以
break;
}
else
{
a.pop();
ans += '0';
}//如果可以的话就把那个火车给出战,然后字符串记录一下出战
}
if (flag)//如果是可以的话,就输出yes和他的顺序,进站1是in,出战0是out
{
puts("Yes.");
reps(i, 0, ans.size())
{
if (ans[i] == '1')
puts("in");
else
puts("out");
}
}
else
puts("No.");//如果不可以的话输出一个no
puts("FINISH");//最后不管怎么样都是以一个finish结尾
}
return 0;
}
G - 士兵队列训练问题
#include <bits/stdc++.h>
#define int long long
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define reps(i, a, b) for (int i = (a); i < (b); i++)
#define per(i, a, b) for (int i = (b); i >= (a); i--)
#define BUFF ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
const int N = 2e5 + 10;
const int INF = 0x3f3f3f3f;
inline int read()
{
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9')
{
if (ch == '-')
f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
{
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
list<int> a;
signed main()
{
int n;
while (~scanf("%lld", &n))
{
while (n--)
{
a.clear();
int x;
x = read();
rep(i, 1, x) a.push_back(i);
int k = 2; //这个就是那个对编号进行判断的操作
while (a.size() > 3)
{
int cnt = 1; //这个就是编号的一个作用
for (auto it = a.begin(); it != a.end(); cnt++) //注意这里是cnt而不是it
//因为这里面涉及到了一个删除的操作,有不理解的看我的
//ACM基础知识STL容器里面的vector里面讲解
{
if (cnt % k == 0)
{
it = a.erase(it);
}
else
{
it++;
}
}
k ^= 1; //这个是位运算
//等同于下面的这个操作
/* if(k==2)k=3;
else k=2; */
}
for (auto it = a.begin(); it != a.end(); it++)
{
if (it != a.begin())
printf(" ");
printf("%lld", *it);
}
printf("\n");
}
}
return 0;
}
H - JiaoZhu and CS
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int M=1e5+5;
struct gun
{
char name[20];
int kill,price;
bool operator < (const gun &that) const
{
if(kill==that.kill)
{
if(price==that.price)
{
return strcmp(name,that.name)<0;
}
return price < that.price;
}
return kill>that.kill;
}
}a[M];
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%s%d%d",&a[i].name,&a[i].kill,&a[i].price);
}
sort(a+1,a+1+n);
for(int i=1;i<=n;i++)
{
printf("%s\n",a[i].name);
}
return 0;
}
整一个不一样的结构体排序用的运算符重载,其实想拿vector写来着后来想一想没必要,结构体一般都是写数组
I - Black Box(思路不清晰)
#include<iostream>
#include<cstdio>
#include<queue>
#define int long long
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define reps(i,a,b) for(int i=(a);i<(b);i++)
#define per(i,a,b) for(int i=(b);i>=(a);i--)
#define BUFF ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
const int N=2e5+10;
const int INF=0x3f3f3f3f;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int a[N],vis[N];
priority_queue<int>q1;//大顶堆
priority_queue<int,vector<int>,greater<int> >q2;//小顶堆
signed main()
{
int n,m;
while(~scanf("%lld%lld",&n,&m))
{
fill(vis,vis+n+5,0);//这个是把一个容器从一个地方到另一个地方的都变成一个值
//和memset的区别其实就是在,memset是初始ascll码,很注意这个问题
while (!q1.empty())
{
q1.pop();
}
while (!q2.empty())
{
q2.pop();
}//没有内置的clear函数,所以这么清空
rep(i,1,n)a[i]=read();
while(m--)
{
int xx;
xx=read();
vis[xx]++;
}
rep(i,1,n)
{
if(!q1.empty()&&q1.top()>a[i])
{
q1.push(a[i]);
q2.push(q1.top());
q1.pop();
}
else
{
q2.push(a[i]);
}
while(vis[i]--)
{
printf("%lld\n",q2.top());
q1.push(q2.top());
q2.pop();
}
}
}
return 0;
}
插个眼,这题代码先上但是思路并不是很清晰
J - 排列2
#include <bits/stdc++.h>
#define int long long
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define reps(i, a, b) for (int i = (a); i < (b); i++)
#define per(i, a, b) for (int i = (b); i >= (a); i--)
#define BUFF ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
const int N = 2e5 + 10;
const int INF = 0x3f3f3f3f;
inline int read()
{
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9')
{
if (ch == '-')
f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
{
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
int a[10];
signed main()
{
int flag = false;
while (~scanf("%lld%lld%lld%lld", &a[0], &a[1], &a[2], &a[3]))
{
if (a[0] + a[1] + a[2] + a[3] == 0)
break;
if (flag)
printf("\n");
stable_sort(a, a + 4); //stable_sort的内部是归并排序,稳定性更好,而且不会对内部顺序造成影响
int temp=a[0];
bool cnt=false;
do
{
if (a[0] == 0)
continue;
if(cnt)
{
if(a[0]==temp)printf(" ");
else printf("\n");
}
printf("%lld%lld%lld%lld",a[0],a[1],a[2],a[3]);
temp=a[0];
cnt=true;
} while (next_permutation(a, a + 4));//全排列的这个函数,它要用到do-while循环
//大佬可以试试dfs
pntf("\n");
flag = true;
}
return 0;
}