牛客网----Wannafly挑战赛12

https://www.nowcoder.com/acm/contest/79#question

A题,简单的递归

https://www.nowcoder.com/acm/contest/79/A

#include <cstring>
#include <queue>
#include <cstdio>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <iostream>
#include <cmath>
using namespace std;
template <typename cinType>
void getArr ( cinType *a, int n ) {
    for ( int i = 0; i < n; i++ )
        cin >> a[i];
}
template <typename coutType>
void putArr ( coutType *a, int n ) {
    for ( int i = 0; i < n; i++ )
        if ( i == n - 1 ) cout << a[i] << endl;
        else cout << a[i] << ' ';
}
template <typename reSetType>
void reSet ( reSetType *a, int n, reSetType val ) {
    for ( int i = 0; i < n; i++ )
        a[i] = val;
}
const int year[4] = {1, 2, 3, 5};
double x[4];
double func1 ( int n ) {
    if ( n < 1 ) return 0;
    for ( int i = 0; i < 4; i++ )
        if ( n == year[i] )
            return pow ( 1 + x[i], year[i] );
    double ans = 1;
    for ( int i = 0; i < 4 ; i++ )
        ans = max ( ans, pow ( 1 + x[i], year[i] ) * func1 ( n - year[i] ) );
    return ans;
}
int main() {
    std::ios::sync_with_stdio ( false );
    cin.tie ( 0 );
    int n;
    while ( cin >> n ) {
        getArr ( x, 4 );
        cout << func1 ( n ) << endl;
    }
    return 0;
}

B题,要注意时间问题,要做好优化

https://www.nowcoder.com/acm/contest/79/B

#include <cstring>
#include <queue>
#include <cstdio>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <iostream>
#include <cmath>
using namespace std;
template <typename cinType>
void getArr ( cinType *a, int n ) {
    for ( int i = 0; i < n; i++ )
        cin >> a[i];
}
template <typename coutType>
void putArr ( coutType *a, int n ) {
    for ( int i = 0; i < n; i++ )
        if ( i == n - 1 ) cout << a[i] << endl;
        else cout << a[i] << ' ';
}
template <typename reSetType>
void reSet ( reSetType *a, int n, reSetType val ) {
    for ( int i = 0; i < n; i++ )
        a[i] = val;
}
int main() {
    std::ios::sync_with_stdio ( false );
    cin.tie ( 0 );
    int n, m;
    while ( cin >> n >> m ) {
        int temp, temp1 = n, temp2 = n;
        priority_queue<int, vector<int>, greater<int> >reduce;
        priority_queue<int, vector<int>, less<int> >add;
        while ( temp1-- ) {
            cin >> temp;
            reduce.push ( temp );
        }
        while ( temp2-- ) {
            cin >> temp;
            add.push ( temp );
        }
        long long ans = 0, com = 0;
        for ( int flag = 0, i = 1; i <= n; i++ ) {
            com += add.top() - reduce.top() ;
            reduce.pop();
            add.pop();
            if ( ( ++flag ) == 3 ) {
                flag = 0;
                com += m;
            }
            ans = max ( ans, com );
            if ( flag == 0 && ans != com ) break;
        }
        cout << ans << endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值