2014-Summer-PK4(Team)

1008

Problem Description

Bob is a game programming specialist. In his new car race game, there are some racers(n means the amount of racers (1<=n<=100000)) racers star from someplace(xi means Starting point coordinate),and they possible have different speed(V means speed).so it possibly takes place to overtake(include staring the same point ). now he want to calculate the maximal amount of overtaking.

Input

The first line of the input contains an integer n-determining the number of racers.Next n lines follow, each line contains two integer Xi and Vi.(xi means the ith racer's Starting point coordinate, Vi means the ith racer's speed.0<Xi, Vi<1000000).

Output

For each data set in the input print on a separate line, on the standard output, the integer that represents the maximal amount of overtaking.

Sample Input

2
2 1
2 2
5
2 6
9 4
3 1
4 9
9 1
7
5 5
6 10
5 6
3 10
9 10
9 5
2 2

Sample Output

1
6
7
a超b满足条件:ax<bx,av>bv。将结构体按照x从小到大的顺序排序,则此节点前的节点的速度若比此节点速度小则此节点不会超过该车(注意断句。。),此时该车超过的数量
等于i-sum(node[i].v)
#include "stdio.h"
#include "string.h"
#include "algorithm"
#define MAX 100010
using namespace std;
long long s[MAX*10];
struct node
{
    long long  x,v;
}node[MAX];
int cmp(struct node a,struct node b)
{
    if(a.x==b.x)
            return a.v>b.v;
     return a.x<b.x;
}
long long lowbit(long long x)
{
    return x&(-x);
}
long long ss(long long p)
{
    long long ans=0;
    while(p)
    {
         ans+=s[p];
         p-=lowbit(p);
    }
    return ans;
}
void add(long long p,long long del)
{
      while(p<=1000000)
      {
           s[p]+=del;
           p+=lowbit(p);
      }
}
int main()
{
    long long sum;
    long long  n,i;
    while(scanf("%I64d",&n)==1)
    {
          sum=0;
          for(i=0;i<n;i++)
                  scanf("%I64d%I64d",&node[i].x,&node[i].v);
          sort(node,node+n,cmp);
          memset(s,0,sizeof(s));
          for(i=0;i<n;i++)
                   {sum=sum+i-ss(node[i].v);
                     add(node[i].v,1);
                   }
        printf("%I64d\n",sum);
    }
    return 0;
}
1006

Problem Description

There are N(N<=1000) villages along a straight road, numbered from 1 to N for simplicity. We know exactly the position of every one (noted pos[i],pos[i] is positive integer and pos[i]<=10^8). The local authority wants to build a post office for the people living in the range i to j(inclusive). He wants to make the sum of |pos[k]-position_of_postoffice| (i<=k<=j) is minimum.

Input

For each test case, the first line is n. Then n integer, representing the position of every village and in acending order. Then a integer q (q<=200000), representing the queries. Following q lines, every line consists of two integers i and j. the input file is end with EOF. Total number of test case is no more than 10.
Be careful, the position of two villages may be the same

Output

For every query of each test case, you tell the minimum sum.

Sample Input

3
1 2 3
2
1 3
2 3
0

Sample Output

2
1
很显然,站点位置为中点。
分奇偶情况后发现w[i][j]=w[i][j-1]+(pos[j]-pos[(i+j)>>1])

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值