12月14号
Technocup 2022 - Elimination Round 1B题
思路:因为我们的m<n所以肯定有一个点是没有出现在b数组里的我们只需要找到一个这样的点做根节点,其余的做子节点即可。
注意memset真的很慢
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1e6 + 10;
int a[N], b[N], c[N];
int st[N], vis[N];
void solve()
{
int n, m;
cin >> n >> m;
int id = 0;
for (int i = 1; i <= n; i++)
{
st[i] = 0;
vis[i] = 0;
}
for (int i = 1; i <= m; i++)
{
cin >> a[i] >> b[i] >> c[i];
st[b[i]] = 1;
}
for (int i = 1; i <= n; i++)
{
if (!st[i])
{
id = i;
vis[id] = 1;
break;
}
}
for (int i = 1; i <= m; i++)
{
if (!vis[a[i]])
{
cout << id << ' ' << a[i] << endl;
vis[a[i]] = 1;
}
if (!vis[b[i]])
{
cout << id << ' ' << b[i] << endl;
vis[b[i]] = 1;
}
if (!vis[c[i]])
{
cout << id << ' ' << c[i] << endl;
vis[c[i]] = 1;
}
}
for (int i = 1; i <= n; i++)
{
if (!vis[i])
{
cout << id << ' ' << i << endl;
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
while (T--)
{
solve();
}
return 0;
}
Codeforces Round #726 (Div. 2)C题
思路:我们从小到大排序,然后我们找到任意一组差值最小的值计下标为i,然后我们可以这样的进行排列
h
i
,
h
i
+
2
,
.
.
.
,
h
n
,
h
1
,
h
2
,
.
.
.
,
h
i
+
1
h_i,h_{i+2},...,h_n,h_1,h_2,...,h_{i+1}
hi,hi+2,...,hn,h1,h2,...,hi+1这样的话我们就可以保证是危险系数是最大的。
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1e6 + 10;
int a[N];
void solve()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
int Min = 0x3f3f3f3f;
int id;
sort(a + 1, a + n + 1);
for (int i = 1; i < n; i++)
{
if (a[i + 1] - a[i] < Min)
{
Min = a[i + 1] - a[i];
id = i;
}
}
cout << a[id] << ' ';
for (int i = id + 2; i <= n; i++)
cout << a[i] << ' ';
for (int i = 1; i < id; i++)
cout << a[i] << ' ';
cout << a[id + 1] << endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
while (T--)
{
solve();
}
return 0;
}
Codeforces Round #738 (Div. 2)C题
思路:这个题的话就是三种情况如果a[1]=1的话,我们就从n+1到n就可以了。如果a[n]=0的话我们就从1到n+1即可。还有一种就是两个连续的数,前面的一个是指向n+1,后面一个被n+1指向。其余的情况都是不可以的。
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1e6 + 10;
int a[N];
void solve()
{
int n;
cin >> n;
if (n == 1)
{
int x;
cin >> x;
if (x == 0)
cout << "1 2" << endl;
else
cout << "2 1" << endl;
return;
}
int sum = 0;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
if (a[i])
sum++;
}
if (a[n] == 0)
{
for (int i = 1; i <= n + 1; i++)
cout << i << ' ';
cout << endl;
}
else if (a[1] == 1)
{
cout << n + 1 << " ";
for (int i = 1; i <= n; i++)
cout << i << ' ';
cout << endl;
}
else
{
int st = 0;
int l, r;
for (int i = 2; i <= n; i++)
{
if (a[i] == 1 && a[i - 1] == 0)
{
st = 1;
l = i - 1, r = i;
}
}
if (!st)
cout << -1 << endl;
else
{
for (int i = 1; i <= n; i++)
{
cout << i << ' ';
if (i == l)
{
cout << n + 1 << ' ';
}
}
cout << endl;
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
while (T--)
{
solve();
}
return 0;
}