G
Swaps and Inversions
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1486 Accepted Submission(s): 539
Problem Description
Long long ago, there was an integer sequence a.
Tonyfang think this sequence is messy, so he will count the number of inversions in this sequence. Because he is angry, you will have to pay x yuan for every inversion in the sequence.
You don't want to pay too much, so you can try to play some tricks before he sees this sequence. You can pay y yuan to swap any two adjacent elements.
What is the minimum amount of money you need to spend?
The definition of inversion in this problem is pair (i,j) which 1≤i<j≤n and ai>aj.
Input
There are multiple test cases, please read till the end of input file.
For each test, in the first line, three integers, n,x,y, n represents the length of the sequence.
In the second line, n integers separated by spaces, representing the orginal sequence a.
1≤n,x,y≤100000, numbers in the sequence are in [−109,109]. There're 10 test cases.
Output
For every test case, a single integer representing minimum money to pay.
Sample Input
3 233 666 1 2 3 3 1 666 3 2 1
Sample Output
0 3
Source
2018 Multi-University Training Contest 2
思路:可以想到线段树来维护区间,但如何每个数加到b[i]就会所在区间贡献加一,那么我们自然可以想到区间最小值来减少操作,当我们每次增加区间的值时,我们可以让这段区间最小值减一。当区间最小值减少到零时,我们就找最小值所在的位置,然后给他重新赋值为b[i],并让他所在区间的sum值加一。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+10;
struct node{
int mi,add,sum;
}a[N*4];
int n,m,b[N];
void pushup(int rt) //更新mi和sum
{
a[rt].mi = min(a[rt<<1].mi, a[rt<<1|1].mi);
a[rt].sum = a[rt<<1].sum + a[rt<<1|1].sum;
}
void pushdown(int rt) //更新add标记
{
a[rt<<1].add+=a[rt].add;
a[rt<<1|1].add+=a[rt].add;
a[rt<<1].mi+=a[rt].add;
a[rt<<1|1].mi+=a[rt].add;
a[rt].add=0; //多组输入,这步不能省。
}
void build(int l,int r,int rt)
{
if(l==r)
{
a[rt].sum=0;a[rt].add=0;a[rt].mi=b[l];return ;
}
int mid=(l+r)>>1;
build(l,mid,rt<<1);
build(mid+1,r,rt<<1|1);
pushup(rt);
a[rt].add=0;
}
void update(int L,int R,int l,int r,int rt,int k)
{
if(L<=l&&r<=R)
{
if(k)
{
a[rt].add--;
a[rt].mi--;
}
if(a[rt].mi>0) return ;
if(l==r)
{
if(a[rt].mi==0)
{
a[rt].sum++;a[rt].mi=b[l];
}
return ;
}
k=0;
}
pushdown(rt);
int mid=(l+r)>>1;
if(L<=mid) update(L,R,l,mid,rt<<1,k);
if(R>mid) update(L,R,mid+1,r,rt<<1|1,k);
pushup(rt);
}
int query(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R) return a[rt].sum;
int mid=(l+r)>>1,ans=0;
if(L<=mid) ans+=query(L,R,l,mid,rt<<1);
if(R>mid) ans+=query(L,R,mid+1,r,rt<<1|1);
return ans;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=1;i<=n;i++)scanf("%d",&b[i]);
build(1,n,1);
while(m--)
{
char s[10];
int x,y;
scanf("%s %d %d",s,&x,&y);
if(s[0]=='q')
{
printf("%d\n",query(x,y,1,n,1));
}
else
{
update(x,y,1,n,1,1);
}
}
}
return 0;
}
Swaps and Inversions
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1513 Accepted Submission(s): 553
Problem Description
Long long ago, there was an integer sequence a.
Tonyfang think this sequence is messy, so he will count the number of inversions in this sequence. Because he is angry, you will have to pay x yuan for every inversion in the sequence.
You don't want to pay too much, so you can try to play some tricks before he sees this sequence. You can pay y yuan to swap any two adjacent elements.
What is the minimum amount of money you need to spend?
The definition of inversion in this problem is pair (i,j) which 1≤i<j≤n and ai>aj.
Input
There are multiple test cases, please read till the end of input file.
For each test, in the first line, three integers, n,x,y, n represents the length of the sequence.
In the second line, n integers separated by spaces, representing the orginal sequence a.
1≤n,x,y≤100000, numbers in the sequence are in [−109,109]. There're 10 test cases.
Output
For every test case, a single integer representing minimum money to pay.
Sample Input
3 233 666 1 2 3 3 1 666 3 2 1
Sample Output
0 3
其实就是求逆序数然后乘上min(x,y)。这里用树状数组求得逆序数。
树状数组求逆序数真是妙啊。
大概就是按照给的顺序插入到一个序列,边插入边找比当前元素大的数的个数。
因为数据太大需要离散化,重新编号,注意相同的数字编号相同。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll lowbit(ll x)
{
return x&-x;
}
ll min(ll x,ll y)
{
if(x<y) return x;return y;
}
ll n;
ll a[1000006];
ll num=0;
ll add(ll x,ll k)
{
while(x<=n)
{
a[x]+=k;
x+=lowbit(x);
}
}
ll Sum(ll x)
{
ll sum=0;
while(x>0)
{
sum+=a[x];
x-=lowbit(x);
}
return sum;
}
struct node{
ll pos,val;
}b[1000006];
ll cmp(node q,node w)
{
return q.val<w.val;
}
ll l[1000006];
int main()
{
ll x,y;
while(~scanf("%lld %lld %lld",&n,&x,&y))
{
memset(a,0,sizeof(a));
memset(l,0,sizeof(l));
num=0;
for(ll i=1;i<=n;i++)
{
ll p;
scanf("%lld",&b[i].val);b[i].pos=i;
}
sort(b+1,b+n+1,cmp);
int cnt=0;
for(ll i=1;i<=n;i++)
{
if(b[i].val!=b[i-1].val)
l[b[i].pos]=++cnt;
else l[b[i].pos]=cnt;
}
for(ll i=1;i<=n;i++)
{
num+=(i-Sum(l[i])-1); //统计当前序列大于l[i]的元素。
add(l[i],1);
}
printf("%lld\n",num*min(x,y));
}
return 0;
}