[Codeforces] #421 (Div. 2)

題目連結:http://codeforces.com/contest/820

#A
就根據提意模擬就好
#include <iostream>
using namespace std;
int main(){
    int c,v0,v1,a,l;cin >> c >> v0 >> v1 >> a >> l;
    int ans = 1,t = 0,s = v0;
    t += s;
    while(t < c){
        t-=l;
        if(s+a > v1)s = v1;
        else s+=a;
        t += s;
        ans++;
    }
    cout << ans <<endl;
    return 0;
}
#B
題目有點難懂,我們只要固定1,2然後枚舉剩下的點即可。
#include <iostream>
using namespace std;
double abs(double x){return x < 0 ? -x : x;}
int main(){
    double n,a;cin >> n >> a;
    double h = (n-2)*180/n;
    double m = h/(n-2);
    double ans = 3;
    double k = abs(h - a);
    for(int i = 4; i <= n ; i++){
        h -= m;
        if(abs(h - a) < k){
             k = abs(h - a);
             ans = i;
        }
    }
    cout << "1 2 " << ans << endl;
    return 0;
}
#C
待編輯

 #D
先預處理a[i]在 i 的左邊還是右邊,往右滑時總合會加在左邊的個數以及減在右邊的個數,然後再特判最後一位到第一位即可。
#include <iostream>
using namespace std;
int a[1000005],p[1000005];
int main(){
    int n;cin >> n;
    long long l = 0,tmp = 0;
    for(int i = 1 ; i <= n ; i++){
        cin >> a[i];
        tmp += abs(a[i]-i);
        a[i] - i > 0 ? p[a[i]-i]++:l++;
    }
    long long ans = tmp,id = 0;
    for(int i = 1; i < n; i++){
        l--;
        if((a[n-i+1] - n-i+1) > 0) p[a[n-i+1]-n-i+1]--;
        p[min(a[n-i+1]+i-1,1000000)]++;
        tmp -= (n-l-1);
        tmp += l;
        tmp -= abs(a[n-i+1]-n);
        tmp += abs(a[n-i+1]-1);
        l += p[i];
        if(tmp < ans)ans = tmp,id = i;
    }
    cout << ans << ' ' << id << endl;
    return 0;
}
#E
待編輯
(我據廢QQ)





留言

這個網誌中的熱門文章

[TIOJ]1617. [Interactive] 中位數

[TIOJ]1337. 隕石

[TIOJ]1994. 冰塊線