个人练习
一、补题
1.Link with Bracket Sequence II
解题思路是区间DP,代码如下
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 510,mod = 1e9+7;
vector<int>v;
map<int,int>mp;
int n, m;
int a[510],f[N][N][2];
bool st[N][N];
bool check(int l,int r){
if(a[l] > 0 && a[r] < 0 && a[r] == a[l] * -1) return true;
return false;
}
void dfs(int l,int r){
if(((r-l+1) % 2 == 1) || st[l][r]) return ;
st[l][r] = 1;
if(r == l + 1){
if(check(l,r)) f[l][r][1] = 1;
else if(a[l] == 0 && a[r] == 0) f[l][r][1] = m;
else if((a[l] == 0 && a[r] < 0) || (a[r] == 0 && a[l] > 0)) f[l][r][1] = 1;
return ;
}
if(check(l,r)){
dfs(l+1,r-1);
f[l][r][1] = 1ll * (f[l+1][r-1][0] + f[l+1][r-1][1]) % mod;
}else if(a[l] == 0 && a[r] == 0){
dfs(l+1,r-1);
f[l][r][1] = 1ll * (f[l+1][r-1][0] + f[l+1][r-1][1]) % mod * m % mod;
}else if((a[l] == 0 && a[r] < 0) || (a[r] == 0 && a[l] > 0)){
dfs(l+1,r-1);
f[l][r][1] = 1ll * (f[l+1][r-1][0] + f[l+1][r-1][1]) % mod;
}
ll sum = 0;
for(int i = l; i < r; i ++){
dfs(l,i);
dfs(i+1,r);
sum = (1ll * sum + 1ll * (f[l][i][0] + f[l][i][1]) % mod * (f[i+1][r][1]) % mod) % mod;
}
f[l][r][0] = sum;
}
void slove(){
scanf("%d %d",&n,&m);
for(int i = 1; i <= n; i ++) scanf("%d",&a[i]);
for(int i = 1; i <= n; i ++)
for(int j = i; j <= n; j ++){
f[i][j][0] = f[i][j][1] = 0;
st[i][j] = 0;
}
dfs(1,n);
ll ans = 1ll * (f[1][n][0] + f[1][n][1]) % mod;
printf("%lld\n",ans);
}
int main(){
//ios::sync_with_stdio(false);
int tt;
scanf("%d",&tt);
while(tt--){
slove();
}
return 0;
}
2.Climb Stairs
后缀和+二分
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 +10;
int a[N];
ll sum[N];
int now;
bool check(int mid, int pos) {
if (sum[pos + 1] - sum[mid + 1] + now >= a[pos])
return 0;
else
return 1;
}
void solve() {
int n, k;
cin >> n >> now >> k;
for (int i = 1; i <= n + 10; i++)
sum[i] = 0;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = n; i >= 1; i--) //后缀
sum[i] = sum[i + 1] + a[i];
int maxx = 0, pre = 1;
for (int i = 1; i <= n; i++) {
int l = i + 1, r = n + 1;
while (l < r) {
int mid = l + r >> 1;
if (check(mid, i))
l = mid + 1;
else
r = mid;
}
// cout << l << endl;
if (now >= a[i])
l = i;
if (l >= min(pre + k, n + 1)) { //大于k
cout << "NO" << endl;
return;
}
maxx = max(maxx, l);
if (l >= maxx && l == i) {
now += sum[pre] - sum[maxx + 1];
pre = i + 1;
maxx = 0;
}
}
cout << "YES" << endl;
}
int main(){
//ios::sync_with_stdio(false);
int tt;
cin >> tt;
while(tt--){
solve();
}
return 0;
}
二、算法学习
1.最小生成树
1.克鲁斯卡尔算法
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 +10;
int n, m;
int p[N];
struct Edge
{
int a,b,w;
bool operator< (const Edge &W)const{//重载运算符,以边长排序
return w < W.w;
}
}edgs[N];
int find(int x){//并查集查找
if(p[x] != x) p[x] = find(p[x]);
return p[x];
}
void keruskal(){
sort(edgs,edgs+m);
for(int i = 1; i <= n; i ++) p[i] = i;
int res = 0, cnt = 0;
for(int i = 0; i < m; i ++){
int a = edgs[i].a, b = edgs[i].b, w = edgs[i].w;
int x = find(a), y = find(b);
if(x != y){
cnt++;
res += w;
p[x] = y;
}
if(cnt == n-1)break;
}
if(cnt < n - 1)puts("impossible");
else printf("%d",res);
}
int main(){
scanf("%d%d",&n,&m);
for(int i = 0; i < m; i ++){
int a,b,w;
scanf("%d%d%d",&a,&b,&w);
edgs[i] = {a,b,w};
}
keruskal();
return 0;
}
2.普利姆算法
#include<bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
int n, m;
int g[510][510];
int dist[510];
bool st[510];
void prime(){
memset(dist,0x3f,sizeof dist);
int res = 0;
for(int i = 0; i < n; i++){
int t = -1;
for(int j = 1; j<= n; j ++)
if(!st[j] && (t == -1 || dist[t] > dist[j]))
t = j;
if(i && dist[t] == INF) {//目前找到的点距离是最大,说明没有边连接到已确定集合上,说明这个点独立,不可能有最小生成树
puts("impossible");
return ;
}
if(i) res += dist[t];//当不是起点的时候,把边加入
for(int j = 1; j <= n; j ++) dist[j] = min(dist[j],g[t][j]);
st[t] = true;
}
printf("%d",res);
return ;
}
int main(){
scanf("%d%d",&n,&m);
memset(g,0x3f,sizeof g);
while(m--){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
g[u][v] = g[v][u] = min(g[u][v],w);
}
prime();
return 0;
}
2.二分图
1.染色法判断二分图
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 +10;
int n, m;
int h[N],ne[N*2],e[N*2],idx;
int color[N];
void add(int u,int v){
e[++idx] = v;
ne[idx] = h[u];
h[u] = idx;
}
bool dfs(int x,int y){
color[x] = y;
for(int i = h[x]; i; i = ne[i]){
int j = e[i];
if(!color[j]){
if(!dfs(j,y%2+1)) return false;
}else if(color[j] == y) return false;
}
return true;
}
int main(){
scanf("%d%d",&n,&m);
for(int i = 1; i<= m; i ++){
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
bool st = true;
for(int i = 1; i <= n; i ++){
if(!color[i]){
if(!dfs(i,1)){
st = false;
break;
}
}
}
if(st) puts("Yes");
else puts("No");
return 0;
}
2.二分图的最大匹配(匈牙利算法)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 +10;
int n1, n2, m;
int h[510],ne[N],e[N],idx;
int match[510];
bool st[510];
void add(int u,int v){
e[++idx] = v;
ne[idx] = h[u];
h[u] = idx;
}
bool find(int x){
for(int i = h[x]; i; i = ne[i]){
int j = e[i];
if(!st[j]){
st[j] = true;
if(match[j] == 0 || find(match[j])){
match[j] = x;
return true;
}
}
}
return false;
}
int main(){
scanf("%d%d%d",&n1,&n2,&m);
for(int i = 1; i<= m; i ++){
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
}
int res = 0;
for(int i = 1; i <= n1; i ++){
memset(st,0,sizeof st);
if(find(i))res ++;
}
printf("%d",res);
return 0;
}
三、洛谷最短路练习(续)
1.P1462 通往奥格瑞玛的道路
二分找答案,利用spfa去验证
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 100010;
#define INF 1000000001
vector<int>v;
map<int,int>mp;
int n, m, b;
int h[N],e[N],ne[N],w[N],idx;
int f[10010];
void add(int x,int y,int z){
e[++idx] = y;
w[idx] = z;
ne[idx] = h[x];
h[x] = idx;
}
ll dist[10010];
bool st[10010];
bool spfa(int x){
if(f[1] > x) return false;
for(int i = 0; i <= n; i ++) dist[i] = INF;
dist[1] = 0;
queue<int>q;
q.push(1);
st[1] = true;
while(q.size()){
int t = q.front();
q.pop();
st[t] = false;
for(int i = h[t]; i; i = ne[i]){
int j = e[i];
if(dist[j] > dist[t] + w[i]){
dist[j] = dist[t] + w[i];
if(!st[j] && f[j] <= x){
q.push(j);
st[j] = true;
}
}
}
}
if(dist[n] <= b) return true;
return false;
}
int main(){
scanf("%d%d%d",&n,&m,&b);
int l = 0, r = 0;
for(int i = 1; i <= n; i ++) scanf("%d",&f[i]),r = max(r,f[i]);
l = max(f[1],f[n]);
for(int i = 1; i <= m; i ++){
int sa,sb,sc;
scanf("%d%d%d",&sa,&sb,&sc);
add(sa,sb,sc);
add(sb,sa,sc);
}
if(!spfa(INF)) {
puts("AFK");
return 0;
}
while(l <= r){
int mid = (l + r) / 2;
if(spfa(mid)) r = mid - 1;
else l = mid + 1;
}
printf("%d",l);
return 0;
}
2.P1522 [USACO2.4] 牛的旅行 Cow Tours
首先用dfs进行染色去区分牧场,然后用floyd去找最短距离,再计算出每个牧区所有点到其他点的最大距离,用作牧区半径,最后找答案
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 310;
#define INF 1000000001
int n;
struct Point{
int x,y;
double distince(const Point &b){
return sqrt((x - b.x) * (x - b.x) + (y - b.y) * (y - b.y));
}
};
double dist[151][151],d[159];
int field[151];
void floyd(){
for(int k = 1; k <= n; k ++)
for(int i = 1; i <= n; i++)
for(int j = 1;j <= n; j++)
dist[i][j] = min(dist[i][j],dist[i][k]+dist[k][j]);
}
void dfs(int i,int id){
field[i] = id;
for(int j = 1; j <= n; j ++)
if(!field[j] && dist[i][j] < INF) dfs(j,id);
}
int main(){
Point a[151];
scanf("%d",&n);
for(int i = 1; i <= n; i ++) scanf("%d %d",&a[i].x,&a[i].y);
char s[n+1];
for(int i = 1; i <= n; i ++){
scanf("%s",s+1);
for(int j = 1; j <= n; j ++){
if(s[j] == '1' || i == j){
dist[i][j] = a[i].distince(a[j]);
}else dist[i][j] = INF;
}
}
int id = 0;
for(int i = 1; i <= n; i ++) if(!field[i]) dfs(i,++id);
floyd();
double md[159];
for(int i = 1; i <= n; i ++){
md[i] = 0.0;
for(int j = 1;j <= n; j ++)
if(dist[i][j] < INF)
md[i] = max(md[i],dist[i][j]);
d[field[i]] = max(d[field[i]],md[i]);
}
double sa = INF,sb = 0;
for(int i = 1; i <= n; i++)
for(int j = i+1; j <= n; j ++)
{
if(field[i] != field[j]){
sb = max(max(d[field[i]],d[field[j]]),
md[i] + a[i].distince(a[j])+md[j]);
sa = min(sa,sb);
}
}
printf("%.6lf",sa);
return 0;
}