第三届河南省程序设计竞赛

版权声明:《本文为博主原创文章,转载请注明出处。 》 https://blog.csdn.net/zbq_tt5/article/details/89321313

模拟练习:

                                       A 房间安排           

  •                              内存限制:64MB 时间限制:3s Special Judge: No
  • 题目描述:

    2010年上海世界博览会(Expo2010),是第41届世界博览会。于2010年5月1日至10月31日期间,在中国上海市举行。本次世博会也是由中国举办的首届世界博览会。上海世博会以“城市,让生活更美好”(Better City,Better Life)为主题,将充分探索21世纪城市生活。

    这次世博会总投资达450亿人民币,创造了世界博览会史上的最大规模记录。吸引200个国家和国际组织参展。预计有7000万人次的参观者。

    为了更好地接待在这期间来自世界各地的参观者,如何合理安排各宾馆的住房问题提到了日程。组委会已接到了大量的客户住宿定单,每张定单的内容包括要住宿的房间数,开始住宿时间和要住的天数。为了便于整个城市各宾馆的管理,组委会希望对这些定单进行安排,目的是用尽可能少的房间来满足这些定单,以便空出更多的房间用于安排流动游客。

    组委会请求DR.Kong来完成这个任务,对这些定单进行合理安排,使得满足这些定单要求的房间数最少。

    假设:某个定单上的游客一旦被安排到某房间,在他预定住宿的期间内是不换房间的。为了简化描述,定单上的开始住宿时间为距离现在的第几天。例如,定单为(10,30,5)表示游客要求使用10个房间,第30天开始连住5天。

    输入描述:

    第一行:T 表示有T组测试数据
    每组测试数据第一行:N   表示定单数
    每组测试数据接下来有N行,每行有三个整数 A B C 表示房间数,开始住宿时间和天数
    1<=T<=100
    1<=N<=10000   1<=A<=10   1<=B<=180  1<=c<=10

    输出描述:

    输出一个整数,为满足所有定单要求的最少房间数。

    样例输入:

    1
    3
    3 10 4
    4 9 3
    3 12 6

    样例输出:

    7
  • 这道题可以直接暴力for循环,把出现的区间里需要的房间数加到数组里对应的天数上面,最后输出最多的房间数就行了!
#include<cstdio>
#include<iostream>
#include<map>
#include<cmath>
#include<cstring>
using namespace std;
const int maxn=11000;
long long int mapp[maxn];
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int m;
        cin>>m;
        memset(mapp,0,sizeof(mapp));
        int number,left,right;
        long long num_number=0;
        while(m--)
        {
            scanf("%d%d%d",&number,&left,&right);
            for(int i=left;i<left+right;++i)
            {
                mapp[i]+=number;
                num_number=(long long)max(num_number,mapp[i]);
            }
        }
        printf("%lld\n",num_number);
    }
    return 0;
}

                                 B 素数

  •                              内存限制:64MB 时间限制:3s Special Judge: No
  • 题目描述:

    走进世博园某信息通信馆,参观者将获得前所未有的尖端互动体验,一场充满创想和喜悦的信息通信互动体验秀将以全新形式呈现,从观众踏入展馆的第一步起,就将与手持终端密不可分,人类未来梦想的惊喜从参观者的掌上展开。

    在等候区的梦想花园中,参观者便开始了他们奇妙的体验之旅,等待中的游客可利用手机等终端参与互动小游戏,与梦想剧场内的虚拟人物Kr. Kong 进行猜数比赛。当屏幕出现一个整数X时,若你能比Kr. Kong更快的发出最接近它的素数答案,你将会获得一个意想不到的礼物。

    例如:当屏幕出现22时,你的回答应是23;当屏幕出现8时,你的回答应是7;若X本身是素数,则回答X;若最接近X的素数有两个时,则回答大于它的素数。

    输入描述:

    第一行:N   要竞猜的整数个数
    接下来有N行,每行有一个正整数X
    1<=N<=5   1<=X<=1000

    输出描述:

    输出有N行,每行是对应X的最接近它的素数

    样例输入:

    4
    22
    5
    18
    8

    样例输出:

    23
    5
    19
    7
  •  

这道题一开始的思路是,反正数据范围不是太大,直接利用欧拉函数找到1000个素数,然后利用map函数进行查找,不过这样写时间超限了!

#include<cstdio>
#include<iostream>
#include<map>
#include<cmath>
#include<cstring>
#include<vector>
using namespace std;
const int maxn=1100;
bool judge[maxn];
int prime[maxn];


int main()
{
    map<int,int>mapp;
    memset(judge,false,sizeof(judge));
    int cnt=0;
    for(int i=2;i<maxn;++i)
    {
        if(!judge[i])
        {
            prime[cnt++]=i;
            mapp[i]=1;
        }
        for(int j=0;j<cnt&&prime[j]*i<maxn;++j)
        {
            judge[i*prime[j]]=true;
            if(i%prime[j]==0)
                 break;
        }
    }
    int T;
    cin>>T;
    while(T--)
    {
        int n;
        cin>>n;
        int a=0,b=0;
        int num1,num2;
        for(int i=n;;++i)
        {
            if(mapp[i]!=0)
                {
                    num1=i;
                    break;
                }
            ++a;
        }
        for(int i=n;;--i)
        {
           if(mapp[i]!=0)
            {
                num2=i;
                break;
            }
           ++b;
        }
        if(a<b)
            printf("%d\n",num1);
            else if(a>b)
                printf("%d\n",num2);
                else
                    printf("%d\n",num1);
    }
    //现在说prime是我们的素数函数
    return 0;
}

然后就想,直接寻找给定数据的前面。后面的数据,这里用到了快速判断素数的方法。

#include<cstdio>
#include<iostream>
#include<map>
#include<cmath>
#include<cstring>
#include<vector>
using namespace std;


bool isPrime(int num)
{
    if(num==1) return false;
    if(num==2||num==3)
    {
        return true;
    }
    if(num%6!=1&&num%6!=5)
    {
        return false;
    }
    for(int i=5;i*i<=num;i+=6)
    {
        if(num%i==0||num%(i+2)==0)
            return false;
    }
    return true;
}
//快速判断素数的方法

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int n;
        cin>>n;
        if(isPrime(n))
        {
            printf("%d\n",n);
            continue;
        }
        for(int i=1;;++i)
        {
            if(isPrime(n+i))
            {
                printf("%d\n",n+i);
                break;
            }
            if(n-i>=0&&isPrime(n-i))
            {
                printf("%d\n",n-i);
                break;
            }

        }
    }
    //现在说prime是我们的素数函数
    return 0;
}

                           C 网络的可靠性

  •                                   内存限制:64MB 时间限制:3s Special Judge: No

题目描述:

A公司是全球依靠的互联网解决方案提供商,也是2010年世博会的高级赞助商。它将提供先进的网络协作技术,展示其”智能+互联“的生活概念,同时为参观者提供高品质的个人体验和互动,以”信息通信,尽情城市梦想”为主题贯穿。借助奇幻的剧场大屏幕和特效,展现信息通信技术的应用前景,通过生动形象的故事,向观众展示沟通无限制的未来社会前景。

为此,A公司为世博园的N个区域建立了视频通信系统,其中每个区域建立一个基站,编号依次为1,2,3...,N。通过基站之间的通信线路为各区域的参观者提供视频服务。

已知在各基站之间已铺设了一些光纤通讯线路,这些线路覆盖了所有的区域,即任意两个区域都可以进行视频传递。但为了节约成本开支,目前只铺设了N-1条线路,同时为了减轻各基站的信息传递负载,每个基站最多有三条光纤通讯线路与之连接。

但在通信系统试运行期间,A公司发现当某个基站发生故障时,会导致其它区域之间无法进行信息传递。为了提高该通信网络的可靠性,A公司准备在基站之间再新铺设一些光纤线路,使得任意一个基站故障后,其它基站之间仍然可以通讯

由于铺设线路的成本昂贵,A公司希望新增设的光纤线路越少越好。A公司请求Dr. Kong来完成这个任务

输入描述:

有多组测试数据,以EOF为结束标志。
第一行: N 表示有N个基站
接下来有N-1行:X Y   表示第X个基站与第Y个基站直连
1<=N<=10000

输出描述:

输出一个整数,表示至少需新铺设的光纤线路数

样例输入:

8
1 3
3 2
5 3
5 4 
5 6
2 7
2 8

样例输出:

3

这道题关键就是理解红色区域部分的意思,其他的两个之间如果能联系就算。

那么我们最后要找的就是有多少个只有一个路数的点,把这些点两个之间连起来。

#include<iostream>
#include<cstdio>
#include<iostream>
#include<map>
#include<cmath>
#include<cstring>
#include<vector>
using namespace std;
int mapp[10005];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        memset(mapp,0,sizeof(mapp));
    for(int i=1;i<n;++i)
    {
        int a,b;
        cin>>a>>b;
        mapp[a]++;
        mapp[b]++;
    }
    int num=0;
    for(int i=1;i<=10000;++i)
    {
        if(mapp[i]==1)
            num++;
    }
    printf("%d\n",(num+1)/2);
    }
    return 0;
}

这个是直接找节点的个数,记得以前在打比赛的时候有一道矩阵的题,跟这个差不多,可以进行一个延伸

                              D 聪明的kk 

  •                                 内存限制:64MB 时间限制:1s Special Judge: No
  • 题目描述:

    聪明的“KK”
    非洲某国展馆的设计灵感源于富有传奇色彩的沙漠中陡然起伏的沙丘,体现出本国不断变换和绚丽多彩的自然风光与城市风貌。展馆由五部分组成,馆内影院播放名为《一眨眼的瞬间》的宽银幕短片,反映了建国以来人民生活水平和城市居住环境的惊人巨变。
    可移动“沙丘”变戏法 的灵感源于其独特而雄伟的自然景观——富于传奇色彩的险峻沙丘。宏伟的结构、可循环的建材,与大自然相得益彰。环绕一周,发现它正是从沙丘那不断变换的形态中汲取灵感的。外形逼真到无论从哪个角度去观察,都能清楚地辨识出沙丘的特征。
    它“坡面”高达20米,微风吹来,你是否感觉到沙的流动?用手去触碰,却发现原来是“魔术戏法”。它表面的不锈钢面板呈现出一种富于变幻的色彩,从不同角度观察,呈现不同色泽,由此来模仿流动沙丘的光感。
    走进第三展厅有一个超大的屏幕,通过奇妙的特效,让观众犹如亲身来到浩瀚的沙漠。更为奇妙的是,只见一个小动物“KK”正从沙漠区域(矩形)的左上角沿着向右或向下的方向往右下角跑去。KK太聪明了,它居然能在跑的过程中会选择吃掉尽可能多的虫子线路。
    你知道它吃掉多少虫子吗?

    输入描述:

    第一行:N M (1≤N M≤20 0≤Xij≤500(i=1,2„.N, j=1,2„,M)
    )表示沙漠是一个N*M的矩形区域
    接下来有N行:每行有M个正整数,Xi1 Xi2 ……Xim 表示各位置中的虫子数(单个空格隔开)
    假设“KK”只能向右走或向下走。

    输出描述:

    输出有一个整数, 表示“KK”吃掉最多的虫子数。

    样例输入:

    3 4
    3 1 2 8
    5 3 4 6
    1 0 2 3

    样例输出:

    24
  • 一开始想的是用搜索来做,不过最后超时了!
  • 然后就发现,可以使用DP,而且,是边输入边判断的DP。

                           F  BUYING FEED

  •                             内存限制:64MB 时间限制:3s Special Judge: No
  • 题目描述:

    Farmer John needs to travel to town to pick up K (1 <= K <= 100)pounds of feed. Driving D miles with K pounds of feed in his truck costs D*Kcents.

    The county feed lot has N (1 <= N<= 100) stores (conveniently numbered 1..N) that sell feed. Each store is located on a segment of the X axis whose length is E (1 <= E <= 350). Store i is at location X_i (0 < X_i < E) on the number line and can sell John as much as F_i (1 <= F_i <= 100) pounds of feed at a cost of C_i (1 <= C_i <= 1,000,000) cents per pound.
    Amazingly, a given point on  the X axis might have more than one store.

    Farmer John  starts  at location 0 on this number line and can drive only in the positive direction, ultimately arriving at location E, with at least K pounds of feed. He can stop at any of the feed stores along the way and buy any amount of feed up to the the store's limit.  What is the minimum amount Farmer John has to pay to buy and transport the K pounds of feed? Farmer John
    knows there is a solution. Consider a sample where Farmer John  needs two pounds of feed from three stores (locations: 1, 3, and 4) on a number line whose range is 0..5:     
    0   1   2  3   4   5    
    ---------------------------------         
    1       1   1                Available pounds of feed         
    1       2   2               Cents per pound

     

    It is best for John to buy one pound of feed from both the second and third stores. He must pay two cents to buy each pound of feed for a total cost of 4. When John travels from 3 to 4 he is moving 1 unit of length and he has 1 pound of feed so he must pay1*1 = 1 cents.

    When John travels from 4 to 5 heis moving one unit and he has 2 pounds of feed so he must pay 1*2 = 2 cents. The total cost is 4+1+2 = 7 cents.

    输入描述:

    The first line of input contains a number c giving the number of cases that follow
    There are multi test cases ending with EOF.
    Each case starts with a line containing  three space-separated integers: K, E, and N
    Then N lines follow  :every line contains three space-separated integers:  Xi   Fi   Ci

    输出描述:

    For each case,Output A single integer that is the minimum cost for FJ to buy and  transport the feed

    样例输入:

    1
    2 5 3                 
    3 1 2
    4 1 2
    1 1 1
    

    样例输出:

    7
  • 题目很长,但是只是简单的贪心!
#include<algorithm>
#include<cstdio>
#include<iostream>
#include<map>
#include<cmath>
#include<cstring>
#include<vector>
using namespace std;
const int maxn=355;

struct xiao
{
    int x,f,c;
}a[maxn];

bool cmp(xiao a, xiao b)
{
    return a.c<b.c;
}

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int k,e,n;
//        cin>>k>>e>>n;
        if(scanf("%d%d%d",&k,&e,&n)==EOF)
            break;
        for(int i=1;i<=n;++i)
        {
           cin>>a[i].x>>a[i].f>>a[i].c;
           a[i].c+=e-a[i].x;
        }
        sort(a+1,a+1+e,cmp);
        //现在的伐度是 k
        long long int num_puts=0;
        for(int i=1;i<=e;++i)
        {
            if(k>=a[i].f)
            {
                k-=a[i].f;
                num_puts+=a[i].c*a[i].f;
            }
            else
            {
                num_puts+=k*a[i].c;
                break;
            }
        }
        printf("%lld\n",num_puts);
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/zbq_tt5/article/details/89321313