当前位置:网站首页>The fourth week of summer vacation
The fourth week of summer vacation
2022-07-26 09:54:00 【Alex Su (*^▽^*)】
Writing a daily newspaper is really a good way to urge learning
It's been rough since summer vacation , Change yourself from today
Monday
Concatenation( String comparison )
The order of violence can pass ……
Obviously, the difficulty lies in the case of the same prefix , At this time ab and ba that will do
#include<bits/stdc++.h>
#define _for(i, a, b) for(int i = a; i <= b; i++)
#define rep(i, a, b) for(int i = a; i < b; i++)
using namespace std;
const int N = 2e6 + 10;
string a[N];
bool cmp(string x, string y)
{
int len = min(x.size(), y.size());
rep(i, 0, len)
if(x[i] != y[i])
return x[i] < y[i];
return x + y < y + x;
}
int main()
{
int n; scanf("%d", &n);
_for(i, 1, n) cin >> a[i];
sort(a + 1, a + n + 1, cmp);
_for(i, 1, n) cout << a[i];
puts("");
return 0;
}Ancestor( Prefix suffix + Code tricks )
During the competition, we went to the classified discussion , It's actually complicated
Remove a point from an interval , There are only prefixes and suffixes left , So we can preprocess the prefix lca And suffixes lca
Because you need to write two copies of the same code , So writing a structure can greatly simplify the code
#include<bits/stdc++.h>
#define _for(i, a, b) for(int i = a; i <= b; i++)
#define rep(i, a, b) for(int i = a; i < b; i++)
using namespace std;
const int N = 1e5 + 10;
int k[N], n, m;
struct Tree
{
int d[N], up[N][20], q[N], h[N], val[N];
vector<int> g[N];
void read()
{
_for(i, 1, n) scanf("%d", &val[i]);
_for(i, 2, n)
{
int x; scanf("%d", &x);
g[x].push_back(i);
}
}
void dfs(int u, int fa)
{
d[u] = d[fa] + 1;
up[u][0] = fa;
_for(j, 1, 19) up[u][j] = up[up[u][j - 1]][j - 1];
for(int v: g[u])
{
if(v == fa) continue;
dfs(v, u);
}
}
int lca(int u, int v)
{
if(!u || !v) return u + v;
if(d[u] < d[v]) swap(u, v);
for(int j = 19; j >= 0; j--)
if(d[up[u][j]] >= d[v])
u = up[u][j];
if(u == v) return u;
for(int j = 19; j >= 0; j--)
if(up[u][j] != up[v][j])
u = up[u][j], v = up[v][j];
return up[u][0];
}
void init()
{
dfs(1, 0);
q[1] = k[1];
_for(i, 2, m) q[i] = lca(q[i - 1], k[i]);
h[m] = k[m];
for(int i = m - 1; i >= 1; i--) h[i] = lca(h[i + 1], k[i]);
}
int cal(int x)
{
return val[lca(q[x - 1], h[x + 1])];
}
}A, B;
int main()
{
scanf("%d%d", &n, &m);
_for(i, 1, m) scanf("%d", &k[i]);
A.read(); A.init();
B.read(); B.init();
int ans = 0;
_for(i, 1, m)
if(A.cal(i) > B.cal(i))
ans++;
printf("%d\n", ans);
return 0;
}Journey(0/1bfs)
Look at the edge as a point , The intersection is regarded as a side map , The weight of the edge is only 0 and 1, Use 0/1bfs that will do , and bfs The complexity is the same
Pay attention to some details , Build drawings id When , Use it carefully unordered_map
0/1bfs and bfs The difference is to use double ended queues , The boundary right is 0 Put your point in front , Otherwise, put it in the back
Update with relax operation , Such a point can join the team twice at most . Then you don't need to vis Array
Besides , The point found for the first time is not necessarily optimal , But the first point of leaving the team must be the best
Actually 0/1bfs It's a special dijsktra, It's just that the priority queue has become a double ended queue , Ensure monotony by joining the team .
#include<bits/stdc++.h>
#define _for(i, a, b) for(int i = a; i <= b; i++)
#define rep(i, a, b) for(int i = a; i < b; i++)
using namespace std;
typedef long long ll;
const int N = 5e5 * 8 + 10;
unordered_map<ll, int> mp;
vector<pair<int, int>> g[N];
int d[N], vis[N], n, cnt;
int id(int x, int y)
{
ll t = 1LL * x * 1e6 + y;
if(!mp[t]) mp[t] = ++cnt;
return mp[t];
}
int solve(int st, int dest)
{
_for(i, 1, cnt) d[i] = 1e9;
d[st] = 0;
deque<int> q; //0/1bfs Double ended queue
q.push_back(st);
while(!q.empty())
{
int u = q.front(); q.pop_front();
if(u == dest) return d[u]; //0/1bfs in , It is the minimum value when leaving the team , The first encounter is not necessarily the minimum
for(auto x: g[u])
{
int v = x.first, w = x.second;
if(d[v] > d[u] + w) // Slack operation
{
d[v] = d[u] + w;
if(!w) q.push_front(v); //0 Lead the team 1 Put the tail of the team
else q.push_back(v);
}
}
}
return -1;
}
int main()
{
scanf("%d", &n);
_for(i, 1, n)
{
int c[5];
_for(j, 1, 4) scanf("%d", &c[j]);
_for(j, 1, 4)
{
if(!c[j]) continue;
g[id(c[j], i)].push_back({id(i, c[j]), 1});
int ii = j % 4 + 1;
if(c[ii]) g[id(c[j], i)].push_back({id(i, c[ii]), 0});
ii = (j + 1) % 4 + 1;
if(c[ii]) g[id(c[j], i)].push_back({id(i, c[ii]), 1});
ii = (j + 2) % 4 + 1;
if(c[ii]) g[id(c[j], i)].push_back({id(i, c[ii]), 1});
}
}
int s1, s2, t1, t2;
scanf("%d%d%d%d", &s1, &s2, &t1, &t2);
int st = id(s1, s2), dest = id(t1, t2);
printf("%d\n", solve(st, dest));
return 0;
}This is a direct question dijsktra You can go through
Of course, pay attention to , Joining the team for the first time is not necessarily the best , The first team is the best . Or simply output after the algorithm runs .
#include<bits/stdc++.h>
#define _for(i, a, b) for(int i = a; i <= b; i++)
#define rep(i, a, b) for(int i = a; i < b; i++)
using namespace std;
typedef long long ll;
const int N = 5e5 * 8 + 10;
unordered_map<ll, int> mp;
int d[N], vis[N], n, cnt;
struct node
{
int v, w;
bool operator < (const node& rhs) const
{
return w > rhs.w;
}
};
vector<node> g[N];
int id(int x, int y)
{
ll t = 1LL * x * 1e6 + y;
if(!mp[t]) mp[t] = ++cnt;
return mp[t];
}
int solve(int st, int dest)
{
_for(i, 1, cnt) d[i] = 1e9;
d[st] = 0;
priority_queue<node> q;
q.push({st, d[st]});
while(!q.empty())
{
node x = q.top(); q.pop();
int u = x.v;
if(u == dest) return d[u];
if(d[u] != x.w) continue;
for(auto x: g[u])
{
int v = x.v, w = x.w;
if(d[v] > d[u] + w)
{
d[v] = d[u] + w;
q.push({v, d[v]});
}
}
}
return -1;
}
int main()
{
scanf("%d", &n);
_for(i, 1, n)
{
int c[5];
_for(j, 1, 4) scanf("%d", &c[j]);
_for(j, 1, 4)
{
if(!c[j]) continue;
g[id(c[j], i)].push_back({id(i, c[j]), 1});
int ii = j % 4 + 1;
if(c[ii]) g[id(c[j], i)].push_back({id(i, c[ii]), 0});
ii = (j + 1) % 4 + 1;
if(c[ii]) g[id(c[j], i)].push_back({id(i, c[ii]), 1});
ii = (j + 2) % 4 + 1;
if(c[ii]) g[id(c[j], i)].push_back({id(i, c[ii]), 1});
}
}
int s1, s2, t1, t2;
scanf("%d%d%d%d", &s1, &s2, &t1, &t2);
int st = id(s1, s2), dest = id(t1, t2);
printf("%d\n", solve(st, dest));
return 0;
}
边栏推荐
- 2022年中科磐云——服务器内部信息获取 解析flag
- Study notes at the end of summer vacation
- Usage of the formatter attribute of El table
- I finished watching this video on my knees at station B
- Logical architecture of MySQL
- 解决npm -v突然失效 无反应
- The problem of accessing certsrv after configuring ADCs
- JS 连等赋值操作
- 2019 ICPC Asia Yinchuan regional (water problem solution)
- 苹果独占鳌头,三星大举复兴,国产手机在高端市场颗粒无收
猜你喜欢

protobuf的基本用法

Alibaba cloud technology expert haochendong: cloud observability - problem discovery and positioning practice

挡不住了,纯国产PC已就位,美国的软硬件体系垄断正式被破

Principle analysis and source code interpretation of service discovery
![[MySQL database] a collection of basic MySQL operations - the basis of seeing (adding, deleting, modifying, and querying)](/img/a7/b3bb6f584dff0eb9b49e81e5b9dade.png)
[MySQL database] a collection of basic MySQL operations - the basis of seeing (adding, deleting, modifying, and querying)

Solve proxyerror: CONDA cannot proceed due to an error in your proxy configuration

Registration module use case writing

Apple dominates, Samsung revives, and domestic mobile phones fail in the high-end market

小白搞一波深拷贝 浅拷贝

服务发现原理分析与源码解读
随机推荐
The use of MySQL in nodejs
asp. Net using redis cache (2)
Mqtt x cli officially released: powerful and easy-to-use mqtt 5.0 command line tool
Use of tabbarcontroller
Matlab Simulink realizes fuzzy PID control of time-delay temperature control system of central air conditioning
万字详解“用知识图谱驱动企业业绩增长”
Mo team learning summary (II)
Mysql5.7.25 master-slave replication (one-way)
Logical architecture of MySQL
POJ 1012 Joseph
Applet record
网络流学习笔记
protobuf的基本用法
SSG framework Gatsby accesses the database and displays it on the page
Solve the problem of storing cookies in IE7 & IE8
Fiddler download and installation
Docker configuring MySQL Cluster
Nodejs service background execution (forever)
The diagram of user login verification process is well written!
RMQ学习笔记