每日水題 11-26
文章目錄
ACM Contest and Blackout
ACM Contest and Blackout
次小生成樹裸題
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<set>
#include<map>
#include<queue>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const double Pi = acos(-1);
namespace io{
template <typename T> inline void read(T &x) {
x = 0; T f = 1;char s = getchar();
for(; !isdigit(s); s = getchar()) if(s == '-') f = -1;
for(; isdigit(s); s = getchar()) x = (x << 3) + (x << 1) + (s ^ 48);
x *= f;
}
template <typename T> inline void write(T x) {
if(x < 0) putchar('-'), x = -x;
if(x > 9) write(x/10);
putchar(x%10+'0');
}
};
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (register int i = (n); i < (m); ++i)
#define _rep(n,m,i) for (register int i = (n); i <= (m); ++i)
#define _srep(n,m,i)for (register int i = (n); i >= (m); i--)
#define _sfor(n,m,i)for (register int i = (n); i > (m); i--)
#define ef(u, i) for(int i = head[u]; i; i = e[i].next)
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define lowbit(x) x & (-x)
#define pii pair<int,int>
#define fi first
#define se second
const int N = 1e2+5;
const int M = 1e4+5;
struct node {
int u, v, w;
bool operator < (const node &a) const {
return w < a.w;
}
}e[M];
int f[N];
int Find(int x) {
return x == f[x] ? x : f[x] = Find(f[x]);
}
bool brid[M];
int mx[N][N];
vector<int> fv[N];
int main() {
int t; scanf("%d", &t);
while(t--) {
int n, m;
scanf("%d %d", &n, &m);
for(int i = 1; i <= n; ++i) {
f[i] = i;
fv[i].clear();
fv[i].push_back(i);
}
for(int i = 0; i < m; ++i) {
brid[i] = 0;
scanf("%d %d %d", &e[i].u, &e[i].v, &e[i].w);
}
sort(e, e + m);
int num = n-1, ans = 0;
for(int i = 0; i < m; ++i) {
int fx = Find(e[i].u), fy = Find(e[i].v);
if(fx != fy) {
brid[i] = 1;
ans += e[i].w;
if(fv[fx].size() > fv[fy].size()) swap(fx, fy);
f[fx] = fy;
for(auto it : fv[fx]) {
for(auto is : fv[fy]) {
mx[it][is] = mx[is][it] = e[i].w;
}
}
for(auto it : fv[fx]) {
fv[fy].push_back(it);
}
fv[fx].clear();
num--;
}
if(!num) break;
}
int cans = INF;
for(int i = 0; i < m; ++i) {
if(!brid[i]) {
cans = min(cans, ans - mx[e[i].u][e[i].v] + e[i].w);
}
}
printf("%d %d\n", ans, cans);
}
return 0;
}
Clairewd’s message
Clairewd’s message
考察kmp性質。貌似看別人都是用exkmp。
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<set>
#include<map>
#include<queue>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const double Pi = acos(-1);
namespace io{
template <typename T> inline void read(T &x) {
x = 0; T f = 1;char s = getchar();
for(; !isdigit(s); s = getchar()) if(s == '-') f = -1;
for(; isdigit(s); s = getchar()) x = (x << 3) + (x << 1) + (s ^ 48);
x *= f;
}
template <typename T> inline void write(T x) {
if(x < 0) putchar('-'), x = -x;
if(x > 9) write(x/10);
putchar(x%10+'0');
}
};
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (register int i = (n); i < (m); ++i)
#define _rep(n,m,i) for (register int i = (n); i <= (m); ++i)
#define _srep(n,m,i)for (register int i = (n); i >= (m); i--)
#define _sfor(n,m,i)for (register int i = (n); i > (m); i--)
#define ef(u, i) for(int i = head[u]; i; i = e[i].next)
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define lowbit(x) x & (-x)
#define pii pair<int,int>
#define fi first
#define se second
const int N = 1e5+5;
char zh[27], fzh[27];
char s[N*2];
int nx[N*2];
void solve(int m) {
int n = strlen(s), k = 0;
for(int i = 1; i < n; ++i) {
while(k && s[i] != s[k]) k = nx[k];
if(s[i] == s[k]) k++;
nx[i+1] = k;
}
int len = m - nx[n];
if(len * 2 < m) {
len = m - nx[m];
k = m / len + (m % len != 0);
len = (k+1)/2*len;
}
for(int i = 0; i < len; ++i) {
s[i+len] = fzh[s[i]-'a'];
}
s[len*2] = '\0';
printf("%s\n", s);
}
int main() {
int t; scanf("%d", &t);
while(t--) {
scanf("%s", zh);
for(int i = 0; i < 26; ++i) {
fzh[zh[i]-'a'] = i + 'a';
}
scanf("%s", s);
int n = strlen(s);
s[n] = '#';
for(int i = 0; i < n; ++i) {
s[i+n+1] = zh[s[i]-'a'];
}
s[n+n+1] = '\0';
solve(n);
}
}
Constructing Roads
Constructing Roads
最小生成樹prim
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<set>
#include<map>
#include<queue>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const double Pi = acos(-1);
namespace io{
template <typename T> inline void read(T &x) {
x = 0; T f = 1;char s = getchar();
for(; !isdigit(s); s = getchar()) if(s == '-') f = -1;
for(; isdigit(s); s = getchar()) x = (x << 3) + (x << 1) + (s ^ 48);
x *= f;
}
template <typename T> inline void write(T x) {
if(x < 0) putchar('-'), x = -x;
if(x > 9) write(x/10);
putchar(x%10+'0');
}
};
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (register int i = (n); i < (m); ++i)
#define _rep(n,m,i) for (register int i = (n); i <= (m); ++i)
#define _srep(n,m,i)for (register int i = (n); i >= (m); i--)
#define _sfor(n,m,i)for (register int i = (n); i > (m); i--)
#define ef(u, i) for(int i = head[u]; i; i = e[i].next)
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define lowbit(x) x & (-x)
#define pii pair<int,int>
#define fi first
#define se second
int a[105][105];
int dis[105];
bool vis[105];
int main() {
int n; cin >> n;
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= n; ++j) {
cin >> a[i][j];
}
}
int m, u, v; cin >> m;
dis[0] = INF;
while(m--) {
cin >> u >> v;
a[u][v] = a[v][u] = 0;
}
for(int i = 1; i <= n; ++i) dis[i] = a[1][i];
vis[1] = 1;
int ans = 0;
for(int i = 1; i < n; ++i) {
int j = 0;
for(int k = 1; k <= n; ++k) {
if(!vis[k] && dis[k] < dis[j]) {
j = k;
}
}
if(!j) break;
vis[j] = 1;
ans += dis[j];
for(int k = 1; k <= n; ++k) {
if(!vis[k] && dis[k] > a[j][k]) {
dis[k] = a[j][k];
}
}
}
cout << ans << endl;
return 0;
}
Mayor’s posters
Mayor’s posters
線段樹+離散化+懶標記
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<set>
#include<map>
#include<queue>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const double Pi = acos(-1);
namespace io{
template <typename T> inline void read(T &x) {
x = 0; T f = 1;char s = getchar();
for(; !isdigit(s); s = getchar()) if(s == '-') f = -1;
for(; isdigit(s); s = getchar()) x = (x << 3) + (x << 1) + (s ^ 48);
x *= f;
}
template <typename T> inline void write(T x) {
if(x < 0) putchar('-'), x = -x;
if(x > 9) write(x/10);
putchar(x%10+'0');
}
};
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (register int i = (n); i < (m); ++i)
#define _rep(n,m,i) for (register int i = (n); i <= (m); ++i)
#define _srep(n,m,i)for (register int i = (n); i >= (m); i--)
#define _sfor(n,m,i)for (register int i = (n); i > (m); i--)
#define ef(u, i) for(int i = head[u]; i; i = e[i].next)
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define lowbit(x) x & (-x)
#define pii pair<int,int>
#define fi first
#define se second
const int N = 1e5+5;
struct node {
int l, r;
}e[N];
vector<int> ve;
int getid(int x) {
return lower_bound(ve.begin(), ve.end(), x) - ve.begin() + 1;
}
int T[N<<3], lazy[N<<3];
void build(int rt, int l, int r) {
T[rt] = lazy[rt] = 0;
if(l == r) {
return ;
}
int mid = l + r >> 1;
build(lson); build(rson);
}
void down(int rt) {
if(lazy[rt]) {
T[rt<<1] = lazy[rt];
lazy[rt<<1] = lazy[rt];
T[rt<<1|1] = lazy[rt];
lazy[rt<<1|1] = lazy[rt];
lazy[rt] = 0;
}
}
void upd(int rt, int l, int r, int L, int R , int val) {
if(L <= l && R >= r) {
lazy[rt] = val;
T[rt] = val;
return ;
}
down(rt);
int mid = l + r >> 1;
if(mid >= L) upd(lson, L, R, val);
if(mid < R) upd(rson, L, R, val);
}
set<int> se;
void qry(int rt, int l, int r) {
if(l == r) {
if(T[rt])se.insert(T[rt]);
return ;
}
down(rt);
int mid = l + r >> 1;
qry(lson); qry(rson);
}
int main() {
int t; scanf("%d", &t);
while(t--) {
int n; scanf("%d", &n);
ve.clear();
for(int i = 0; i < n; ++i) scanf("%d %d", &e[i].l, &e[i].r), ve.push_back(e[i].l),ve.push_back(e[i].r);
sort(ve.begin(), ve.end());
ve.erase(unique(ve.begin(), ve.end()), ve.end());
int len = ve.size();
build(1, 1, len);
for(int i = 1; i <= n; ++i)
upd(1,1,len,getid(e[i-1].l), getid(e[i-1].r), i);
se.clear();
qry(1, 1, len);
printf("%d\n", se.size());
}
return 0;
}
Heavy Transportation
Heavy Transportation
最大生成樹,使1和n連通的那條邊
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<set>
#include<map>
#include<queue>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const double Pi = acos(-1);
namespace io{
template <typename T> inline void read(T &x) {
x = 0; T f = 1;char s = getchar();
for(; !isdigit(s); s = getchar()) if(s == '-') f = -1;
for(; isdigit(s); s = getchar()) x = (x << 3) + (x << 1) + (s ^ 48);
x *= f;
}
template <typename T> inline void write(T x) {
if(x < 0) putchar('-'), x = -x;
if(x > 9) write(x/10);
putchar(x%10+'0');
}
};
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (register int i = (n); i < (m); ++i)
#define _rep(n,m,i) for (register int i = (n); i <= (m); ++i)
#define _srep(n,m,i)for (register int i = (n); i >= (m); i--)
#define _sfor(n,m,i)for (register int i = (n); i > (m); i--)
#define ef(u, i) for(int i = head[u]; i; i = e[i].next)
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define lowbit(x) x & (-x)
#define pii pair<int,int>
#define fi first
#define se second
const int N = 1e5+5;
struct node {
int u, v, w;
bool operator < (const node &a) const {
return w > a.w;
}
}e[N];
int f[N];
int Find(int x) {
return x == f[x] ? x : f[x] = Find(f[x]);
}
int main() {
int t; scanf("%d", &t);
for(int ca = 1; ca <= t; ++ca) {
int n, m; scanf("%d %d", &n, &m);
for(int i = 1; i <= n; ++i) f[i] = i;
for(int i = 0; i < m; ++i) {
scanf("%d %d %d", &e[i].u, &e[i].v, &e[i].w);
}
sort(e, e + m);
int w;
for(int i = 0; i < m; ++i) {
int fx = Find(e[i].u), fy = Find(e[i].v);
if(fx != fy) {
f[fx] = fy;
w = e[i].w;
}
if(Find(1) == Find(n)) break;
}
printf("Scenario #%d:\n%d\n\n", ca, w);
}
return 0;
}
相關文章
- leedcode每日一題:860. 檸檬水找零每日一題
- LeetCode每日一題: 檸檬水找零(No.860)LeetCode每日一題
- 水題得分...
- 每日一題每日一題
- 每日"兩"題 題解
- 【Leetcode 每日一題】1030. 距離順序排列矩陣單元格(水題,曼哈頓距離排序)LeetCode每日一題矩陣排序
- 3 月水題練習
- 【每日一題】20241205每日一題
- 【每日一題】20241208每日一題
- 【每日一題】20241203每日一題
- 每日一題1每日一題
- 每日刷題 3.17
- 每日五題(Spring)Spring
- leetcode每日一題LeetCode每日一題
- 每日一題 2020.11.5每日一題
- 每日一題2每日一題
- hdu2045 遞迴水題遞迴
- sicp每日一題[2.78]每日一題
- sicp每日一題[2.83]每日一題
- sicp每日一題[2.79]每日一題
- sicp每日一題[2.82]每日一題
- sicp每日一題[2.73]每日一題
- sicp每日一題[2.71]每日一題
- sicp每日一題[2.66]每日一題
- sicp每日一題[2.62]每日一題
- sicp每日一題[2.61]每日一題
- sicp每日一題[2.59]每日一題
- sicp每日一題[2.58]每日一題
- sicp每日一題[2.60]每日一題
- sicp每日一題[2.47]每日一題
- sicp每日一題[2.56]每日一題
- sicp每日一題[2.49]每日一題
- sicp每日一題[2.43]每日一題
- sicp每日一題[2.44]每日一題
- sicp每日一題[2.33]每日一題
- sicp每日一題[2.29]每日一題
- sicp每日一題[2.30]每日一題
- sicp每日一題[2.19]每日一題