• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

.yin

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

Codeforces Round #719 (Div. 3) E题- Arranging The Sheep

传送门:Problem - E - Codeforces

官方题解:

Let's denote by kk the number of sheep in the string, and by x1,x2,…,xkx1,x2,…,xk (1≤x1<x2<…<xk≤n1≤x1<x2<…<xk≤n) their positions in the string.

Note that in the optimal solution the sheep with the number m=⌈n2⌉m=⌈n2⌉ will not make moves. This can be proved by considering the optimal solution in which the sheep with the number mm makes at least one move and come to the conclusion that this solution is not optimal. Consider sheep with numbers from i=1i=1 to nn. Then the final position of the ii-th sheep will be xm−m+ixm−m+i, and the answer will be ∑i=1k|xi−(xm−m+i)|∑i=1k|xi−(xm−m+i)|.

(自行翻译)

我的代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--) {
        int n;
        cin>>n;
        char s[n];
        scanf("%s",s);
        int a[n],l=0;
        for(int i=0;i<n;i++) {
            if(s[i]=='*') {
                a[l++]=i;
            }
        }
        long long sum=0;
        for(int i=0;i<l;i++) {
            sum+=abs(a[i]-(a[]-l/2+i));
        }
        cout<<sum<<endl;
    }
    return 0;
}
View Code

 

posted on 2021-05-07 20:59  .yin  阅读(78)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3