Codeforces打卡(七)

本文介绍了三道来自Codeforces编程竞赛的题目,包括解题思路和解决方案。第一题涉及数组处理,将值为i的元素移动到下一位;第二题是排序与贪心策略的应用,寻找数组中三段划分的最大差值;第三题关注字符串比较,计算替换字符的最小代价。这些题目展示了在编程竞赛中如何运用排序、贪心和字符串操作等技巧。
摘要由CSDN通过智能技术生成

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值