DAY1小题
求逆序对的板子题
#include<cstdio>
#define ll long long
using namespace std; const int maxn=1e5+;
ll a[maxn],r[maxn],n;
ll ans=; void msort(ll s,ll t)
{
if(s==t)
return;
ll mid=s+t>>;
msort(s,mid),msort(mid+,t);
ll i=s,j=mid+,k=s;
while(i<=mid&&j<=t)
if(a[i]<=a[j])
r[k++]=a[i++];
else
r[k++]=a[j++],ans+=(ll)mid-i+;
while(i<=mid)
r[k]=a[i],k++,i++;
while(j<=t)
r[k]=a[j],k++,j++;
for(int i=s; i<=t; i++)
a[i]=r[i];
}
inline ll read()
{
char ch=getchar();
ll sum=,k=;
while(ch<''||ch>'')
{
if(ch=='-')
k=-;
ch=getchar();
}
while(ch>=''&&ch<='')
sum=sum*+(ch^),ch=getchar();
return sum*k;
}
int main()
{
scanf("%lld",&n);
for(int i=; i<=n; i++)
a[i]=read();
msort(,n);
printf("%lld\n",ans);
return ;
}
unsigned long long等数据类型的定义方法的巧用
(直接long long居然也可以水到90分.)
#include<cstdio>
using namespace std;
int main()
{
unsigned long long n;
scanf("%llu",&n);
printf("%llu",n*n);
return ;
}
单调队列
对于每一个数记录他是第几个放入的
放入时遇到比他小的元素就弹掉
这样保证了队首就是最大值
进行弹出操作时只要关注弹的次数是否等于队首元素id就可以了
(copy大佬的题解)
#include<cstdio>
#include<utility>
#include<deque>
#include<algorithm>
using namespace std;
int n,num,cnt;
deque<pair<int,int> >q;
void solu1(int x,int n)
{
while(q.size() != && q.back().first< x)
{
q.pop_back();
}
q.push_back(make_pair(x,n));
}
void solu2(int x)
{
while(q.front().second <= x && q.size() != )
q.pop_front();
}
int main()
{
scanf("%d",&n);
int opt;
for(int i = ; i <= n; i++)
{
scanf("%d",&opt);
if(opt == )
{
int x;
scanf("%d",&x);
solu1(x,++num);
}
if(opt == )
{
solu2(++cnt);
}
if(q.size() == )
printf("-1\n");
else
printf("%d\n",q.front().first);
}
return ;
}
(大概是我写两个cmp是不对的
(一个就够了qwq)
(于是爆0.
#include<cstdio>
#include<algorithm>
#include<deque>
using namespace std; struct shu
{
int x,y;
};
shu c[maxn+];
int n; bool cmp(shu a,shu b)
{
if(a.x==b.x) return a.y<b.y;
return a.x<b.x;
} int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d%d",&c[i].x,&c[i].y);
}
sort(c+,c+n+,cmp);
for(int i=;i<=n;i++)
{
printf("%d %d\n",c[i].x,c[i].y);
}
return ;
}
双向队列的基本操作
#include<cstdio>
#include<algorithm>
#include<deque>
using namespace std;
int n,opt;
deque<int>q;
int main()
{
scanf("%d",&n);
for(int i = ; i <= n; i++)
{
scanf("%d",&opt);
if(opt == )
{
int x;
scanf("%d",&x);
if(q.size() == )
printf("-1\n");
else
printf("%d\n",q.back());
q.push_back(x);
}
else if(opt == )
{
if(q.size() == )
printf("-1\n");
else
{
printf("%d\n",q.back());
q.pop_back();
}
}
else if(opt == )
{
int x,y;
scanf("%d%d",&x,&y);
if(q.size()<=x)
printf("-1\n");
else
{
printf("%d\n",q[x]);
q[x]=y;
}
}
else if(opt == )
{
int x;
scanf("%d",&x);
if(q.size() == )
printf("-1\n");
else
printf("%d\n",q.front());
q.push_front(x);
}
else if(opt == )
{
if(q.size() == )
printf("-1\n");
else
{
printf("%d\n",q.front());
q.pop_front();
}
}
}
return ;
}
开两个栈,一个用来模拟真实的栈,另一个求最大值。
每当加入新元素时
如果该元素大于等于栈顶元素 就压进去
弹出元素时,如果真实栈的弹出元素和最大值栈的栈顶元素相等,就弹出。
(qxt好gou啊.
//实现一个栈,支持push和pop,每次执行完毕后要输出栈内元素的最大值
#include<cstdio>
#include<stack>
using namespace std;
stack<int> q,pq;
int n;
void solu1(int x)
{
q.push(x);
if(pq.size() == || pq.top() <= x)
pq.push(x);
}
void solu2()
{
if(q.size())
{
if(pq.size())
if(q.top() == pq.top())
{
q.pop();
pq.pop();
}
else
q.pop();
else
q.pop();
}
}
int main()
{
scanf("%d",&n);
int opt;
for(int i = ; i <= n; i++)
{
scanf("%d",&opt);
if(opt == )
{
int x;
scanf("%d",&x);
solu1(x);
}
if(opt == )
{
solu2();
} if(pq.size() == )
printf("-1\n");
else
printf("%d\n",pq.top());
}
return ;
}
DAY1小题的更多相关文章
- 常让人误解的一道js小题
一道小题引发的深思 今天无意中看到一个js笔试题,不由得想起初学js那会被各种题目狂虐的心酸,虽说现在也会被笔试题所虐,但毕竟比之前好了很多,下面就是我的个人理解,欢迎拍砖.指正: var x = 1 ...
- 一些js小题(一)
一些js小题,掌握这些对于一些常见的面试.笔试题应该很有帮助: var a=10; function aa(){ alert(a); } function bb(){ aa(); } bb();//1 ...
- 关于理解python类的小题
今天看了python部落翻译的一篇<一道python类的小题>文章,感觉挺有启发性,记录下来: print('A') class Person(object): print('B') de ...
- 20181014xlVBA获取小题零分名单
Sub GetZeroName() Dim Dic As Object Const SUBJECT = "科目名称" Dim Key As String Dim OneKey Di ...
- 关于SQL的几道小题详解
关于SQL的几道小题详解 当我们拿到题目的时候,并不是急于作答,那样会得不偿失的,而是分析思路,采用什么方法,达到什么目的,还要思考有没有简单的方法或者通用的方法等等,这样才会达到以一当十的效果,这样 ...
- CF上的3道小题(2)
CF上的3道小题(2) T1:CF630K Indivisibility 题意:给出一个数n,求1到n的数中不能被2到9中任意一个数整除的数. 分析:容斥一下,没了. 代码: #include < ...
- CF上的3道小题(1)
CF上的3道小题 终于调完了啊.... T1:CF702E Analysis of Pathes in Functional Graph 题意:你获得了一个n个点有向图,每个点只有一条出边.第i个点的 ...
- python 小题
python 小题:给定一个字符串,找出不含有重复字符的最长子串的长度.示例 1:输入: "abcabcbb"输出: 3 解释: 无重复字符的最长子串是 "abc&quo ...
- 牛客寒假基础集训营 | Day1 J题—u's的影响力(水题)
Day1 J题-u's的影响力 有一天,kotori发现了一个和lovelive相似的游戏:bangdream.令她惊讶的是,这个游戏和lovelive居然是同一个公司出的! kotori经过一段时间 ...
随机推荐
- hdu2328 后缀树
#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #in ...
- rke安装k8s cluster配置
rke安装k8s cluster配置 最简配置 cluster.yml nodes: - address: 192.168.0.103 user: lishikai role: [controlpla ...
- RPC 学习(一)认识
文章部分描述来自参考资料 RPC 什么是RPC RPC(Remote Procedure Call)-远程过程调用,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议. ...
- Centos7 FRPS
#下载Sever端 wget https://github.com/fatedier/frp/releases/download/v0.16.1/frp_0.16.1_linux_amd64.tar. ...
- python的setup和teardown
关于python unittest ① setup():每个测试函数运行前运行② teardown():每个测试函数运行完后执行③ setUpClass():必须使用@classmethod 装饰器, ...
- Light Up Your Business Promotions With LED Keychain
Imagine you want to insert the car key into the keyhole in the dark. What would you do? You will def ...
- selenium的显示等待、隐式等待
转载:https://www.cnblogs.com/mabingxue/p/10293296.html Selenium显示等待和隐式等待的区别1.selenium的显示等待原理:显示等待,就是明确 ...
- 百炼OJ - 1005 - I Think I Need a Houseboat
题目链接:http://bailian.openjudge.cn/practice/1005/ 思路 一个半圆面积每年增长50,已知一个点坐标,求第几年点在半圆内. #include <stdi ...
- Hadoop3.1.1架构体系——设计原理阐述与Client源码图文详解 : 总览
一.设计原理 1.Hadoop架构: 流水线(PipeLine) 2.Hadoop架构: HDFS中数据块的状态及其切换过程,GS与BGS 3.Hadoop架构: 关于Recovery (Lease ...
- echarts相关问题记录
1.图标距离容器边界 //echats options options : { //... grid : { top : 40, //距离容器上边界40像素 bottom: 30 //距离容器下边界3 ...