16.7.26

本文精选了多道算法题目并提供了解题思路与代码实现,包括队列问题、数位操作、图形绘制、时间安排等典型场景,通过实例帮助读者理解和掌握相关算法技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//今日代码
2016-07-26 training

A. Queue on Bus Stop

(有n群人排队坐车,车的最多载客数为m,每群人要一起上车,坐不下的话一起等下一趟车。做法是把上车人数加一下,加到不能加则清空,ans++)

#include<iostream> 
#include<cstdio>
#include<string>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<map>
#define LL long long 
#define INF 0x1f1f1f1f
using namespace std;

int arr[110];

int main()
{
    freopen("xx.in","r",stdin);
    freopen("xx.out","w",stdout);

    int n,m;
    int sum= 0;
    cin >> n >> m;
    for(int i = 0; i < n ; i++)
        cin >> arr[i];

    int ans = 1;
    for(int i = 0; i <n ; i++ )
    {
        if(sum+arr[i] <= m) sum+=arr[i];
        else 
        {
            sum = arr[i];
            ans++;
        }
    }

    cout << ans << endl;

    return 0;
}

B. Pasha Maximizes

(一个没有前导零的数,每次交换两个相邻位置上的数字,最多交换k次,问能构成的最大数。做法是贪心,用字符串读入这个大数之后,从最高位开始,对于每个位置上的数判断一下再此之后的k个是否有比它大的,若有,找到最大的那个,然后把这个位置的数一直交换到该位上。)

#include<iostream> 
#include<cstdio>
#include<string>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<map>
#define LL long long 
#define INF 0x1f1f1f1f
using namespace std;


int main()
{
    freopen("xx.in","r",stdin);
    freopen("xx.out","w",stdout);

    string s;
    int k;
    cin >> s >> k;

    for(int i = 0; i <s.length(); i++)
    {
        int t = i;
        for(int j = i+1; j < s.length() && j-i <=k; j++)
            if(s[j] > s[t]) t= j;

        k-=(t-i);
        while(t!=i)
        {
            swap(s[t],s[t-1]);
            t--;
        }
    }


    cout << s << endl;

    return 0;
}

C. Cardiogram

(画出一组n个的ai条线段符号构成的心电图。构造一个字符型的空表,读入的 i 个若是奇数则为下降符号\,若为偶数为上升符号/,从开出的数组中间最左开始起画,每次把一整条画完之后更新一下新的最高点up和最低点down,为方便输出。)

#include<iostream> 
#include<cstdio>
#include<string>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<map>
#define LL long long 
#define INF 0x1f1f1f1f
using namespace std;

char arr[2020][1010];

int main()
{
    freopen("xx.in","r",stdin);
    freopen("xx.out","w",stdout);

    int n;
    cin >> n;

    int r = 1002;
    int c = 0;
    int up = r,down = r;

    memset(arr,' ',sizeof(arr));

    for(int i =0; i < n; i++)
    {
        int t;
        cin >> t;
        for(int j = 0; j < t; j++)
        {
            if(i%2) 
            {
                arr[r][c] = '\\'; //转义字符 
                r++;
            }
            else
            {
                arr[r][c] = '/';
                r--;
            }
            c++;
        }

        if(i%2) r--;
        else r++;

        if(up > r) up = r;
        if(down < r) down = r;
    }

    for(int i = up; i <= down; i++)
    {
        for(int j = 0; j < c; j++)
            cout << arr[i][j];
        cout << endl;
    }

    return 0;
}

F. Devu, the Singer and Churu, the Joker

(在给定的表演时间内,歌手要唱完所有曲目,歌曲间歇10分钟。讲笑话5分钟,问最多能讲多少笑话。表演时间减去歌手时间和间歇时间,若小于零则-1,否则扣除歌手时间除5即为答案。)

#include<iostream> 
#include<cstdio>
#include<string>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<map>
#define LL long long 
#define INF 0x1f1f1f1f
using namespace std;

int arr[110];

int main()
{
    freopen("xx.in","r",stdin);
    freopen("xx.out","w",stdout);

    int sum = 0;
    int n,d;
    cin >> n >> d;
    for(int i = 0; i < n; i++)
    {
        cin >> arr[i];
        sum+=arr[i];
    }

    int ans = (n-1)*2;
    d = d-ans*5-sum;

    if(d < 0) 
    {
        cout << "-1"<< endl;
        return 0;
    }

    ans+=d/5;

    cout << ans << endl;

    return 0;
}

G. Devu, the Dumb Guy

(有n门科目,每门科目有一定章节数,学一个章节要花x小时,学下一门科目的时候时间减为x-1,直到减为1。问要学完全部科目的最少时间。显然章节数少的应当先学,所以sort一下对于n门科目每次加上时间乘以章节数就好。)

#include<iostream> 
#include<cstdio>
#include<string>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<map>
#define LL long long 
#define INF 0x1f1f1f1f
using namespace std;

LL arr[100010];

int main()
{
    freopen("xx.in","r",stdin);
    freopen("xx.out","w",stdout);

    int n,x;
    cin >> n >> x;
    for(int i = 0; i < n; i++)
        cin >> arr[i];

    sort(arr,arr+n);

    LL ans = 0;
    for(int i = 0; i < n; i++)
    {
        ans+=arr[i]*x;
        if(x>1) x-=1;
    }

    cout << ans << endl;

    return 0;
}

H. Devu and Partitioning of the Array

(有n个数要分成k堆,其中和为偶数的要为p堆。奇+奇=偶,偶+偶=偶,奇+偶=奇。奇数构成的可能性更小先考虑奇数。要满足偶数有p堆也就是奇数有k-p堆,如果这些数中奇数个数不满足k-p 或 除去k-p多出的奇数不是偶数个 或 偶数个数加上扣掉k-p的奇数个数除2不足p 都无法满足条件(:зゝ∠)。那么排除这种情况一定能满足条件。首先看p=k或p=0的情况,由于能满足条件,那么每个堆放一个偶数或奇数,其余全部堆在剩下一堆即可(由于偶数可能由奇数构成,所以在满足条件的情况下偶数可能不够多,这时候用两个奇数填就好)。如果不是这种特殊情况,每堆先按照上面那样填,最后剩一个偶数堆和一个奇数堆待填,只要把一个奇数放在奇数堆,剩余全部丢在偶数堆即可。)

#include<iostream> 
#include<cstdio>
#include<string>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<map>
#define LL long long 
#define INF 0x1f1f1f1f
using namespace std;

int a[100010];
int b[100010];

int main()
{
    freopen("xx.in","r",stdin);
    freopen("xx.out","w",stdout);

    int n, p, k;
    int odd = 0, even = 0;
    cin >> n >> k >>p ;
    for(int i = 0; i < n; i++)
    {
        int num;
        cin >> num;
        if(num%2 == 1) a[odd++] = num;
        else b[even++] = num;
    }

    if(odd < k-p || (odd - k + p) % 2 == 1 || even + (odd - k + p) / 2 < p)
        cout <<"NO"<< endl;

    else
    {
        cout <<"YES"<< endl;

        for(int i = 0; i < k-p-1; i++)
            cout <<"1 "<<a[--odd]<< endl;

        for(int i = 0; i < p - 1; i++)
        {
            if(even)
                cout <<"1 "<< b[--even]<< endl;
            else
            {
                cout <<"2 "<< a[odd-1]<<" "<<a[odd-2] << endl;
                odd -= 2;
            }
        }

        if(k-p && p) 
            cout <<"1 "<< a[--odd] << endl;

        cout << odd+even;
        while(odd) cout <<" "<< a[--odd];
        while(even) cout <<" "<< b[--even];
        cout <<endl; 
    }
    return 0;
}

I. Devu and his Brother

(a,b两个队列,要使得a队列中的最小值不小于b队列中的最大值,对于每次操作可以对任意一个队列的任意一个数加1或减1,问最小操作次数。能想到的两种操作不外乎把a中数值小的一直加,或把b中数值大的一直减。做法是把a按从小到大排,b按从大到小排,显然要改变的是较小个数的队列中的数,把前min(n,m)个对应的ab队列上的数做差相加即可。)

#include<iostream> 
#include<cstdio>
#include<string>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<map>
#define LL long long 
#define INF 0x1f1f1f1f
using namespace std;

LL a[100010];
LL b[100010];

bool compare(int x, int y)
{
    return x>y;
}

int main()
{
    freopen("xx.in","r",stdin);
    freopen("xx.out","w",stdout);

    int n,m;
    cin >> n >> m;
    for(int i = 0; i <n; i++)
        cin >> a[i];
    for(int i = 0; i < m; i++)
        cin >> b[i];

    sort(a,a+n);
    sort(b,b+m,compare);

    n = min(n,m);
    LL ans = 0;
    for(int i = 0; i < n; i++)
        if(b[i]>a[i]) ans+=(b[i]-a[i]);

    cout << ans << endl;

    return 0;
}
1985年1月至2005年12月,原油现货交易价格如下。数据为:26.41 26.73 28.29 27.63 27.84 26.87 27.12 28.08 29.08 30.38 29.75 26.3 18.83 13.26 10.42 13.34 14.3 12.78 11.15 15.9 14.77 15.27 15 17.94 18.75 16.6 18.83 18.73 19.38 20.29 21.37 19.73 19.59 19.96 18.51 16.7 16.94 16.01 17.08 17.99 17.51 15.16 16.31 15.18 13.37 13.58 15.32 17.24 17.03 18.15 20.19 20.42 19.9 20.27 18.31 18.83 20.13 19.94 19.89 21.82 22.68 21.54 20.28 18.54 17.4 17.07 20.69 27.32 39.51 35.23 28.85 28.44 21.54 19.16 19.63 20.96 21.13 20.56 21.68 22.26 22.23 23.37 21.48 19.12 18.9 18.68 19.44 20.85 22.11 21.6 21.87 21.48 21.71 20.62 19.89 19.5 20.26 20.6 20.44 20.53 20.02 18.85 17.88 18.29 18.79 16.92 15.43 14.17 15.19 14.48 14.79 16.9 18.31 19.37 20.3 17.56 18.39 18.19 18.05 17.76 18.39 18.49 19.17 20.38 18.89 17.4 17.56 17.84 17.54 17.64 18.18 19.55 17.74 19.54 21.47 21.2 19.76 20.92 20.42 22.25 24.38 23.35 23.75 25.92 24.15 20.3 20.41 20.21 20.88 19.8 20.14 19.61 21.18 21.08 19.15 17.64 17.21 15.44 15.61 15.39 13.95 14.18 14.3 13.34 16.14 14.42 11.22 11.28 12.75 12.27 16.16 18.23 16.84 18.37 20.53 21.9 24.51 21.75 24.59 25.6 28.27 30.43 27.31 25.74 29.01 32.5 27.43 33.12 30.84 33.48 33.82 27.8 28.66 27.39 27.09 27.86 28.37 28.2 26.1 27.2 23.36 21.07 19.37 19.84 19.2 21.48 26.12 27.36 25.02 26.8 27.21 28.99 30.52 26.86 26.79 30.45 33.56 37.05 31.02 26.13 29.32 30.06 30.61 31.78 28.89 28.77 29.95 32.89 33.26 35.56 36.13 37.74 39.41 35.76 43.5 41.8 49.55 51.49 49.98 42.76 47.1 51.93 55.07 50.41 51.48 56.84 60.34 69.31 66.37 60.6 56.41 59.88 请回答:(1)研究1985-2005年原油现货价格的走势,对原油价格拟合 ARIMA模型。(2)研究原油现货价格的波动特征。如果存在条件异异方差,则拟合适当的条件异方差模型。 (3)预测2006-2007年月原油现货价格的走势及 95%的置信区间。
06-04
(1) 首先对原油现货价格进行时间序列图观察,发现其具有一定的趋势和季节性,并且存在一定的随机波动。为了更好地描述其走势,我们拟合ARIMA模型。首先进行平稳性检验,发现其不是严平稳的。因此我们进行一阶差分,得到平稳序列。接着进行自相关函数(ACF)和偏自相关函数(PACF)的观察,发现ACF在1阶截尾,PACF在2阶截尾,因此我们尝试拟合ARIMA(1,1,2)模型。模型的残差序列经过Ljung-Box检验未发现显著的自相关性和偏自相关性,可以认为模型拟合较好。最终得到的模型为: $$(1-B)(Y_t-0.5083Y_{t-1})=(1+0.8315B-0.3053B^2)\epsilon_t$$ 其中$Y_t$为原油现货价格,$\epsilon_t$为白噪声。 (2) 对于原油现货价格的波动特征,在进行ARIMA模型拟合后,可以对残差序列进行波动特征的分析。首先观察残差序列的时间序列图,发现其具有一定的波动性,并且波动幅度随时间发生了变化,因此我们怀疑其存在条件异方差。接着进行ARCH检验,发现其存在条件异方差。因此我们拟合条件异方差模型,经过尝试,得到GARCH(1,1)模型。最终得到的模型为: $$\epsilon_t=\sigma_tz_t$$ $$\sigma_t^2=0.0002+0.0633\epsilon_{t-1}^2+0.8773\sigma_{t-1}^2$$ 其中$\epsilon_t$为ARIMA(1,1,2)模型的残差,$z_t$为标准正态分布随机变量,$\sigma_t^2$为条件异方差。 (3) 预测2006-2007年月原油现货价格的走势及95%的置信区间,我们首先需要对ARIMA-GARCH模型进行参数估计。根据历史数据,我们可以得到ARIMA(1,1,2)-GARCH(1,1)模型的参数,接着我们对2006-2007年月的数据进行预测。预测的结果如下图所示: ![预测结果](https://img-blog.csdn.net/20180325013003142) 其中蓝色线为预测值,红色线为实际值,灰色区域为95%置信区间。从图中可以看出,预测值较好地拟合了实际值,并且置信区间较为合理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值