R - is the order a rabbit (末尾不能有空格!!,螞蟻爬動問題)
阿新 • • 發佈:2022-05-06
In a remote place, there is a rabbits market where rabbits can be bought and sold in every day. The trading rules in this market are as follows: 1.The market is available only in morning and afternoon in one day. And the rabbit transaction price may be different every day even different between morning and afternoon inView problema day. 2.It is prohibited that purchasing more than once in a day. And only one rabbit can be purchased at a time. 3. You can only sell once in a day, but you can sell rabbits unlimited quantities (provided that you have so many rabbits) Mr.Cocktail has traded in this market for N days, suppose he has unlimited money to buy rabbits. Before the first day and after the last day, he has no rabbits. Now, inthese N days, how much money did he earn the most? Input The first line contains a positive integer N, which means there are N days in total (1 \leq n \leq 10^5)(1≤n≤10 5 ) Next, there are N lines, each line contains two positive integers, representing the rabbit price in the morning and afternoon of the day a_i,b_i(1\leq a_i ,b_i \leq 10^9)a i ,b i (1≤a i ,b i ≤10 9 ). Output Output an integer on a line to indicate the answer. Sample 1 Inputcopy Outputcopy 3 1 6 2 3 7 1 11 Sample 2 Inputcopy Outputcopy 2 5 4 3 2 0 Note In example 1, a rabbit was purchased for 1 in the morning of the first day, and a rabbit was purchased for1inthemorningofthefirstday,andarabbitwaspurchasedfor2 in the afternoon of the second day. On the morning of the third day, the two rabbits were sold, a total profit of $11. The price of the rabbit in sample 2 continues to decrease, and it is impossible to make money through buying and selling, so choose not to make any buying and selling, and output the answer 0.
swjtu—春季集訓 - Virtual Judge (vjudge.net)
思路:
- 就是拿到螞蟻爬的問題,碰撞相互變向,我們思考就讓他不變向,按著原來方向走就行了,
- 這樣 k秒過後,就是 答案的位置,
- 如何確定每一個位置具體是那個呢?
- 按照 原來的位置進行排序,因為不允許相互穿過去,所以k秒後,原來的排序大小還是不變
#include <bits/stdc++.h> using namespace std; #define ri register int #define M 100055 template <class G >void read(G &x) { x=0;int f=0;char ch=getchar(); while(ch<'0'||ch>'9'){f|=ch=='-';ch=getchar();} while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();} x=f?-x:x; return ; } struct dian{ int val,id; int v; bool operator <(const dian &t)const { return val<t.val; } }p[M]; int n,m; int num[M]; long long ans[M]; int main(){ read(n);read(m); for(ri i=1;i<=n;i++) { read(p[i].val);read(p[i].v); p[i].id=i; } sort(p+1,p+1+n); for(ri i=1;i<=n;i++) { num[i]=p[i].id; } for(ri i=1;i<=n;i++) { p[i].val+=p[i].v*m; } sort(p+1,p+1+n); for(ri i=1;i<=n;i++) { ans[num[i]]=p[i].val; } for(ri i=1;i<=n;i++) { if(i==1) printf("%lld",ans[i]); ////////////// ma de else printf(" %lld",ans[i]); } return 0; }View Code
後記:
- !!!媽的,第一次遇到oj上不能允許某位有空格!!!
- 這個錯找了 25min !!氣死了