2017武汉大学校赛网络预选赛g题

Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 512 mebibytes
Everyone hates the verdict “Time Limit Exceeded” or “TLE”. However, sometimes, we can’t come up with a “elegant” solution which is clear and fast.

Our problem setter also hates “TLE”, but he is dumb and can only work out what a problem is. He can’t really solve it. We called him “Zui Qiang Xuan Shou”

One day, he got a problem, again, he said “I got it! It is **”, he spoke so fast that no one knew what he said. Luckily, he typed his solution—

这里写图片描述
He said that it should get AC small cases, but he might receive “Time Limit Exceeded”.

As he clicked the “submit” button, the network is down. So he went to the dinning hall and ask you to correct his solution.

Your program should output exact same things as the program listed above.

Input
The format of input should make the program work if we don’t care about the time.

In the input file, all numbers are positive integers and no more than 10^610
​6
​​ .

Output
You should output what the program outputs if the time is unlimited.

Examples
Input 1
5
1 2 3 4 5
Output 1
0
Input 2
6
1 2 3 4 5 6
Output 2
0

题意:让你写一个程序,输入和输出跟上面一样。

解题思路:很明显上面就是一个冒泡排序的代码,答案就是这个数组的逆序对数目.那么求逆序对还不容易,树状数组就行,而且这题不需要离散化,很简单,上次听队友说求逆序数还可以用归并排序,方法很多,这里用树状数组的复杂度是n*logn,所有跑起来很快。

#include<stdio.h>
#include<iostream>
#include<cstring>
using namespace std;
const int maxn = 1e6 + 10;
typedef long long ll;
int n;
ll a[maxn];
ll Tree[2*maxn];
ll lowbit(ll x)
{
    return x&(-x);
}
ll get(ll x)
{
    ll sum = 0;
    for(ll i = x; i > 0; i -= lowbit(i))
    {
        sum += Tree[i];
    }
    return sum;
}
void update(ll x,ll value)
{
    for(ll i = x; i <= maxn; i += lowbit(i))
    {
        Tree[i] += value;
    }
}
int main()
{
    scanf("%d",&n);
    memset(Tree,0,sizeof(Tree));
    for(ll i = 1; i <= n; i++)
    {
        scanf("%lld",&a[i]);
    }
    ll ans = 0;
    for(int i = 1; i <= n; i++)
    {
        ll res = get(a[i]);
        ans += i - 1 - res;
        update(a[i],1);
    }
    printf("%lld\n",ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用:这是关于统计数字1出现次数的问目要求找出一个数,使得从1到这个数之间的所有整数中1出现的次数正好等于2021次。可以通过遍历数字1到一个稍大的数,统计每个数中1的个数,直到满足条件为止。具体代码如下: count = 0 ans = '' for i in range(10000): tmp = str(i) count = count + tmp.count('1') if count == 2021: print(i) break if count > 2021: print(i-1) break 引用:这的难度级别是简单,主要考察了input函数、in运算、条件分支语句和print函数的使用。首先,input函数用于接受标准输入数据,并返回为字符串类型。其次,in运算可用于判断某个字符或者字符串是否存在于另一个字符串中。然后,条件分支语句if...else用于根据条件的真假执行不同的代码分支。最后,print函数用于打印输出结果。在解过程中,需要掌握字符串操作和条件判断语句的使用。 引用:这是关于直线斜率的计算问目给出了一组点的坐标,需要计算通过这些点的直线斜率有多少种不同的情况。可以通过遍历所有的点对,计算它们之间的斜率,并将斜率和截距作为一个元组加入到一个集合中,最后返回集合的长度即可。具体代码如下: X = [] Y = [] for i in range(20): X.append(i) for i in range(21): Y.append(i) ans = set() def calculate_slope(x1, x2, y1, y2): if x1 == x2: return tmp = ((y1-y2)/(x1-x2)) b = 0.5*(y1 + y2) - 0.5*tmp*(x1 + x2) b = round(b, 4) ans.add((tmp, b)) for i in range(20): for j in range(21): for x in range(20): for y in range(21): calculate_slope(i, x, j, y) print(len(ans))

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值