B
题意
给你n个数,它们的顺序是非递减的,问你现在是否有一种排列顺序让排列后的顺序与原顺序相同,如果有输出排列后的坐标,没有输出-1
思路
既然要是非递减的顺序,那么每个数出现的次数必须是大于等于2的,这样交换了两个位置才不会出现比原来值小的情况。因此我们只需要判断是否出现过的每个数的次数都是大于等于2的,如果是,将这几个相同的位置,内部进行顺序交换即可。
代码:
#include <bits/stdc++.h>
#define int long long
#define x first
#define y second
using namespace std;
const int N = 1e5 + 10;
int n;
int a[N];
void solve()
{
bool flag = 1;
map<int, int> mp;
cin >> n;
for (int i = 1; i <= n; i ++)
{
cin >> a[i];
mp[a[i]] ++;
}
for (auto it : mp)
if (it.y <= 1) { flag = 0; break; }
if (!flag) { cout << "-1\n"; return ; }
int l = 1, r = 1, tmp = a[1];
for (int i = 2; i <= n; i ++)
{
if (a[i] == tmp) r = i;
if (a[i] != tmp || n == i)
{
cout << r << ' ';
for (int j = l; j < r; j ++)
cout << j << ' ';
l = i, r = i;
tmp = a[i];
}
}
cout << endl;
}
signed main()
{
std::ios::sync_with_stdio(false);
int t; cin >> t;
while (t --) solve();
return 0;
}
C
题意
给你一个01字符串s,d[i] = s[i][i + 1],01 = 1,10 = 10,11= 11,让你求它们的和。
思路
最先没有读到只能交换相邻的两个数。
我们先抛开首尾两个数,只看中间的数,对于一个位置来说,他能做出的贡献是,10s[i] + 1s[i],意思是每个位置,只要它是1,它能做出的贡献就是11,而对于第一个数,它只能贡献10,而最后一个数,它只能贡献1,所以我们将最后一个1移动到末尾,剩余的1移动到开头。
#include <bits/stdc++.h>
#define int long long
#define x first
#define y second
using namespace std;
void solve()
{
int n, k; cin >> n >> k;
string s; cin >> s;
s = " " + s;
int cnt = 0, l = 0, r = 0;
int ans = 0;
for (int i = 1; i <= n; i ++)
{
if (l == 0 && s[i] == '1') l = i;
if (s[i] == '1') { r = i; cnt ++; }
}
if (cnt && n - r <= k)
{
cnt --;
k -= n - r;
ans += 1;
}
if (cnt && l - 1 <= k)
{
ans += 10;
cnt --;
}
cout << ans + cnt * 11 << endl;
}
signed main()
{
std::ios::sync_with_stdio(false);
int t; cin >> t;
while (t --) solve();
return 0;
}