当前位置:网站首页>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;
}
边栏推荐
- protobuf的基本用法
- Write a script that can run in Bash / shell and PowerShell
- PMM (percona monitoring and management) installation record
- Show default image when wechat applet image cannot be displayed
- Customize permission validation in blazor
- spolicy请求案例
- Solve NPM -v sudden failure and no response
- [information system project manager] summary of essence of high-level series for the first time
- 在.NET 6.0中配置WebHostBuilder
- Network flow learning notes
猜你喜欢
Fiddler download and installation
Interview shock 68: why does TCP need three handshakes?
解决npm -v突然失效 无反应
asp. Net using redis cache
Apple dominates, Samsung revives, and domestic mobile phones fail in the high-end market
QT handy notes (III) use qtcharts to draw a line chart in VS
protobuf的基本用法
Principle analysis and source code interpretation of service discovery
Customize permission validation in blazor
Fuzzy PID control of motor speed
随机推荐
图解用户登录验证流程,写得太好了!
苹果独占鳌头,三星大举复兴,国产手机在高端市场颗粒无收
AR model in MATLAB for short-term traffic flow prediction
R语言ggpubr包ggsummarystats函数可视化分组箱图(自定义分组颜色)并在X轴标签下方添加分组对应的统计值(样本数N、中位数median、四分位数的间距iqr、统计值的色彩和分组图色匹配
The combination of officially issued SSL certificate and self signed certificate realizes website two-way authentication
[datawhale] [machine learning] Diabetes genetic risk detection challenge
Draw arrows with openlayer
Sqoop [environment setup 01] CentOS Linux release 7.5 installation configuration sqoop-1.4.7 resolve warnings and verify (attach sqoop 1 + sqoop 2 Latest installation package +mysql driver package res
Fiddler packet capturing tool for mobile packet capturing
MySQL 5.7.25 source code installation record
Mysql5.7.25 master-slave replication (one-way)
Show default image when wechat applet image cannot be displayed
Principle analysis and source code interpretation of service discovery
PMM (percona monitoring and management) installation record
R语言ggplot2可视化: 将图例标题(legend title)对齐到ggplot2中图例框的中间(默认左对齐、align legend title to middle of legend)
spolicy请求案例
在同一conda环境下先装Pytroch后装TensorFlow
Azkaban【基础知识 01】核心概念+特点+Web界面+架构+Job类型(一篇即可入门Azkaban工作流调度系统)
Node 内存溢出及V8垃圾回收机制
莫队学习总结(二)