成绩
很简单的模拟。。。
我们找到结束的坐标 ,然后 循环,每一次都先输出当前位置,然后更新当前位置。
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e3 + 5;
int x[N][N] , y[N][N];
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n , m , sx , sy;
cin >> m >> n >> sx >> sy;
int ex , ey;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
cin >> x[i][j] >> y[i][j];
if (x[i][j] == 0 && y[i][j] == 0)
{
ex = i;
ey = j;
}
}
}
// cout << ex << " " << ey << endl;
int nx = 1 , ny = 1;
while (x[nx][ny] != 0 && y[nx][ny] != 0)
{
cout << nx << " " << ny << endl;
int fx = nx , fy = ny;
nx = x[fx][fy];
ny = y[fx][fy];
// cout << nx << " " << ny << " " << x[nx][ny] << " " << y[nx][ny] << endl;
}
cout << nx << " " << ny << endl;
return 0;
}每一次都是从 开始旅行的,但我是从 开始的…痛失70分┭┮﹏┭┮
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e3 + 5;
int x[N][N] , y[N][N];
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n , m , sx , sy;
cin >> m >> n >> sx >> sy;
int ex , ey;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
cin >> x[i][j] >> y[i][j];
if (x[i][j] == 0 && y[i][j] == 0) // 找到结束的点
{
ex = i;
ey = j;
}
}
}
// cout << ex << " " << ey << endl;
int nx = sx , ny = sy;
while (x[nx][ny] != 0 && y[nx][ny] != 0)
{
cout << nx << " " << ny << endl; // 输出答案
int fx = nx , fy = ny;
nx = x[fx][fy];
ny = y[fx][fy]; // 更新当前位置
// cout << nx << " " << ny << " " << x[nx][ny] << " " << y[nx][ny] << endl;
}
cout << nx << " " << ny << endl;
return 0;
}记忆化搜索……
-
函数
因为这道题数据范围比较大,所以我们需要用记忆化搜索来做。
我们用 数组来存答案,然后遍历所有跟当前点有关的点,累计答案。
-
主函数
我们通过题目给的图可以知道,其实输入的长度为 的序列其实是一棵树,所以我们输入后就可以利用 来建边存树。
然后我们 ,最后输出 。
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 5;
int u[N] , a[N] , f[N];
vector <int> g[N];
int dfs(int step)
{
f[step] = a[step]; // 当前点至少会有 a[step] 这么多
int len = g[step].size();
for (int i = 0; i < len; i++) // 遍历所有跟当前点有关联的点
{
int u = g[step][i];
f[step] += dfs(u); // 累计答案
}
return f[step];
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 2; i <= n; i++)
{
cin >> u[i];
g[u[i]].push_back(i); // 建边
}
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
dfs(1);
for (int i = 1; i <= n; i++)
{
cout << f[i] << " "; // 输出答案
}
return 0;
}我们分多种情况来讨论:
这个字符串本身就是回文字符串
① 位数是偶数(以 ABCDEF 为例)
我们可以知道,如果样例给的是 ABCDEF,那么 ABCCBA 肯定最小回文数。但这里我们也要分几种情况:
-
ABCDEFABCCBA那么
ABCCBA就是答案。 -
ABCDEFABCCBA
我们就取出 ABC,然后将 C 加一,答案字符串就为 AB(C+1)(C+1)BA。
但是这里有一个特例,就是如果 C 为9,那 C 加一就是 :,就出错了。我们可以加上一个判断,但又不能只加一个,因为有可能 B 也为9。所以我们加上一个循环,只要当前字符是 9,我们就判断下一个字符,否则,当前字符加一,退出循环,我们将这个修改后的字符串称为 GHI。
最后答案就是 GHIIHG
② 位数是奇数(以 ABCDEFG 为例)
我们可以知道,如果样例给的是 ABCDEFG,那么 ABCDCBA 肯定最小回文数。但这里我们也要分几种情况:
-
ABCDCBAABCDEFG答案就是
ABCDCBA。 -
ABCDCBAABCDEFG我们就让
D加一,答案字符串就为ABC(D+1)CBA。但是这里有一个特例,就是如果
D为9,那D加一就是:,就出错了。我们可以加上一个判断,但又不能只加一个,因为有可能C也为9。所以我们加上一个循环,只要当前字符是9,我们就判断下一个字符,否则,当前字符加一,退出循环,我们将这个修改后的字符串称为HIJK。最后答案就为
HIJKJIH。
这个字符串本身不是回文字符串
与上面的同理。
还有一个特例
这个特例就是全是9的情况。如果全是9,我们就先输出一个1,然后输出 个 0(len 为字符串长度),最后再输出一个1。(可以自己推导)
#include <bits/stdc++.h>
#define int long long
using namespace std;
bool check(string s , int lens)
{
string st = "";
for (int i = lens - 1; i >= 0; i--)
{
st += s[i];
}
if (s == st)
{
return 1;
}
return 0;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string s;
cin >> s;
int lens = s.size();
if (check(s , lens) == 1)
{
string t = s.substr(0 , lens / 2);
int lent = t.size();
if (lens % 2 == 0)
{
t[lent - 1]++;
cout << t;
string st = "";
for (int i = lent - 1; i >= 0; i--)
{
st += t[i];
}
cout << st;
}
else
{
cout << t;
cout << char(s[lent] + 1);
string st = "";
for (int i = lent - 1; i >= 0; i--)
{
st += t[i];
}
cout << st;
}
}
else
{
string t = s.substr(0 , lens / 2);
int lent = t.size();
if (lens % 2 == 0)
{
// t[lent - 1]++;
if (t[0] <= s[lens - 1])
{
t[lent - 1]++;
// cout << s[lens - 1] << " " << t[0] << " ";
}
cout << t;
string st = "";
for (int i = lent - 1; i >= 0; i--)
{
st += t[i];
}
cout << st;
}
else
{
cout << t;
cout << char(s[lent] + 1);
// cout << s[lent + 1];
// cout << lent;
string st = "";
for (int i = lent - 1; i >= 0; i--)
{
st += t[i];
}
cout << st;
}
}
return 0;
}没有判断9的情况。
#include <bits/stdc++.h>
#define int long long
using namespace std;
bool check(string s , int lens)
{
string st = "";
for (int i = lens - 1; i >= 0; i--)
{
st += s[i];
}
if (s == st)
{
return 1;
}
return 0;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string s;
cin >> s;
if (s == "0" || s == "1" || s == "2" || s == "3" || s == "4" || s == "5" || s == "6" || s == "7" || s == "8" || s == "9")
{
cout << 11;
return 0;
}
int lens = s.size();
bool flag = 0;
for (int i = 0; i < lens; i++)
{
if (s[i] != '9')
{
flag = 1;
}
}
if (flag == 0)
{
cout << 1;
for (int i = 1; i <= lens - 1; i++)
{
cout << 0;
}
cout << 1;
return 0;
}
if (check(s , lens) == 1)
{
string t = s.substr(0 , lens / 2);
int lent = t.size();
if (lens % 2 == 0)
{
for (int i = lent - 1; i >= 0; i--)
{
if (t[i] == '9')
{
t[i] = '0';
}
else
{
t[i]++;
break;
}
}
cout << t;
string st = "";
for (int i = lent - 1; i >= 0; i--)
{
st += t[i];
}
cout << st;
}
else
{
char x = s[lent];
if (x == '9')
{
x = '0';
for (int i = lent - 1; i >= 0; i--)
{
if (t[i] == '9')
{
t[i] = '0';
}
else
{
t[i]++;
break;
}
}
}
else
{
x++;
}
cout << t;
cout << x;
string st = "";
for (int i = lent - 1; i >= 0; i--)
{
st += t[i];
}
cout << st;
}
}
else
{
string t = s.substr(0 , lens / 2);
int lent = t.size();
// cout << s.substr(lens / 2 , lens / 2) << endl;
if (lens % 2 == 0)
{
// t[lent - 1]++;
string str = t;
reverse(str.begin() , str.end());
if (str <= s.substr(lens / 2 , lens / 2))
{
for (int i = lent - 1; i >= 0; i--)
{
if (t[i] == '9')
{
t[i] = '0';
}
else
{
t[i]++;
break;
}
}
// cout << s[lens - 1] << " " << t[0] << " ";
}
cout << t;
string st = "";
for (int i = lent - 1; i >= 0; i--)
{
st += t[i];
}
cout << st;
}
else
{
char x = s[lent];
if (x == '9')
{
x = '0';
for (int i = lent - 1; i >= 0; i--)
{
if (t[i] == '9')
{
t[i] = '0';
}
else
{
t[i]++;
break;
}
}
}
else
{
x++;
}
cout << t;
cout << x;
string st = "";
for (int i = lent - 1; i >= 0; i--)
{
st += t[i];
}
cout << st;
}
}
return 0;
}实在调不出来了awa
这道题什么情况下会输出 No 呢?我们画图分析后可以得知,如果有环,就输出 No。
我们考虑没有环的情况。我们找到一个点对 ,将它设为开头的,然后剩下的直接连边。此时我们就有若干个联通块,最后我们把这些联通块都串联起来。
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 3e5 + 5;
int x[N] , y[N];
int fa[N];
int get(int x)
{
if (x == fa[x])
{
return x;
}
return fa[x] = get(fa[x]);
}
void unite(int x , int y)
{
x = get(x);
y = get(y);
if (x != y)
{
fa[x] = y;
}
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n , m;
cin >> n >> m;
for (int i = 1; i <= m; i++)
{
cin >> x[i] >> y[i];
}
for (int i = 1; i <= n; i++)
{
fa[i] = i;
}
for (int i = 1; i <= m; i++)
{
int nx = get(x[i]);
int ny = get(y[i]);
if (nx != ny)
{
unite(nx , ny);
}
else
{
cout << "No";
return 0;
}
}
cout << "Yes" << endl;
for (int i = 1; i <= m - 1; i++)
{
cout << x[i] << " " << y[i] << endl;
}
int pre = x[m];
for (int i = 1; i <= n; i++)
{
if (get(i) == i && get(i) != get(x[m]))
{
cout << pre << " " << i << endl;
pre = i;
}
}
cout << pre << " " << y[m];
return 0;
}