紫书第5章 C++STL

例题

例题5-1 大理石在哪儿(Where is the Marble?,Uva 10474)

主要是熟悉一下sort和lower_bound的用法

关于lower_bound:

http://blog.csdn.net/niushuai666/article/details/6734403

此外还有upper_bound

http://blog.csdn.net/niushuai666/article/details/6734650

#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <stack>
#include <cctype>
using namespace std;

#define mem(a,b) memset(a,b,sizeof(a))
#define pf printf
#define sf scanf
#define spf sprintf
#define debug printf("!\n")
#define INF 10000
#define MAXN 5010
#define MAX(a,b) a>b?a:b
#define blank pf("\n")
#define LL long long

int a[10005];

int main()
{
     int n,q,i,x,cnt=1;
     while(~sf("%d%d",&n,&q) && n)
     {
          pf("CASE# %d:\n",cnt++);
          for(i=0;i<n;i++) sf("%d",&a[i]);
          sort(a,a+n);
          while(q--)
          {
               sf("%d",&x);
               int p = lower_bound(a,a+n,x)-a;
               if(a[p]!=x)
               {
                    pf("%d not found\n",x);
                    continue;
               }
               else
                    pf("%d found at %d\n",x,p+1);
          }
     }
}

例题5-2 木块问题(The Blocks Problem,Uva 101)

主要是熟悉vector的pb和resize,以及字符串结束的处理方法

#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <stack>
#include <cctype>
#include <vector>
using namespace std;

#define mem(a,b) memset(a,b,sizeof(a))
#define pf printf
#define sf scanf
#define spf sprintf
#define pb push_back
#define debug printf("!\n")
#define INF 10000
#define MAXN 5010
#define MAX(a,b) a>b?a:b
#define blank pf("\n")
#define LL long long

vector<int> pile[30];
int n;

void find_block(int x,int &p,int &h)
{
     for(p=0;p<n;p++)
     {
          for(h=0;h<pile[p].size();h++)
          {
               if(pile[p][h] == x) return;
          }
     }
}

void clear_block(int p,int h)
{
     int i;
     for(i = h+1;i<pile[p].size();i++)
     {
          int b = pile[p][i];
          pile[b].pb(b);
     }
     pile[p].resize(h+1);
}

void onto_block(int p,int h,int p2)
{
     int i;
     for(i = h;i<pile[p].size();i++)
          pile[p2].pb(pile[p][i]);
     pile[p].resize(h);
}

void print()
{
     int p,h;
     for(p=0;p<n;p++)
     {
          pf("%d:",p);
          for(h=0;h<pile[p].size();h++)
          {
               pf(" %d",pile[p][h]);
          }
          blank;
     }
}

int main()
{
     int i,a,b;
     sf("%d",&n);
     for(i=0;i<n;i++) pile[i].pb(i);
     char s[5],s1[5];

     while(sf("%s",s) && s[0]!=‘q‘)
     {

          sf("%d%s%d",&a,s1,&b);
          int pa,pb,ha,hb;
          find_block(a,pa,ha);
          find_block(b,pb,hb);
          if(pa==pb) continue;
          if(!strcmp(s,"move")) clear_block(pa,ha);
          if(!strcmp(s1,"onto")) clear_block(pb,hb);
          onto_block(pa,ha,pb);
     }
     print();
}
时间: 2024-10-08 11:13:21

紫书第5章 C++STL的相关文章

紫书第4章 函数和递归

1  序 系统的整理下第四章的学习笔记.同上次一样,尽量在不依赖书本的情况下自己先把例题做出来.这次有许多道题代码量都比较大,在例题中我都用纯C语言编写,但由于习题的挑战性和复杂度,我最终还是决定在第五章开始前,就用C++来完成习题.不过所有的代码都是能在C++提交下AC的. 在习题中,我都习惯性的构造一个类来求解问题,从我个人角度讲,这会让我的思路清晰不少,希望自己的一些代码风格不会影响读者对解题思路的理解. 其实在第四章前,我就顾虑着是不是真的打算把题目全做了,这些题目代码量这么大,要耗费很

紫书第三章 数组和字符串

1  序 系统的整理下第三章的学习笔记.例题代码是在未看书本方法前自己尝试并AC的代码,不一定比书上的标程好:习题除了3-8百度了求解方法,其它均独立完成后,会适当查阅网上资料进行整理总结.希望本博文方便自己日后复习的同时,也能给他人带来点有益的帮助(建议配合紫书--<算法竞赛入门经典(第2版)>阅读本博客).有不足或错误之处,欢迎读者指出. 2  例题 2.1  UVa272--Tex Quotes #include <stdio.h> int main() { bool log

lrj紫书第五章

UVA-1592 1 // UVa1592 Database 2 // Rujia Liu 3 // 本程序只是为了演示STL各种用法,效率较低.实践中一般用C字符串和哈希表来实现. 4 5 #include<iostream> 6 #include<cstdio> 7 #include<vector> 8 #include<string> 9 #include<map> 10 #include<sstream> 11 using n

紫书第五章训练2 F - Compound Words

F - Compound Words You are to find all the two-word compound words in a dictionary. A two-word compound word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary. Input Standard input consists of a number

紫书第9章 动态规划初步

9.1 数字三角形 9.1.2 记忆化搜索与递推 方法1:递归计算.程序如下(需注意边界处理): int solve(int i,int j) { return a[i][j] + (i==n ?0:max(solve(i+1,j),solve(i+1,j+1)); } 用直接递归的方法计算状态转移方程,效率往往十分低下.其原因是相同的子问题被重复计算了多次. 方法2:递推计算.程序如下(需再次注意边界处理): int i, j; for(j = 1; j <= n; j++) d[n][j]

算法入门经典第二版 紫书 第9章 动态规划初步

9-1  http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3466 书上是递推的,我写了个记忆化搜索dp 有3种决策,左边的车,右边的车,原地不动, 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #define mt(a,b)

紫书第三章训练2 暴力集

A - Master-Mind Hints MasterMind is a game for two players. One of them, Designer, selects a secret code. The other, Breaker, tries to break it. A code is no more than a row of colored dots. At the beginning of a game, the players agree upon the leng

紫书第三章练习题:UVA 1225 Digit Counting by 15邱盼威

来源:http://m.blog.csdn.net/article/details?id=70861055 Trung is bored with his mathematicshomeworks. He takes a piece of chalk and starts writing a sequence ofconsecutive integers starting with 1 to N (1 < N < 10000). After that, hecounts the number

紫书第五章训练3 D - Throwing cards away I

D - Throwing cards away I Given is an ordered deck of n cards numbered 1 to n with card 1 at the top and card n at the bottom. The following operation is performed as long as there are at least two cards in the deck: Throw away the top card and move