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 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

Input

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

Output

Minimum reverse number you can get after K exchanging operations.

Sample Input

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

Sample Output

Case #1: 2
Case #2: 5
 先用树状数组求出逆序数。因为每一次交换可以增加或着减少一对逆序数,假设有m对逆序数,我们交换n对,那么这n对我们让他每次都减少一对逆序数,交换n次后 还有m - n对
逆序。注意题目中k的取值,如果求出的逆序数大于k,那么可以直接得出结果 res - k ,如果小于k,此时就要注意数字串中是否有重复的,如果没有那么当交换res - k次后
此时逆序数恰好为0, 剩余交换次数为k - res,如果k - res为偶数,那么我们可以重复交换同一对此时逆序数还为0,如果为奇数,此时只能结果为1。  如果字串中有重复的
那么可以交换那两个重复的数,此时无论是奇数还是偶数,结果并不影响最小逆序对总数。

/*=============================================================================
#
#      Author: liangshu - cbam
#
#      QQ : 756029571
#
#      School : 哈尔滨理工大学
#
#      Last modified: 2015-08-30 22:32
#
#     Filename: A.cpp
#
#     Description:
#        The people who are crazy enough to think they can change the world, are the ones who do !
=============================================================================*/
#
#include<iostream>
#include<sstream>
#include<algorithm>
#include<cstdio>
#include<string.h>
#include<cctype>
#include<string>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
using namespace std;
#define maxn 100010
struct node
{
    int v,id;
} s[maxn];
int c[maxn],n;
typedef long long ll;
ll res;

bool cmp(node x,node y)
{
    return ((x.v>y.v) || ((x.v==y.v)&&(x.id>y.id)));
}

int Lowbit(int x)
{
    return x&(x^(x-1));
}
ll Getsum(int pos)
{
    ll ret = 0LL;
    while(pos>0)
    {
        ret+=c[pos];
        pos -= Lowbit(pos);
    }
    return ret;
}
ll update(int pos)
{
    while(pos<=n)
    {
        c[pos]++;
        pos+=Lowbit(pos);
    }
}

int main()
{
    int k,x;
    int cs = 1;
    while(scanf("%d%d",&n,&k)!=EOF)
    {
        set<int>cnt;
        memset(c,0,sizeof(c));
        res = 0;
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&s[i].v);
            cnt.insert(s[i].v);
            s[i].id = i;
        }
        sort(s+1,s+n+1,cmp);
        for(int i=1; i<=n; i++)
        {
            res += Getsum(s[i].id);
            update(s[i].id);
        }
        if((res-k)>=0)
            printf("Case #%d: %lld\n",cs ++,res-k);
        else
        {
            if(cnt.size() < n)
            {
                printf("Case #%d: 0\n",cs ++);
            }
            else
            {
                if(abs(res-k)%2) printf("Case #%d: 1\n",cs ++);
                else
                    printf("Case #%d: 0\n",cs ++);
            }
        }
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-05 12:23:36

HUST 1343 Reverse Number(哈理工 亚洲区选拔赛前练习赛)的相关文章

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

C - Coil Judge Time Limit:1000MS    Memory Limit:131072KB    64bit IO Format:%lld & %llu SubmitStatusPracticeHUST 1343 Description Mortal Coil is a very popular game in the past few months. The game has a rectangular maze with obstacles. You guide a

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

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

HUST 1341 A - A Simple Task(哈理工 亚洲区选拔赛练习赛)

A - A Simple Task Time Limit:1000MS    Memory Limit:131072KB    64bit IO Format:%lld & %llu SubmitStatusPracticeHUST 1341 Description As is known to all, lots of birds are living in HUST. A bird has s units of food on the first day, and eats k units

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