普通dfs算法,不过要理解 2 ∗ s u m − d 2*sum - d 2∗sum−d
#include <bits/stdc++.h>
#define rep(i, a, b) for(int i = a; i <= b; i ++ )
#define deb2(x, y) cout << #x << '=' << x << ',' << #y << '=' << y <<endl
using namespace std;
const int N = 1e5 + 10, mod = 1e9 + 7;
typedef long long ll;
typedef pair<int, int> PII;
int p[N], d[N];
ll sum;
int mx;
int dfs(int u) {
if(p[u] == -1 || d[u]) {
return d[u];
}
sum ++;
d[u] = dfs(p[u]) + 1;
return d[u];
}
void run()
{
int n, m; cin >> n >> m;
int i;
for (i = 1; i <= n; i ++ ) cin >> p[i];
while(m -- ) {
int x; cin >> x;
mx = max(mx, dfs(x));
cout << 2*sum - mx << endl;
}
}
signed main()
{
ios::sync_with_stdio(false), cin.tie(0);
cout << fixed; cout.precision(18);
int T = 1;
//cin >> T;
while(T -- ) run();
}
从字符串输入输出可以更方便
#include<iostream>
using namespace std;
int main(){
int y, m, d;
char s[] = "hello 2023/1/15", k[20];
int num = sscanf(s, "%s %d/%d/%d", k, &y, &m, &d);
printf("成功填充的项数: %d\n", num); // 4
printf("%s %d/%d/%d", k, y, m, d); // hello 2023/1/15
}
与c++的string类型对象无法兼容,需要搭配c_str()函数使用转换为c语言字符串指针
借助c_str()函数(cstring库),将string类型字符串转为 const char *
#include<iostream>
#include<cstring> // c_str()函数头文件
using namespace std;
int main(){
int y, m, d;
char k[20];
string s = "hello 2023/1/15";
int num = sscanf(s.c_str(), "%s %d/%d/%d", k, &y, &m, &d);
printf("成功填充的项数: %d\n", num); // 4
printf("%s %d/%d/%d", k, y, m, d); // hello 2023/1/15
}
int get(string s)
{
int hour, minute, second;
sscanf(s.c_str(), "%d:%d:%d", &hour, &minute, &second);
return hour * 3600 + minute * 60 + second;
}
string format(int x)
{
int hour = x / 3600;
x %= 3600;
int minute = x / 60;
int second = x % 60;
char str[10];
sprintf(str, "%02d:%02d:%02d", hour, minute, second);
return str;
}
#include <bits/stdc++.h>
#define rep(i, a, b) for(int i = a; i <= b; i ++ )
#define deb2(x, y) cout << #x << '=' << x << ',' << #y << '=' << y <<endl
using namespace std;
const int N = 2e5 + 10, mod = 1e9 + 7;
typedef long long ll;
typedef pair<int, int> PII;
int st[24*3600+10];
int get(string str) {
int h, m, s;
sscanf(str.c_str(), "%d:%d:%d", &h, &m, &s);
return h*3600 +m*60 + s;
}
string format(int x) {
int h = x / 3600;
int m = x / 60 % 60;
int s = x % 60;
char str[10];
sprintf(str, "%02d:%02d:%02d", h, m, s);
return str;
}
void run()
{
int n; cin >> n;
while(n -- ) {
string s1, s, s2;
cin >> s1 >> s >> s2;
int l = get(s1), r = get(s2);
int i;
for (i = l; i < r; i ++) st[i] = 1;
}
int i, j;
for (i = 0; i < 24*3600-1; i ++ ) {
if(st[i] == 0) {
j = i+1;
while(j < 24*3600-1 && st[j] == 0) j ++ ;
cout <<format(i) <<" - " << format(j) <<endl;
i = j;
}
}
}
signed main()
{
ios::sync_with_stdio(false), cin.tie(0);
cout << fixed; cout.precision(18);
int T = 1;
//cin >> T;
while(T -- ) run();
}
典型模拟,需要吧else if那些整理清楚
#include <bits/stdc++.h>
#define rep(i, a, b) for(int i = a; i <= b; i ++ )
#define deb2(x, y) cout << #x << '=' << x << ',' << #y << '=' << y <<endl
using namespace std;
const int N = 1010, mod = 1e9 + 7;
typedef long long ll;
typedef pair<int, int> PII;
int a[N];
void run()
{
int n, m, k; cin >> n >> m >> k;
int i;
stack<int> st;
queue<int> q;
for (i = 1; i <= n; i ++ ) cin >> a[i], q.push(a[i]);
while(q.size() || st.size()) {
int cnt = 0, last = 110;
while(true) {
if(cnt == k) break;
if(st.size() && st.top() <= last) {
cout << st.top() << ' ';
last = st.top(); st.pop(); cnt ++ ;
}
else if(q.size()) {
int t = q.front();
if(t <= last) {
cout << t << ' ';
last = t; q.pop(); cnt ++ ;
} else if(st.size() < m) {
st.push(t); q.pop();
} else break;
}
else break;
}
cout << endl;
}
}
signed main()
{
ios::sync_with_stdio(false), cin.tie(0);
cout << fixed; cout.precision(18);
int T = 1;
//cin >> T;
while(T -- ) run();
}
flody算法
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 510;
int d[N][N];
char sex[N];
int main()
{
memset(d, 0x3f, sizeof d);
int n, i, j; cin >> n;
for (i = 1; i <= n; i ++ ) {
char s[2]; int cnt;
scanf("%s%d", s, &cnt);
sex[i] = s[0];
while(cnt -- ) {
int v, x;
scanf("%d:%d", &v, &x);
d[i][v] = x;
}
}
int k;
for (k = 1; k <= n; k ++ )
for (i = 1; i <= n; i ++ )
for (j = 1; j <= n; j ++ )
if(d[i][j] >= d[i][k] + d[k][j])
d[i][j] = d[i][k] + d[k][j];
for (auto c : string("FM")) {
int minx = 0x3f3f3f3f;
for (i = 1;i <= n; i ++) {
if(sex[i] == c) {
int maxn = 0;
for (j = 1; j <= n; j ++ )
if(sex[j] != c) maxn = max(maxn, d[j][i]);
minx = min(minx, maxn);
}
}
for (i = 1;i <= n; i ++) {
if(sex[i] == c) {
int maxn = 0;
for (j = 1; j <= n; j ++ )
if(sex[j] != c) maxn = max(maxn, d[j][i]);
if(minx == maxn) cout <<i << ' ';
}
}
cout << endl;
}
}