今天的题目都看着比较容易但实操时犯了很多错,写了P3613,P1446,P1996,P1160就讲一下P3613和P1160吧。
P3613的话就时用一个map映射一下就行了。我一开始想的时pair,但是有个大佬就直接用i*100000+j唯一表示了两个数这个我感觉细节的划重点。上代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <map>
using namespace std;
typedef long long ll;
int n, q;
map <ll, int> m;
int main()
{
cin >> n >> q;
for (int i = 0; i < q; i++)
{
int tmp;
cin >> tmp;
if (tmp == 1)
{
int i, j, k;
cin >> i >> j >> k;
m[i * 100000 + j] = k;
}
else {
int i, j;
cin >> i >> j;
cout << m[i * 100000 + j] << endl;
}
}
return 0;
}
然后就是P1160,写这个题我一开始是直接手敲双向链表,但是超时了3个测试点,然后看看了发现有STL的模板。就学了一下。下面的是链接:
#include <cstdio>
#include <list>
using namespace std;
using Iter = list<int>::iterator;
const int maxN = 1e5 + 10;
Iter pos[maxN];
list<int> queList;
bool erased[maxN];
int N;
void buildQueue()
{
queList.push_front(1);
pos[1] = queList.begin();
for (int i = 2; i <= N; i++)
{
int k, p;
scanf("%d%d", &k, &p);
if (p == 0)
{
pos[i] = queList.insert(pos[k], i); //left
}
else
{
auto nextIter = next(pos[k]);
pos[i] = queList.insert(nextIter, i); //right
}
}
int M;
scanf("%d", &M);
for (int x, i = 1; i <= M; i++)
{
scanf("%d", &x);
if (!erased[x])
{
queList.erase(pos[x]);
}
erased[x] = true;
}
}
int main()
{
scanf("%d", &N);
buildQueue();
bool first = true;
for (int x: queList)
{
if (!first)
putchar(' ');
first = false;
printf("%d", x);
}
putchar('\n');
return 0;
}