当前位置:网站首页>暑假第四周
暑假第四周
2022-07-26 09:20:00 【Alex Su (*^▽^*)】
写日报真的是很好的督促学习的方式
暑假以来比较浪,从今天开始改变自己
周一
Concatenation(字符串比较)
暴力排序竟然可以过……
显然难点在于前缀相同的情况,这个时候比较ab和ba即可
#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(前缀后缀+代码技巧)
比赛时分类讨论去了,其实是弄复杂了
一个区间去掉一个点,就剩下前缀和后缀了,于是我们可以预处理出前缀lca和后缀lca
因为同样的代码要写两份,所以写个结构体可以大大简化代码
#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)
边看作点,路口看作边建图,边的权值只有0和1,这时用0/1bfs即可,和bfs复杂度一样
注意一些细节,建图配id的时候,注意用unordered_map
0/1bfs和bfs的区别在于用双端队列,边权为0的点放前面,否则放后面
用松弛操作来更新,这样一个点最多入队两次。然后不需要vis数组
此外,第一次找到的点不一定是最优的,但第一次出队的点一定是最优的
其实0/1bfs就是一个特殊的dijsktra,只是说优先队列变成了双端队列,通过入队方式来保证单调性。
#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用双端队列
q.push_back(st);
while(!q.empty())
{
int u = q.front(); q.pop_front();
if(u == dest) return d[u]; //0/1bfs中,出队的时候是最小值,第一次遇到不一定是最小值
for(auto x: g[u])
{
int v = x.first, w = x.second;
if(d[v] > d[u] + w) //松弛操作
{
d[v] = d[u] + w;
if(!w) q.push_front(v); //0放队首 1放队尾
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;
}
这题直接dijsktra也可以过
当然要注意,第一次入队不一定是最优的,第一次出队才是最优的。或者干脆算法跑完后再输出。
#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;
}
边栏推荐
- Simple message mechanism of unity
- Sliding window, double pointer, monotone queue, monotone stack
- "No input file specified" problem handling
- Ext4 file system opens dir_ After nlink feature, link_ Use link after count exceeds 65000_ Count=1 means the quantity is unknown
- Grain College of all learning source code
- Clean the label folder
- 2B和2C
- jvm命令归纳
- MySQL strengthen knowledge points
- 209. Subarray with the smallest length
猜你喜欢
Polynomial open root
Introduction to excellent verilog/fpga open source project (30) - brute force MD5
NTT (fast number theory transformation) polynomial inverse 1500 word analysis
Babbitt | metauniverse daily must read: does the future of metauniverse belong to large technology companies or to the decentralized Web3 world
李沐d2l(四)---Softmax回归
Paper notes: knowledge map kgat (unfinished temporary storage)
Nuxt - Project packaging deployment and online to server process (SSR server rendering)
【Flutter -- 布局】Align、Center、Padding 使用详解
2022化工自动化控制仪表操作证考试题模拟考试平台操作
209. Subarray with the smallest length
随机推荐
[arkit, realitykit] turn pictures into 3D models
解决“NOTE: One or more layouts are missing the layout_width or layout_height attributes.”
Pat grade a A1034 head of a gang
Processing of inconsistent week values obtained by PHP and MySQL
力扣刷题,三数之和
“No input file specified “问题的处理
Innovus卡住,提示X Error:
[online problem] timeout waiting for connection from pool problem troubleshooting
Object type collections are de duplicated according to the value of an attribute
Bloom filter
Two tips for pycharm to open multiple projects
Under a directory of ext3 file system, subfolders cannot be created, but files can be created
JS - DataTables 关于每页显示数的控制
HBuilderX 运行微信开发者工具 “Fail to open IDE“报错解决
李沐d2l(四)---Softmax回归
2022流动式起重机司机考试题模拟考试题库模拟考试平台操作
The Child and Binary Tree-多项式开根求逆
Qt | 关于如何使用事件过滤器 eventFilter
Datax的学习笔记
Pat grade a a1076 forwards on Weibo