日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

paper plane fly away

發布時間:2024/5/15 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 paper plane fly away 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

鏈接:https://ac.nowcoder.com/acm/contest/903/E
來源:牛客網

There are n boys, indexed from 1 to n, and n girls indexed from n+1 to 2n.
One day, they have a party together. The girls are seated in the first row, and the boys sit in the second row. They take their seat in such a way, that a boy sit just behind a girl, and boy indexed 1 sit on the leftmost chair, 2 sit on the second, etc.
Each boy has a girl he likes,and he may make contact to his sweetheart, by writing down what he wants to tell her on a piece of paper, and makes it a paper plane, which he will throw directly at her. You may assume that the plane will go in a straight line.
But, the trouble occurs, when two paper plane collide in the air and drop halfway. Obviously, this will be extremely awkward. So each boy wants to know, if he throws his paper plane, how many paper planes have the potential to collide with it halfway.
It’s guaranteed that each girl is the sweetheart of exactly one boy.
輸入描述:
The first line contains a single integer n.
Then n lines follow, the i-th of which contains two integers, the index of the girl seated in front of the i-th boy and the girl he likes.
1

n

10
5
1≤n≤105
輸出描述:
Output n lines, the i-th of which contains one integer, the number of planes that may collide with i-th boy’s plane.
示例1
輸入
復制
5
7 9
6 6
8 10
9 7
10 8
輸出
復制
3
2
2
3
2
這個題,我在場時的思路是把喜歡的人的連用圖來存起來,然后之后沒飛一個飛機就行和列去掃描,,,,思路比較清晰,但是實在是太慢了

以下是題解:

用樹狀數組做,快速求得前i項和,正反插入兩次,求得逆序對的數量和
參考了一位大佬寫的代碼

#include<bits/stdc++.h> using namespace std; const int MAX = 1e6+5; int n,a[MAX],b[MAX],c[MAX],l[MAX],count1[MAX],count2[MAX]; int lowbit(int x){return x&(-x); } int sum(int x){int ans=0;for(;x>0;x-=lowbit(x)) ans+=c[x];return ans; } void add(int x,int count){for(;x<=n;x+=lowbit(x)) c[x]+=count; } void solve(){scanf("%d",&n);for(int i=1;i<=n;++i){int num1,num2;scanf("%d%d",&num1,&num2);a[i]=num2,b[num1]=i;}for(int i=1;i<=n;++i) l[i]=b[a[i]];memset(c,0,sizeof(c));for(int i=1;i<=n;++i){count1[i]=sum(n)-sum(l[i]);add(l[i],1);}memset(c,0,sizeof(c));for(int i=n;i>=1;--i){count2[i]=sum(l[i]-1);add(l[i],1);}for(int i=1;i<=n;++i) printf("%d\n",count1[i]+count2[i]); } int main(void) {solve();return 0; }

總結

以上是生活随笔為你收集整理的paper plane fly away的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。