2 条题解

  • 1
    @ 2025-11-9 9:02:54
    #include <bits/stdc++.h>
    using namespace std;
    #define int long long
    int a[10005];
    void solve() {
        int n; cin >> n;
        for(int i = 1; i <= n; i++) cin >> a[i];
        int ans = 1e18;
        // 每次统计在他后面比他大的
        // 再加上他前面的i-1个数即为当前i的成本,取最小值
        for(int i = 1; i <= n; i++){
            int cnt = 0;
            for(int j = i + 1; j <= n; j++)
            if(a[j] > a[i]){
                cnt++;
            }
            ans = min(ans, cnt + i - 1);
        }
        cout << ans << "\n";
    }
    signed main() {
        ios::sync_with_stdio(0);
        cin.tie(0),cout.tie(0);
        int T = 1;
        cin >> T;
        while (T--)
            solve();
        return 0;
    }
    
    • 0
      @ 2025-12-7 12:24:55
      #include <stdio.h>
      int main(){
          int t;
          scanf("%d",&t);
          while(t--){
              long long n,a[2001],i=0,ans=1e18;
              scanf("%lld",&n);
              while(i<n){
                  scanf("%lld",&a[i]);
                  i++;
              }
              for(i=0;i<n;i++){
                  int cnt=0;
                  for(int j=i+1;j<n;j++){
                      if(a[i]<a[j])cnt++;
                  }
                  if(ans>cnt+i)ans=cnt+i;
              }
              printf("%lld\n",ans);
          }
      }
      
      • 1

      信息

      ID
      1182
      时间
      1000ms
      内存
      256MiB
      难度
      4
      标签
      (无)
      递交数
      78
      已通过
      37
      上传者