#include<bits/stdc++.h>
using namespace std;
map<int,int> mp;
set<int> st;
int main() {
mp.insert({1,2});
mp.insert({2,4});
auto [a,b] = *mp.begin();
auto [c,d] = *mp.rbegin();
cout<<a<<" "<<b<<endl;
cout<<c<<" "<<d<<endl;
st.insert(1);
st.insert(2);
cout<<*st.begin()<<endl;
cout<<*st.rbegin()<<endl;
return 0;
}
multiset<int> st;
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
//set<int> st;
multiset<int> st;
int main(){
st.insert(0);
st.insert(2);
st.insert(4);
st.insert(10);
st.insert(2);
for(auto v : st) cout<<v<<" ";
cout<<"\n";
int x;
cin >> x;
cout<<*st.lower_bound(x)<<endl;
if(st.lower_bound(x) == st.end()) cout<<"NO\n";
else cout<<"YES\n";
return 0;
}
若set不存在key则返回end迭代器: st.lower_bound(k) == end 判断条件!
map嵌套: map<int,map<int,int>> mp; mp[1][2] = 3;
set.lower_bound({pair}),set<tuple<int,int,int,int>> s;
map.lower_bound({pair});
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
const int N = 1e6+9;
typedef long long ll;
typedef pair<int,int> PII;
map<int,int> mp;
unordered_set<int> st;
int a[N];
int main() {
int n,q;
cin >> n >> q;
for(int i=1; i<=n; i++) {
int x;
scanf("%d",&x);
if(!st.count(x)) {
st.insert(x);
mp[x] = i;
}
}
while(q--) {
int x;
scanf("%d",&x);
auto [a,b] = *mp.lower_bound(x);
if(a == x) cout<<b<<" ";
else cout<<"-1 ";
}
return 0;
}
https://www.luogu.com.cn/problem/P2249
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
const int N = 1e6+9;
typedef long long ll;
typedef pair<int,int> PII;
unordered_map<int,int> mp;
set<PII> st;
set<tuple<int,int,int,int>> s;
int a[N];
int main() {
int n,q;
cin >> n >> q;
for(int i=1; i<=n; i++) {
int x;
scanf("%d",&x);
if(!mp[x]) {
mp[x] = 1;
st.insert({x,i});
}
}
while(q--) {
int x;
scanf("%d",&x);
auto [a,b] = *st.lower_bound({x,0});
if(a == x) cout<<b<<" ";
else cout<<"-1 ";
}
return 0;
}
刚刚看了好像deque也是O(1)插入和删除操作并且迭代器可以进行运算! deque迭代器好像动态的,没弄明白不太好用!
List
list的erase(iterato'pos) 会返回下一个元素迭代器的位置,若在end-1(end 无数字)的位置会返回end迭代器即L.size() 其中只需要保证it的正确性即可!
STL 中有用于操作迭代器的三个函数模板,它们是: advance(p, n):使迭代器 p 向前或向后移动 n 个元素。 distance(p, q):计算两个迭代器之间的距离,即迭代器 p 经过多少次 + + 操作后和迭代器 q 相等。如果调用时 p 已经指向 q 的后面,则这个函数会陷入死循环。 iter_swap(p, q):用于交换两个迭代器 p、q 指向的值。
//最终于版本!
#pragma comment(linker, "/STACK:102400000,102400000") 手动扩栈空间!
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 9;
int a[N];
list<int> L;
int main()
{
int n, k;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]), L.emplace_back(a[i]);
int ans = 0;
while (1)
{
int f = 0;
for(auto it = L.begin(); it != L.end();)
{
if(L.size() <= 1) break;
auto pre = it,ne = it;
advance(pre,-1);
advance(ne,1);
if(it == L.begin()) pre = L.end(),pre--;
auto x = L.end();
if(it == --x) ne = L.begin();
if(*it == *ne || *it + *ne == k)
{
f = 1;
ans++;
L.erase(it);
it = L.erase(ne);
}
else if(*it == *pre || *it + *pre == k)
{
f = 1;
ans++;
L.erase(pre);
it = L.erase(it);
}
else it++;
}
if(f == 0) break;
}
cout<<ans<<"\n";
return 0;
}
https://ac.nowcoder.com/acm/contest/33192/F
因为没有特判1,导致RE了一个小时,呜呜~
#pragma comment(linker, "/STACK:102400000,102400000")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5+9;
int a[N];
list<int> L;
int main()
{
int n,k;
scanf("%d%d",&n,&k);
for(int i=1; i<=n; i++) scanf("%d",&a[i]),L.emplace_back(a[i]);
int ans = 0;
int cnt = 0;
while(1)
{
int f = 0;
for(auto it = L.begin(); it != L.end();)
{
if(L.size() == 1) break;
++cnt;
auto ne = it,pre = it;
ne++,pre--;
if(it == L.begin()) pre = L.end(),pre--;
auto x = L.end();
x--;
if(it == x) ne = L.begin();
if(*it == *ne || *it + *ne == k)
{
f = 1;
if(ne == L.begin())
{
L.erase(it);
it = L.end();
it--;
ne = L.erase(ne);
}
else
{
it = L.erase(it);
ne = L.erase(ne);
it = ne;
}
ans++;
}
else if(*it == *pre || *it + *pre == k)
{
if(it == L.begin())
{
it = L.erase(it);
pre = L.erase(pre);
pre = L.end();
pre--;
}
else
{
pre = L.erase(pre);
it = L.erase(pre);
pre = it;
}
f = 1;
ans++;
}
else it++;
}
if(f == 0) break;
}
printf("%d\n",ans);
return 0;
}
/************
10 5
1 1 4 5 1 4 4 2 3 6
************/
#include<bits/stdc++.h>
using namespace std;
int main()
{
list<int> L;
int n;
cin >> n;
for(int i=1; i<=n; i++) L.push_back(i);
L.push_front(0);
auto del = L.begin();
for(int i=1; i<=2; i++) del++;
auto end = L.end();
end--;
for(auto it = del; it != end; it++)
{
auto x = it;
x++;
cout<<*it<<" "<<*x<<endl;
}
return 0;
}