12月23号
Codeforces Global Round 23C题
思路:此题就是把i加到a[i]=i的后面一个及i+1个位置
#include <cstring>
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
const int N = 1e6 + 10;
int a[N];
map<int, int> mp;
void solve()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
if (i == n)
{
mp[a[i]] = i;
}
else
{
mp[a[i]] = i + 1;
}
}
for(int i=1;i<=n;i++)
{
cout<<mp[i]<<' ';
}
cout<<endl;
mp.clear();
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
while (T--)
{
solve();
}
return 0;
}
Codeforces Round #831 (Div. 1 + Div. 2)C题
思路:其实这个题的话我们可以先排好序,然后通过分析其实就是让我们分成三段,然后值最大的那段的最小值减去其余两段的最大值。抽象出模型来之后我们贪心可知要么就是留一个最小值,要么就是留一个最大值,所以我们从前往后和从后往前遍历一遍就可以了。
#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];
sort(a + 1, a + n + 1);
int Max = -1;
for (int i = 3; i <= n; i++)
Max = max(Max, abs(a[i] - a[1]) + abs(a[i] - a[i - 1]));
for (int i = 1; i <= n - 2; i++)
Max = max(Max, abs(a[i] - a[n]) + abs(a[i] - a[i + 1]));
cout << Max << endl;
}
int main()
{
cin.tie(0)->sync_with_stdio(false);
int T;
cin >> T;
while (T--)
{
solve();
}
return 0;
}
Codeforces Round #821 (Div. 2)C题
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define int long long
void solve()
{
int n, x, y;
string s1, s2;
cin >> n >> x >> y;
cin >> s1 >> s2;
int cnt1 = 0, cnt2 = 0;
for (int i = 0; i < n; i++)
if (s1[i] != s2[i])
cnt1++;
for (int i = 0; i < n; i++)
{
if (i != 0 && s1[i] != s2[i] && s1[i - 1] != s2[i - 1])
{
cnt2++;
i++;
}
}
if (cnt1 % 2 == 1)
{
cout << -1 << endl;
return;
}
int ans = 0x3f3f3f3f;
if (cnt1 == 2 && cnt2 == 1)
{
ans = min(x, 2 * y);
}
else
{
ans = cnt1 / 2 * y;
}
cout << ans << endl;
}
signed main()
{
cin.tie(0)->sync_with_stdio(false);
int T;
cin >> T;
while (T--)
{
solve();
}
return 0;
}