Codeforces 962 /2错误 相间位置排列 堆模拟 X轴距离最小值 前向星点双连通分量求只存在在一个简单环中的边
A
#include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #define TS printf("!!!\n") #define pb push_back #define inf 0x3f3f3f3f //std::ios::sync_with_stdio(false); using namespace std; //priority_queue<int,vector<int>,greater<int>> que; get min const double eps = 1.0e-10; const double EPS = 1.0e-4; typedef pair<int, int> pairint; typedef long long ll; typedef unsigned long long ull; //const int maxn = 3e5 + 10; const int turn[4][2] = {{0, 1}, { 1, 0}, { 0, -1}, { -1, 0}}; //priority_queue<int, vector<int>, less<int>> que; //next_permutation ll pre[200005]; int main() { //freopen("out.txt","w",stdout); int n; cin >> n; ll sum = 0; ll now; for (int i = 1; i <= n; i++) { cin >> now; pre[i] = pre[i - 1] + now; sum += now; } for (int i = 1; i <= n; i++) { if (pre[i]*2>= sum) { cout << i << endl; return 0; } } }
B
#include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #define TS printf("!!!\n") #define pb push_back #define inf 0x3f3f3f3f //std::ios::sync_with_stdio(false); using namespace std; //priority_queue<int,vector<int>,greater<int>> que; get min const double eps = 1.0e-10; const double EPS = 1.0e-4; typedef pair<int, int> pairint; typedef long long ll; typedef unsigned long long ull; //const int maxn = 3e5 + 10; const int turn[4][2] = {{0, 1}, { 1, 0}, { 0, -1}, { -1, 0}}; //priority_queue<int, vector<int>, less<int>> que; //next_permutation int kuai[200005]; int pop = 0; int main() { //freopen("out.txt","w",stdout); int n, a, b; cin >> n >> a >> b; string s; cin >> s; int len = s.size(); int cur = 0; for (int i = 0; i <= n; i++) { if (i == n || s[i] == '*') { kuai[++pop] = cur; cur = 0; continue; } if (s[i] == '.') { cur++; } } int anser = 0; int jian; for (int i = 1; i <= pop; i++) { if (kuai[i] == 0) { continue; } //cout << kuai[i] << " "; if (kuai[i] % 2 == 0) { anser += min(a, kuai[i] / 2) + min(b, kuai[i] / 2); a -= min(a, kuai[i] / 2); b -= min(b, kuai[i] / 2); } else { anser += min(a, kuai[i] / 2) + min(b, kuai[i] / 2); a -= min(a, kuai[i] / 2); b -= min(b, kuai[i] / 2); if (a >= b && a > 0) { anser++; a--; continue; } if (b >= a && b > 0) { anser++; b--; continue; } } } cout << anser << endl; }
C
#include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #define TS printf("!!!\n") #define pb push_back #define inf 0x3f3f3f3f //std::ios::sync_with_stdio(false); using namespace std; //priority_queue<int,vector<int>,greater<int>> que; get min const double eps = 1.0e-10; const double EPS = 1.0e-4; typedef pair<int, int> pairint; typedef long long ll; typedef unsigned long long ull; //const int maxn = 3e5 + 10; const int turn[4][2] = {{0, 1}, { 1, 0}, { 0, -1}, { -1, 0}}; //priority_queue<int, vector<int>, less<int>> que; //next_permutation ll n; ll lenn = 0; ll len = 0; ll sqr[100005]; int pop = 0; ll anser = 100; int aim[11]; int num[11]; int dfs(int x, int str) { if (x == 0) { return 1; } if (str == 0 && x != 0) { return 0; } if (x % 10 == str % 10) { return dfs(x / 10, str / 10); } else { return dfs(x, str / 10); } } int main() { //freopen("out.txt","w",stdout); cin >> n; for (ll i = 1; i <= 100000; i++) { if (1LL * i * i > 2000000000) { break; } else { sqr[i] = 1LL * i * i; pop++; } } ll cur = n; ll now = 0; int flag; while (cur > 0) { num[cur % 10]++; cur /= 10; lenn++; } for (int i = 1; i <= pop; i++) { mem(aim, 0); cur = sqr[i]; flag = dfs(cur, n); if (flag) { len = 0; cur = sqr[i]; while (cur > 0) { cur /= 10; len++; } anser = min(lenn - len,anser); } } if (anser == 100) { cout << -1 << endl; } else { cout << anser << endl; } }
D
#include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #define TS printf("!!!\n") #define pb push_back #define inf 0x3f3f3f3f //std::ios::sync_with_stdio(false); using namespace std; //priority_queue<int,vector<int>,greater<int>> que; get min const double eps = 1.0e-10; const double EPS = 1.0e-4; typedef pair<int, int> pairint; typedef long long ll; typedef unsigned long long ull; //const int maxn = 3e5 + 10; const int turn[4][2] = {{0, 1}, { 1, 0}, { 0, -1}, { -1, 0}}; //priority_queue<int, vector<int>, less<int>> que; //next_permutation ll num[150005]; int visit[150005]; int anser = 0; pair<ll, ll> cur, cnt, now; priority_queue<pair<ll, ll>, vector<pair<ll, ll> >, greater<pair<ll, ll> > > que; int main() { //freopen("out.txt","w",stdout); ll n; cin >> n; for (int i = 1; i <= n; i++) { scanf("%lld", &num[i]); } for (int i = 1; i <= n; i++) { cur.first = num[i], cur.second = i; que.push(cur); } while (!que.empty()) { cur = que.top(); que.pop(); if (que.top().first > cur.first || !que.size()) { visit[cur.second] = 1; num[cur.second] = cur.first; continue; } cnt = que.top(); que.pop(); num[cnt.second] = cur.first + cnt.first; now.first = cur.first + cnt.first, now.second = cnt.second; que.push(now); } for (int i = 1; i <= n; i++) { if (visit[i]) { anser++; } } cout << anser << endl; for (int i = 1; i <= n; i++) { if (visit[i]) { cout << num[i] << " "; } } cout << endl; }
E
#include<iostream> #include<cstring> #include<algorithm> #include<queue> #include<vector> #include<cstdio> #include<cmath> #include<set> #include<map> #include<cstdlib> #include<ctime> #include<stack> #include<bitset> using namespace std; #define mes(a) memset(a,0,sizeof(a)) #define rep(i,a,b) for(i = a; i <= b; i++) #define dec(i,a,b) for(i = b; i >= a; i--) #define fi first #define se second #define ls rt<<1 #define rs rt<<1|1 #define lson ls,L,mid #define rson rs,mid+1,R typedef double db; typedef long long int ll; typedef pair<int,int> pii; typedef unsigned long long ull; const ll inf = 1e15; const int mx = 2e5+5; const int x_move[] = {1,-1,0,0,1,1,-1,-1}; const int y_move[] = {0,0,1,-1,1,-1,1,-1}; int main(){ int t,q,ca = 1; int n; ll pa,pb,pc; ll ans = 0; pa = pb = pc = inf; ll pra = 0,prb = 0; scanf("%d",&n); for(int i = 0; i < n; i++){ ll v; char str[10]; scanf("%I64d%s",&v,str); if(str[0]=='R'){ if(pa!=inf){ pra = max(pra,v-pa); ans += v-pa; } pa = v; } else if(str[0]=='B'){ if(pb!=inf){ prb = max(prb,v-pb); ans += v-pb; } pb = v; } else{ if(pb!=inf){ prb = max(prb,v-pb); ans += v-pb; } if(pa!=inf){ pra = max(pra,v-pa); ans += v-pa; } if(pc!=inf) ans = min(ans,ans+v-pc-pra-prb); pa = pb = pc = v; pra = prb = 0; } } printf("%I64d\n",ans); return 0; }
F
因为边连通无法处理一个点同时在两个环中的情况 所以用点连通缩点 最后判一下点连通分量中是否是点的数量等于边的数量(简单环性质)
如果是的话 当前点连通分量中的边是答案的一部分
/*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define pb push_back using namespace std; typedef long long ll; typedef unsigned long long ull; const int dir[8][2] = {{0, 1}, {1, 0}, {0, -1}, { -1, 0}, {1, 1}, {1, -1}, { -1, -1}, { -1, 1}}; const int mod = 1e9 + 7; const int gakki = 5 + 2 + 1 + 19880611 + 1e9; const int MAXN = 2e5 + 5; const int MAXM = 2e5 + 5; int to[MAXM << 1], nxt[MAXM << 1], Head[MAXN], tot = 1; inline void addedge(int u, int v) { to[++tot] = v; nxt[tot] = Head[u]; Head[u] = tot; } int n, m; int dfn[MAXN], low[MAXN], dfs_clock = 0; int BCCcnt = 0, blong[MAXN], inque[MAXM << 1]; int st[MAXN], l = 0, ans[MAXN], ansnum = 0; bool vis[MAXM << 1]; void tarjanBCC(int x, int fa) { dfn[x] = low[x] = ++dfs_clock; for (int i = Head[x]; i; i = nxt[i]) { int v = to[i]; if (v == fa || vis[i]) { continue; } vis[i] = vis[i ^ 1] = 1; st[l++] = i; if (!dfn[v]) { tarjanBCC(v, x); low[x] = min(low[v], low[x]); if (dfn[x] <= low[v]) { int now, vnumber = 0, enumber = 0; BCCcnt++; while(1) { now = st[--l]; if (blong[to[now]] != BCCcnt) { blong[to[now]] = BCCcnt, ++vnumber; } if (blong[to[now ^ 1]] != BCCcnt) { blong[to[now ^ 1]] = BCCcnt, ++vnumber; } inque[++enumber] = now; if(now==i) break; } if (vnumber == enumber) { for (int i = 1; i <= enumber; i++) { ans[++ansnum] = inque[i] / 2; } } } } else { low[x] = min(low[x], dfn[v]); } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; int u, v; for (int i = 1; i <= m; i++) { cin >> u >> v; addedge(u, v), addedge(v, u); } for (int i = 1; i <= n; i++) { if (!dfn[i]) { tarjanBCC(i, -1); } } sort(ans + 1, ans + 1 + ansnum); cout << ansnum << endl; for (int i = 1; i <= ansnum; i++) { cout << ans[i] << " "; } return 0; }