算法竞赛入门经典5.2stl初步(代码片段)

jkzr jkzr     2022-12-28     552

关键词:

1.  排序和检索,学会使用sort排序,以及low_bound函数

Raju and Meena love to play with Marbles. They have got a lot of marbles with numbers written on them. At the beginning, Raju would place the marbles one after another in ascending order of the numbers written on them. Then Meena would ask Raju to find the first marble with a certain number. She would count 1...2...3. Raju gets one point for correct answer, and Meena gets the point if Raju fails. After some fixed number of trials the game ends and the player with maximum points wins. Today it’s your chance to play as Raju. Being the smart kid, you’d be taking the favor of a computer. But don’t underestimate Meena, she had written a program to keep track how much time you’re taking to give all the answers. So now you have to write a program, which will help you in your role as Raju.

Input

There can be multiple test cases. Total no of test cases is less than 65. Each test case consists begins with 2 integers: N the number of marbles and Q the number of queries Mina would make. The next N lines would contain the numbers written on the N marbles. These marble numbers will not come in any particular order. Following Q lines will have Q queries. Be assured, none of the input numbers are greater than 10000 and none of them are negative. Input is terminated by a test case where N = 0 and Q = 0.

Output

For each test case output the serial number of the case. For each of the queries, print one line of output. The format of this line will depend upon whether or not the query number is written upon any of the marbles. The two different formats are described below: ? ‘x found at y’, if the first marble with number x was found at position y. Positions are numbered 1, 2, . . . , N. ? ‘x not found’, if the marble with number x is not present. Look at the output for sample input for details.

Sample Input

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

Sample Output

CASE# 1: 5 found at 4

CASE# 2: 2 not found 3 found at 3

#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
using namespace std;

const int maxn = 10005;
int N,Q;
int temp;
int casee=1;
int a[maxn];

int main()

    while(cin>>N>>Q)
    
        if(N==0&&Q==0)
            break;
        else
        
            cout<<"CASE# "<<casee++<<":"<<endl;
            for(int i=0;i<N;i++)
                cin>>a[i];
            sort(a,a+N);//排序,可以自己手写cmp函数作为参数,一般是用在结构体里面排序。
            while(Q--)
            
                cin>>temp;
                int flag=lower_bound(a,a+N,temp)-a;//在已经排序的数组里面寻找x
                if(a[flag]==temp)
                    cout<<temp<<" found at "<<flag+1<<endl;
                else
                    cout<<temp<<" not found"<<endl;
            
        
    
    return 0;

 

2.  vector的用法。

常用函数,push_back() 尾部添加元素     pop_back() 删除最后一个元素  size() 返回长度  resize(b) 改变大小,保留下标0—b之间的元素

reverse(vec.begin(),vec.end());将元素翻转,即逆序排列!

vector元素遍历利用迭代器  for(vector<int>::iterator it = v.begin();it!=v.end();it++)

                cout<<*it<<" "; 

UVA-101

题目链接https://vjudge.net/problem/UVA-101

#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
using namespace std;

const int maxn=30;
vector<int>v[maxn];
int n;
string s1,s2;
int a,b;

void findd(int a,int &p,int &h)

    for(p=0;p<n;p++)
    
        for(h=0;h<v[p].size();h++)
        
            if(v[p][h]==a)
                return;
        
    


void fun1(int p,int h)//归位

    for(int i=h+1;i<v[p].size();i++)
    
        int j=v[p][i];
        v[j].push_back(j);
    
    v[p].resize(h+1);


void fun2(int p1,int h,int p2)

    for(int i=h;i<v[p1].size();i++)
        v[p2].push_back(v[p1][i]);
    v[p1].resize(h);


int main()

    cin>>n;
    for(int i=0;i<n;i++)
        v[i].push_back(i);
    while(cin>>s1)
    
        if(s1=="quit")
            break;
        cin>>a>>s2>>b;
        int pa,ha,pb,hb;
        findd(a,pa,ha);
        findd(b,pb,hb);
        if(pa==pb)
            continue;
        //cout<<pa<<" "<<ha<<" "<<pb<<" "<<hb<<endl;
        if(s2=="onto")
            fun1(pb,hb);
        if(s1=="move")
            fun1(pa,ha);
        fun2(pa,ha,pb);
    
    for(int i=0;i<n;i++)
    
        cout<<i<<":";
        for(int j=0;j<v[i].size();j++)
            cout<<" "<<v[i][j];
        cout<<endl;
    
    return 0;

 

stl初步

最近看了算法竞赛入门经典,里面有关于C++STL的部分,借此简单总结一下书中提到的用法,等自己复习的时候好翻出来查看1、排序(sort)在algorithm头文件中,已经写好了许多常用的算法,其中排序算法时经常使用的算法default(1)temp... 查看详情

[算法竞赛入门经典]crosswordanswersacm/icpcworldfinals1994,uva232(代码片段)

DescriptionAcrosswordpuzzleconsistsofarectangulargridofblackandwhitesquaresandtwolistsofdefinitions(ordescriptions).Onelistofdefinitionsisfor“words”tobewrittenlefttorightacrosswhitesquaresintherowsand 查看详情

《算法竞赛入门经典(第2版)》pdf下载在线阅读,求百度网盘云资源

《算法竞赛入门经典(第2版)》(刘汝佳)电子书网盘下载免费在线阅读资源链接:链接:https://pan.baidu.com/s/1hn9oYzCM-fjrw649WmvKyg 提取码:6bov  书名:算法竞赛入门经典(第2版)作者:刘汝佳豆瓣评分:8.9出版社:清... 查看详情

随机生成数,摘自算法竞赛入门经典p120-p123测试stl。

//#include<bits/stdc++.h>#include<cstring>#include<iostream>#include<cstdio>#include<time.h>///调用time的头文件。#include<algorithm>#include<vector>#include<cstdl 查看详情

[算法竞赛入门经典]kickdownacm/icpcneerc2004,uva1587(代码片段)

DescriptionAresearchlaboratoryofaworld-leadingautomobilecompanyhasreceivedanordertocreateaspecialtransmissionmechanism,whichallowsforincrediblyefficientkickdown—anoperationofswitchingtolowergear.After 查看详情

《算法竞赛入门经典(第二版)》pdf

...下载内容简介  · · · · · ·《算法竞赛入门经典(第2版)》是一本算法竞赛的入门与提高教材,把C/C++语言、算法和解题有机地结合在一起,淡化理论,注重学习方法和实践技巧。全书内容分为12章,... 查看详情

笔记算法竞赛入门经典

contents基础题目选解WERTYU、数据结构基础暴力求解法高效算法设计动态规划初步数学概念与方法图论模型与算法 1、WERTYU刚开始的思路是output[‘S‘]=‘A‘。。。书上的常量表应该会比较通用一点。。而不仅仅适于有序常量。i... 查看详情

[算法竞赛入门经典]messagedecoding,acm/icpcworldfinals1991,uva213(代码片段)

DescriptionSomemessageencodingschemesrequirethatanencodedmessagebesentintwoparts.Thefirstpart,calledtheheader,containsthecharactersofthemessage.Thesecondpartcontainsapatternthatrepresentsthemessage.Yo 查看详情

[算法竞赛入门经典]repeatingdecimals,acm/icpcworldfinals1990,uva202(代码片段)

DescriptionThedecimalexpansionofthefraction1/33is0.03,wherethe03isusedtoindicatethatthecycle03repeatsindefinitelywithnointerveningdigits.Infact,thedecimalexpansionofeveryrationalnumber(fraction)hasare 查看详情

《算法竞赛入门经典》例题5.4.1(代码片段)

题目:现代数学的著名证明之一是GeorgCantor证明了有理数是可枚举的。他是用下面这一张表来证明这一命题的:第一项是1/1,第二项是是1/2,第三项是2/1,第四项是3/1,第五项是2/2,……。输入n,... 查看详情

算法竞赛入门经典第二版第二章习题及思考题(代码片段)

enmmmmmm】 大部分好像除了最后一个思考题都很简单代码如下#include<iostream>#include<cstring>#include<cstdio>#include<cmath>intmain()/*for(inti=100;i<=999;i++)inta=i/100;intc=i%10;intb=(i-a*10 查看详情

算法竞赛入门经典题解——第三章3-4周期串uva455(代码片段)

 思路:遍历可能的周期,比较s[k]与s[k%i](其中i为周期)#include<stdio.h>#include<stdlib.h>#include<string.h>intmain()intT;chars[90];scanf("%d",&T);while(T--)scanf("%s",s);intlen,i;len=strlen(s) 查看详情

算法竞赛入门经典(第二版)3-3数数字uva1225(代码片段)

#include<cstdio>#include<string.h>intmain()intn;scanf("%d",&n);getchar();while(n--)charstr[10000];scanf("%s",str);intlen=strlen(str);intnum=0;for(inti=0;i<10;i++)for(intj=0;j<len;j++)if((str[j]-‘0‘)==i)num++;printf("%d",num);num=0;printf("");https://vjudge.net/problem/UVA-1... 查看详情

算法竞赛入门经典数组实现树的层序遍历的一个小错误(代码片段)

p154紫书原文:纠正一下这里的newnode()函数中,have_value[root]=false应该是have_value[u]=false附上代码,不知道原题的可以上网搜一下例题6_7Uva122树的层序遍历数组(静态链表)实现:#include<cstdio>#include<cstring>#includ 查看详情

算法竞赛入门经典_4.3_递归

...归头,二是递归体。我们使用gcc调试工具H:编程书籍学习算法竞赛入门经典2代码算法入门经典第四章>bf‘b‘不是内部或外部命令,也不是可运行的程 查看详情

算法竞赛入门经典——训练指南

1.UVa11300我的代码:#include<iostream>#include<cstdio>#include<algorithm>usingnamespacestd;longlongC[1000010],M,a;intmain(){intn;while(~scanf("%d",&n)){C[0]=0;for(inti=1;i<=n;i++){ 查看详情

《入门经典》——6.29

算法分析初步: 在任何一本比较正经的算法书当中,第一章都会介绍算法正确性的证明方法和对算法复杂度的分析,因为算法本身两个重要的特性就是正确性和高效性,只有保证了这两部分方能设计出有实际利用价值的算法... 查看详情

算法竞赛入门经典刘汝佳

        点击图片或此处下载  查看详情