hust 1347 - Reverse Number

题目描述

Given a non-negative integer sequence A with length N, you can exchange two adjacent numbers each time. After K exchanging operations, what’s the minimum reverse number the sequence can achieve? The reverse number of a sequence is the number of pairs (i, j) such that i < j and Ai > Aj

入There are multiple cases. For each case, first line contains two numbers: N, K 2<=N<=100000, 0 <= K < 2^60 Second line contains N non-negative numbers, each of which not greater than 2^30

输出

Minimum reverse number you can get after K exchanging operations.

样例输入

3 1
3 2 1
5 2
5 1 4 3 2

样例输出

Case #1: 2
Case #2: 5

本题当然是先求逆序对,归并排序,树状数组,线段树,什么都可以,主要是先求出来,做k次变化,我们的目标不就是找刚好是逆序的变换,这样再看看k是否大于逆序对数的情况,不就完了
#include<map>
#include<set>
#include<queue>
#include<cmath>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define  inf 0x0f0f0f0f

using namespace std;
const int maxn=100000+10;

int a[maxn],b[maxn],ans;

void merge_sort(int* A,int x,int y,int*T)
{
     if (y-x>1)
     {
          int m=x+(y-x)/2;
          int p=x, q=m, i=x;
          merge_sort(A,x,m,T);
          merge_sort(A,m,y,T);
          while (p<m || q<y)
          {
               if (q>=y || (p<m && A[p]<=A[q])) T[i++]=A[p++];
               else {T[i++]=A[q++];ans+=m-p;}
          }
          for (int i=x;i<y;i++) A[i]=T[i];
     }
}

int main()
{
     int n,k,t=0;
     while(scanf("%d%d",&n,&k)!=EOF)
     {
          t++;
          for (int i=0;i<n;i++) scanf("%d",&a[i]);
          ans=0;
          merge_sort(a,0,n,b);
          ans-=k;
          bool cut=false;
          for (int i=0;i<n-1;i++) if (a[i]==a[i+1]) {cut=true;break;}
          if (ans<0)
          {
               if ((-ans)%2==0) ans=0;
               else
               {
                    if(cut) ans=0;
                    else ans=1;
               }
          }
          printf("Case #%d: %d\n",t,ans);
     }
     return 0;
}

作者 chensunrise

hust 1347 - Reverse Number,布布扣,bubuko.com

时间: 2024-11-05 06:15:37

hust 1347 - Reverse Number的相关文章

HUST 1343 Reverse Number(哈理工 亚洲区选拔赛前练习赛)

G - Reverse Number Time Limit:1000MS    Memory Limit:131072KB    64bit IO Format:%lld & %llu SubmitStatusPracticeHUST 1347 Description Given a non-negative integer sequence A with length N, you can exchange two adjacent numbers each time. After K exc

HDU1266 Reverse Number

问题链接:HDU1266 Reverse Number.基础训练级的题,用C语言编写. 简单的问题,就不说了. AC通过的C语言程序如下: /* HDU1266 Reverse Number */ #include <stdio.h> #include <string.h> #define MAXN 16 int main(void) { int t, start, end; char s[MAXN], c; scanf("%d", &t); while

HDU 1266 Reverse Number(字符串逆转 水题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1266 Problem Description Welcome to 2006'4 computer college programming contest! Specially, I give my best regards to all freshmen! You are the future of HDU ACM! And now, I must tell you that ACM proble

杭电 HDU 1266 Reverse Number

Reverse Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5763    Accepted Submission(s): 2643 Problem Description Welcome to 2006'4 computer college programming contest! Specially, I give

LeetCode #Reverse Number#

刚背了单词,然后做个题玩玩-挑个软柿子踩踩-哈哈 很简单的思路.不过好玩的是我忘记检查处理完的数据是否符合整形数据返回了.因而好一会儿不能AC. 感谢 @Fantasy. 很快的指出我没有检查返回数据的范围. 先给出我超丑陋的解(python), 而后给出其他高手给出的很优雅的解!!也是用python 最后会给出利用java和C/C++的解. """ Programmer : EOF Date : 2015.03.31 File : reverse_interger.py &

reverse number

class Solution {public: int reverse(int x) { bool negative_flag=false; if(x==INT_MIN) return 0; if(x<0) { x=-x; negative_flag=true; } long long result=0; while(x!=0) { result=result*10+x%10; x=x/10; } if(result>INT_MAX) return 0; if(negative_flag) r

Palindrome Number &amp;&amp; Reverse Number

Problem I: 判断数字是不是回文字符串形式 Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note th

Leetcode——Reverse number

这个题其实逻辑不难,但是最坑爹的问题是如何判断整型数的溢出!!! class Solution  { public: int reverse(int x) { if (INT_MIN == x) return 0; if (x < 0) return -reverse(-x); int result = 0; while (x > 0){ int a = x % 10; x = x / 10; if ((INT_MAX - a) / 10 < result)    //判断溢出 retu

【leetcode】1347. Minimum Number of Steps to Make Two Strings Anagram

题目如下: Given two equal-size strings s and t. In one step you can choose any character of t and replace it with another character. Return the minimum number of steps to make t an anagram of s. An Anagram of a string is a string that contains the same c