1. 程式人生 > 其它 >二路歸併 ( 包含四捨五入問題)

二路歸併 ( 包含四捨五入問題)

滑雪

瞎寫的

#include<iostream>
#include<algorithm>

using namespace std;

const int N = 10010;

double a[N],b[N];
int lena,lenb;
int n;
double dis;
double t = 0,v = 1;
double now = 1;

int main(){
    cin >> n;

    for(int i = 0;i < n;i ++){
        string op;
        cin >> op;

        if(op == "T") cin >> a[lena ++];
        else cin >> b[lenb ++];            
    }

    sort(a,a + lena),sort(b,b + lenb);

    int sta = 0,stb = 0;
    while(sta < lena || stb < lenb){
        double disa = (a[sta] - t) * v;
        if(sta >= lena) disa = 0x3f3f3f3f;
        
        if(sta < lena && stb < lenb && disa + dis == b[stb]){
            t = a[sta ++];
            dis = b[stb ++];
            v = 1 / (++ now);
            continue;
        }

        if(dis + disa > b[stb] && stb < lenb){
            t += (b[stb] - dis) / v;
            dis = b[stb ++];
        }else{
            t = a[sta ++];
            dis += disa;
        }

        v = 1 / (++ now);
    }

    if(dis < 1000) t += (1000 - dis) / v;
    cout << (int) (t + 0.5001);          //小數四捨五入小技巧
}