2024大连理工大学新生赛
签到题 2道
DUT校赛
签到
#include<bits/stdc++.h>
using namespace std;
signed main(){
printf("DUT");
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////
美妙的旋律
模拟
复杂度
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define For(i, j, n) for(int i = j ; i <= n ; ++i)
int ans, tot, len;
string s;
signed main(){
cin >> s, len = s.length();
For(i, 0, len - 1){
For(j, 0, 6) if(s[i + j] - '1' == j) ++tot;
if(tot == 7) ++ans;
tot = 0;
}
printf("%lld", ans);
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
简单题 5道
刺客
搜索
用bfs模拟即可。
复杂度
#include<bits/stdc++.h>
using namespace std;
#define N 1005
#define PUSH Push(t.x + xx[i], t.y + yy[i], dis[t.x][t.y])
#define For(i, j, n) for(int i = j ; i <= n ; ++i)
int n, a[N][N], vis[N][N], dis[N][N];
int xx[16] = {0, 0, 1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 0, 0, 2, -2};
int yy[16] = {1, -1, 0, 0, -2, 2, -2, 2, 1, -1, 1, -1, 2, -2, 0, 0};
struct node{ int x, y; }c, t;
queue<node>q;
inline void Push(int x, int y, int k){
c.x = x, c.y = y;
if(!a[x][y] && !vis[x][y] && x >= 1 && y >= 1 && x <= n && y <= n)
q.push(c), vis[x][y] = 1, dis[x][y] = k + 1;
}
inline void bfs(){
c.x = 1, c.y = 1, q.push(c), vis[1][1] = 1;
while(!q.empty()){
t = q.front();
q.pop();
For(i, 0, 3) PUSH;
if(t.x + t.y & 1) For(i, 4, 7) PUSH;
if(t.x == t.y) For(i, 8, 11) PUSH;
if(t.x == n) For(i, 12, 13) PUSH;
if(t.y == n) For(i, 14, 15) PUSH;
}
}
int main(){
scanf("%d", &n);
For(i, 1, n) For(j, 1, n) scanf("%d", &a[i][j]);
bfs();
if(!vis[n][n]) printf("-1");
else printf("%d", dis[n][n]);
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////
方差
数学
不难证明,想要让方差尽量地大,最后答案一定是取前k - t大的数和后t大的数,枚举t,利用
复杂度
#include<bits/stdc++.h>
using namespace std;
#define N 1000005
#define int long long
#define Fol(i, j, n) for(int i = j ; i >= n ; --i)
#define For(i, j, n) for(int i = j ; i <= n ; ++i)
int n, k;
double ans, sum, tot, a[N];
signed main(){
scanf("%lld %lld", &n, &k);
For(i, 1, n) scanf("%lf", &a[i]);
sort(a + 1, a + n + 1);
For(i, 1, k) sum += a[i] * a[i], tot += a[i];
ans = max(ans, sum / k - (tot / k) * (tot / k));
Fol(i, k, 1){
sum += a[n + i - k] * a[n + i - k] - a[i] * a[i];
tot += a[n + i - k] - a[i];
ans = max(ans, sum / k - (tot / k) * (tot / k));
}
printf("%.10lf", ans);
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////
区间数量统计
二分,前缀和
枚举左端点l,相当于询问有多少个右端点r,满足
复杂度复杂度
#include<bits/stdc++.h>
using namespace std;
#define N 1000005
#define int long long
#define inf 2000001028
#define For(i, j, n) for(int i = j ; i <= n ; ++i)
int k, n, p, x, ans, a[N], sum[N];
signed main(){
scanf("%lld %lld", &n, &x);
For(i, 1, n) scanf("%lld", &a[i]), sum[i] = sum[i - 1] + a[i];
For(i, 1, n) p = lower_bound(sum + 1, sum + n + 1, x + sum[i - 1]) - sum, ans += n - p + 1;
printf("%lld\n", ans);
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////
最近公共祖先
树上dfs
用
复杂度
#include<bits/stdc++.h>
using namespace std;
#define N 200005
#define int long long
#define For(i, j, n) for(int i = j ; i <= n ; ++i)
int n, k, t, u, v, ans, col[N], siz[N], mson[N];
vector<int>E[N];
inline void dfs(int x, int fa){
siz[x] = col[x];
for(auto i : E[x]){
if(i == fa) continue;
dfs(i, x), siz[x] += siz[i];
if(siz[mson[x]] < siz[i]) mson[x] = i;
}
ans += (siz[x] != siz[mson[x]]) && (siz[x] > 1);
}
signed main(){
scanf("%lld %lld", &n, &k);
For(i, 1, k) scanf("%lld", &t), col[t] = 1;
For(i, 2, n) scanf("%lld %lld", &u, &v), E[u].push_back(v), E[v].push_back(u);
dfs(1, 1);
printf("%lld", ans);
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////
点的删除
堆
用堆来维护,把点的度数当作元素存入堆内,第一种操作直接查询堆顶元素,第二种操作相当于删除原有的两个度数元素,增加两个新的度数元素,第三种操作相当于删除这个点相连的所有点原有对应的度数元素,增加新的对应的度数元素。
复杂度
#include<bits/stdc++.h>
using namespace std;
#define N 1000005
#define int long long
#define For(i, j, n) for(int i = j ; i <= n ; ++i)
int n, m, u, v, x, y, Q, op, d[N], del[N], vis[N];
vector<int>E[N];
signed main(){
scanf("%lld %lld %lld", &n, &m, &Q);
priority_queue<int>q;
For(i, 1, m){
scanf("%lld %lld", &u, &v);
E[u].push_back(v), E[v].push_back(u), d[u]++, d[v]++;
}
For(i, 1, n) vis[d[i]]++, q.push(d[i]);
while(Q--){
scanf("%lld", &op);
if(op == 1){
while(!vis[q.top()]) q.pop();
printf("%lld\n", q.top());
}
if(op == 2){
scanf("%lld %lld", &x, &y);
if(del[x] || del[y]) continue;
vis[d[x]]--, vis[d[y]]--;
E[x].push_back(y), E[y].push_back(x);
d[x]++, d[y]++, q.push(d[x]), q.push(d[y]);
vis[d[x]]++, vis[d[y]]++;
}
if(op == 3){
scanf("%lld", &x);
if(del[x]) continue;
for(auto i : E[x]) if(!del[i]) vis[d[i]]--, d[i]--, q.push(d[i]), vis[d[i]]++;
vis[d[x]]--, del[x] = 1;
}
}
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
中等题 4道
变向
搜索
分层图,把一个点拆成4个点,分别表示到这个点是走了上下左右哪个方向。
复杂度
#include<bits/stdc++.h>
using namespace std;
#define N 1005
#define For(i, j, n) for(int i = j ; i <= n ; ++i)
int n, m, a[N][N], vis[N][N][5], dis[N][N][5];
int xx[4] = {0, 0, 1, -1}, yy[4] = {1, -1, 0, 0};
struct node{ int x, y, d; }p, t;
inline int bfs(){
For(i, 0, 3) vis[1][1][i] = 1;
queue<node>q;
t.x = t.y = 1, t.d = 4;
q.push(t);
while(!q.empty()){
p = q.front();
q.pop();
For(i, 0, 3){
if(i == p.d) continue;
t.x = p.x + xx[i], t.y = p.y + yy[i], t.d = i;
if(!t.x || !t.y || t.x > n || t.y > m) continue;
if(t.x == n && t.y == m) return dis[p.x][p.y][p.d] + 1;
if(!vis[t.x][t.y][t.d] && !a[t.x][t.y])
vis[t.x][t.y][t.d] = 1, dis[t.x][t.y][t.d] = dis[p.x][p.y][p.d] + 1, q.push(t);
}
}
return -1;
}
signed main(){
scanf("%d %d", &n, &m);
For(i, 1, n) For(j, 1, m) scanf("%d", &a[i][j]);
printf("%d", bfs());
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////
公约数问题
动态规划