数据结构
今天主要学习的内容是
二叉树与图
二叉树里学了个堆,堆里学了个priority_queue
图里主要学的是储存
优先队列(超简单)D题
链接
就是简单用优先队列存个人数,然后按顺序输出即可
#include<bits/stdc++.h>
using namespace std;
struct node
{
int id;
int num;
friend bool operator<(const node &a,const node &b )
{
return a.num> b.num;
}
}a[100005];
priority_queue<node>q;
int main()
{
int n;
scanf("%d",&n);
for(int i=1; i<=n; i++)
{
scanf("%d",&a[i].num);
a[i].id = i;
q.push(a[i]);
}
// cout<<q.top().id<<endl;
for(int i=1;i<=n;i++)
{
printf("%d ",q.top());
q.pop();
}
printf("\n");
return 0;
}
C题(思维题)
【苦涩】
题目很简单,我用优先队列求失活出不来
结果发现是int 没写成long long的问题【苦涩】
priority_queue<long long ,vector<long long>,greater<long long> >q;
这块儿也要用 long long
用优先队列模拟(B题)POJ
用优先队列存个优先循序,用双向队列存个原来的队列,如果优先度不高就退后。
学长的思路:
优先队列和结构体一起用需要加重载
struct node
{
int num;
bool me;
friend bool operator<(const node &a,const node &b )
{
return a.num< b.num;
}
} a[200];
單調棧 E題(poj3250 )
AC碼
#include<stack>
#include<cstdio>
#include<iostream>
using namespace std;
long long a[80010];
stack<long long>s;
int main()
{
long long n;
scanf("%lld",&n);
for(int i =1; i<=n; i++)
{
scanf("%lld",&a[i]);
// s.push(i);
}
while(!s.empty())s.pop();
s.push(n);
long long sum=0;
for(int i=n-1; i>=1; i--)
{
while(!s.empty()&&a[i]>a[s.top()])
{
s.pop();
}
if(s.empty())
{
sum+=(n-i);
// cout<<sum<<endl;
}
else
{
sum+=s.top()-i-1;
// cout<<sum<<endl;
}
s.push(i);
}
printf("%lld\n",sum);
return 0;
}
單調棧:每次加入一个新元素时,把栈中小于等于这个值的元素弹出。
while(!s.empty()&&a[i]>a[s.top()])
{
s.pop();
}
WA原因:
1.long long
2. while(!s.empty())s.pop();///清空棧
s.push(n);