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)1006#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; }
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 sameOutput
For every query of each test case, you tell the minimum sum.Sample Input
3 1 2 3 2 1 3 2 3 0Sample Output
很显然,站点位置为中点。2 1
分奇偶情况后发现w[i][j]=w[i][j-1]+(pos[j]-pos[(i+j)>>1])