ACM集训第一次积分赛赛前复习+day4

不知不觉4天过去了,我们迎来了我们第一次积分赛,赛前的四天我们学了以下知识点吧:

day 1、排序

之前一直想用qsort,但是总是写不明白,STL的sort()可以说是很方便了。

先写一个最基础的数组排序

bool compare(int a,int b)
{
  return a<b; //升序排列,如果改为return a>b,则为降序
}#include <algorithm>
int main()
{
  int a[20]={2,4,1,23,5,76,0,43,24,65},i;
  for(i=0;i<20;i++)
  cout<<a[i]<<endl;
  sort(a,a+20,compare);
  for(i=0;i<20;i++)
  cout<<a[i]<<endl;
  return 0;
}

还有就是稍微复杂点的结构体排序

#include <iostream>
#include <vector>
#include <algorithm>  

using namespace std;  

typedef struct example
{
    int elem1;
    int elem2;
}example;  

/*这个comparison函数很重要.如果希望升序排序,就是"<",降序排列就是">"号,这样便于直观记忆.如果希望用elem2作为比较标准
就把elem1改为elem2,这样结构体就以elem2为比较标准排序了.*/ 

bool comparison(example a,example b){
    return a.elem1<b.elem1;
}  

int main()
{
    int N;
    fin>>N;  

    vector<example> array(N);  

    for(int i=0;i<N;i++)
    {
        fin>>array[i].elem1>>array[i].elem2;
    }  

    sort(array.begin(),array.end(),comparison);  

    for(int i=0;i<N;i++)
    {
        cout<<array[i].elem1<<" "<<array[i].elem2<<endl;
    }
        return 0;
}  

day 2、BIT冬训-模拟&枚举

模拟和枚举个人感觉算是比较简单的了,因为没有算法,所以数据范围不会很大,也不用担心TLE,但是给我印象很深的就是迷宫题和五子棋题,挺复杂的。

这里贴一个五子棋的题

Alice and Bob play 5-in-a-row game. They have a playing field of size 10?×?10. In turns they put either crosses or noughts, one at a time. Alice puts crosses and Bob puts noughts.

In current match they have made some turns and now it‘s Alice‘s turn. She wonders if she can put cross in such empty cell that she wins immediately.

Alice wins if some crosses in the field form line of length not smaller than 5. This line can be horizontal, vertical and diagonal.

Input

You are given matrix 10?×?10 (10 lines of 10 characters each) with capital Latin letters ‘X‘ being a cross, letters ‘O‘ being a nought and ‘.‘ being an empty cell. The number of ‘X‘ cells is equal to the number of ‘O‘ cells and there is at least one of each type. There is at least one empty cell.

It is guaranteed that in the current arrangement nobody has still won.

Output

Print ‘YES‘ if it‘s possible for Alice to win in one turn by putting cross in some empty cell. Otherwise print ‘NO‘.

Example

Input

XX.XX..........OOOO.................................................................................

Output

YES

Input

XXOXX.....OO.O......................................................................................

Output

NO代码如下
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <string>
#include <map>
#include <vector>
#include <set>
#include <queue>
using namespace std;char mp[12][12];
int main()
{
    for (int i = 0; i < 10; ++i)
    {
        scanf("%s",mp[i]);
    }  

    for (int i = 0; i < 10; ++i)
    {
        for (int j = 0; j < 10; ++j)
        {
            if (mp[i][j] == ‘.‘)
            {
                int s=1;
                s = 1;
                for (int k = j - 1; k >= 0; --k)
                {
                    if (mp[i][k] == ‘X‘)
                    {
                        ++s;
                        if (s==5)
                        {
                            printf("YES\n");
                            return 0;
                        }
                    }
                    else break;
                }
                for (int k = j + 1; k < 10; ++k)
                {
                    if (mp[i][k] == ‘X‘)
                    {
                        ++s;
                        if (s==5)
                        {
                            printf("YES\n");
                            return 0;
                        }
                    }
                    else break;
                }
                s = 1;
                for (int k = i - 1; k >= 0; --k)
                {
                    if (mp[k][j] == ‘X‘)
                    {
                        ++s;
                        if (s==5)
                        {
                            printf("YES\n");
                            return 0;
                        }
                    }
                    else break;
                }
                for (int k = i + 1; k < 10; ++k)
                {
                    if (mp[k][j] == ‘X‘)
                    {
                        ++s;
                        if (s==5)
                        {
                            printf("YES\n");
                            return 0;
                        }
                    }
                    else break;
                }
                s = 1;
                for (int k = i - 1, q = j - 1; k >= 0 && q >= 0; --k, --q)
                {
                    if (mp[k][q] == ‘X‘)
                    {
                        ++s;
                        if (s==5)
                        {
                            printf("YES\n");
                            return 0;
                        }
                    }
                    else break;
                }  

                for (int k = i + 1, q = j + 1; k < 10 && q < 10; ++k, ++q)
                {
                    if (mp[k][q] == ‘X‘)
                    {
                        ++s;
                        if (s==5)
                        {
                            printf("YES\n");
                            return 0;
                        }
                    }
                    else break;
                }
                s = 1;
                for (int k = i + 1, q = j - 1; k < 10 && q >= 0; ++k, --q)
                {
                    if (mp[k][q] == ‘X‘)
                    {
                        ++s;
                        if (s==5)
                        {
                            printf("YES\n");
                            return 0;
                        }
                    }
                    else break;
                }
                // 右上
                for (int k = i - 1, q = j + 1; k >= 0 && q < 10; --k, ++q)
                {
                    if (mp[k][q] == ‘X‘)
                    {
                        ++s;
                        if (s==5)
                        {
                            printf("YES\n");
                            return 0;
                        }
                    }
                    else break;
                }
            }
        }
    }
    printf("NO\n");
    return 0;
}  

这题的思路是看别人的,不知道贴上来算不算抄袭、

3、可以看昨天的博客

4、简单数据结构

其实今天挺复杂的,而且也是有点填鸭了,所谓的讲就是把栈,队列,堆,树这些东西怎么定义告诉了我们,并没有告诉我们有什么性质,或者有什么意义,30分钟不到讲了四个东西。我不是吐槽今天讲课的小姐姐,因为这四天都是这样,但是今天的比较困难,就有点说不过去了。不过老实说以后讲课时间会保障1个小时,看看吧。

(1)先说说栈

我也像讲课那样吧,简单的说一下用法。

#include<iostream>
#include<stack>
using namespace std;

int main()
{
    stack <int>stk;
    //入栈
    for(int i=0;i<50;i++){
        stk.push(i);
    }
    cout<<"栈的大小:"<<stk.size()<<endl;
    while(!stk.empty())
    {
        cout<<stk.top()<<endl;
        stk.pop();
    }
    cout<<"栈的大小:"<<stk.size()<<endl;
    return 0;
}

(2)、C++ Queues(队列)、Priority Queues(优先队列)

C++队列是一种容器适配器,它给予程序员一种先进先出(FIFO)的数据结构。
1.back() 返回一个引用,指向最后一个元素
2.empty() 如果队列空则返回真
3.front() 返回第一个元素
4.pop() 删除第一个元素
5.push() 在末尾加入一个元素
6.size() 返回队列中元素的个数

队列可以用线性表(list)或双向队列(deque)来实现(注意vector container 不能用来实现queue,因为vector 没有成员函数pop_front!):
queue<list<int>> q1;
queue<deque<int>> q2;
其成员函数有“判空(empty)” 、“尺寸(Size)” 、“首元(front)” 、“尾元(backt)” 、“加入队列(push)” 、“弹出队列(pop)”等操作。

int main()
 {
     queue<int> q;
     q.push(4);
     q.push(5);
printf("%d\n",q.front());
     q.pop();
 }

C++ Priority Queues(优先队列)

C++优先队列类似队列,但是在这个数据结构中的元素按照一定的断言排列有序。
1.empty() 如果优先队列为空,则返回真
2.pop() 删除第一个元素
3.push() 加入一个元素
4.size() 返回优先队列中拥有的元素的个数
5.top() 返回优先队列中有最高优先级的元素

优先级队列可以用向量(vector)或双向队列(deque)来实现(注意list container 不能用来实现queue,因为list 的迭代器不是任意存取iterator,而pop 中用到堆排序时是要求randomaccess iterator 的!):
priority_queue<vector<int>, less<int>> pq1; // 使用递增less<int>函数对象排序
priority_queue<deque<int>, greater<int>> pq2; // 使用递减greater<int>函数对象排序
其成员函数有“判空(empty)” 、“尺寸(Size)” 、“栈顶元素(top)” 、“压栈(push)” 、“弹栈(pop)”等。

 1 #include <iostream>
 2  #include <queue>
 3  using namespace std;
 4
 5  class T {
 6  public:
 7      int x, y, z;
 8      T(int a, int b, int c):x(a), y(b), z(c)
 9      {
10      }
11  };
12  bool operator < (const T &t1, const T &t2)
13  {
14      return t1.z < t2.z; // 按照z的顺序来决定t1和t2的顺序
15  }
16  main()
17  {
18      priority_queue<T> q;
19      q.push(T(4,4,3));
20      q.push(T(2,2,5));
21      q.push(T(1,5,4));
22      q.push(T(3,3,6));
23     while (!q.empty())
24      {
25          T t = q.top();
26          q.pop();
27          cout << t.x << " " << t.y << " " << t.z << endl;
28      }
29      return 1;
30  }

输出结果为(注意是按照z的顺序从大到小出队的): 
      3 3 6 
      2 2 5 
      1 5 4 
      4 4 3

 #include <iostream>
  #include <queue>
  using namespace std;
  class T
  {
  public:
      int x, y, z;
     T(int a, int b, int c):x(a), y(b), z(c)
     {
     }
 };
 bool operator > (const T &t1, const T &t2)
 {
     return t1.z > t2.z;
 }
 main()
 {
     priority_queue<T, vector<T>, greater<T> > q;
     q.push(T(4,4,3));
     q.push(T(2,2,5));
     q.push(T(1,5,4));
     q.push(T(3,3,6));
     while (!q.empty())
     {
         T t = q.top();
         q.pop();
         cout << t.x << " " << t.y << " " << t.z <<  endl;
   }
    return 1;
}

输出结果为: 
      4 4 3 
      1 5 4 
      2 2 5 
      3 3 6
      如果我们把第一个例子中的比较运算符重载为: bool operator < (const T &t1, const T &t2) { return t1.z > t2.z; // 按照z的顺序来决定t1和t2的顺序} 则第一个例子的程序会得到和第二个例子的程序相同的输出结果。

目前主要学的就这么多了,感觉题目已经很难很难了。

以上均为不规范转载。。。因为我还没有完全掌握。

原文地址:https://www.cnblogs.com/PixelOrange/p/8321871.html

时间: 2024-08-02 05:02:21

ACM集训第一次积分赛赛前复习+day4的相关文章

acm集训训练赛B题【排序+模拟】

一.原题 Description Being a programmer, you like arrays a lot. For your birthday, your friends have given you an array a consisting of ndistinct integers. Unfortunately, the size of a is too small. You want a bigger array! Your friends agree to give you

yzm10的ACM集训小感

7月30号,ACM集训进行了两周,一切都已on the right way.这时的我适时地从题海中探出头,其实除了刷题,也该写点什么来总结下过去.首先,在第一周里,我学习了数据结构,知道了STL这么一个神奇的存在.不管是stack.queue亦或multiset,还有最具代表的priority_queue(习惯性地打上下划线..)有时候堆的logn真的能帮你优化不少时间.只需一个头文件,你就可以调用他们(美滋滋~).还有k学长讲的并查集也非常实用,区间合并用到cys学长share的next跳(类

七月23 ACM集训——最小生成树

prim算法模板 int  prim(int x){    int i,j,sum=0,min=M,k; memset(vit,0,sizeof(vit));    memset(dis,0,sizeof(dis));    for(i=1;i<=m;i++)        dis[i]=p[x][i];    dis[x]=0;    vit[x]=1;     for(i=1;i<m;i++)    {        min=M;        k=-1;        for(j=1;j

acm集训训练赛A题【签到题】

一.题目 Description After winning gold and silver in IOI 2014, Akshat and Malvika want to have some fun. Now they are playing a game on a grid made of nhorizontal and m vertical sticks. An intersection point is any point on the grid which is formed by t

acm集训训练赛(二)D题【并查集】

一.题目 Description There is a town with N citizens. It is known that some pairs of people are friends. According to the famous saying that ?The friends of my friends are my friends, too? it follows that if A and B are friends and B and C are friends th

2015年暑期ACM集训总结

今年暑假学校照例进行了ACM的暑假集训,跟以往不同的是:今年我作为一个老队员站上了讲台,体验了一把当老师的感觉,给新队员讲解ACM系列知识. 在集训开始之前,我跟一个同学写了一个ACM的评测系统,啊,评测系统?高达上啊.其实不然,这个系统也没有想象的那么复杂,只能简单地评测程序的几种状态: Compile Error Runtime Error Time Limit Exceeded Wrong Answer Accepted 所写的评测系统,是一个线下的评测系统,即只能本地上交程序,然后评测,

七月25 ACM集训——kmp算法

字符串比配问题,通过引入next[]而使效率提高 关于next[]数组,是对模式串的特征来构造的: 为了确定在匹配不成功时,下次匹配时j的位置,引入了next[]数组,next[j]的值表示P[0...j-1]中最长后缀的长度等于相同字符序列的前缀. 在匹配过程称,若发生不匹配的情况,如果next[j]>=0,则目标串的指针i不变,将模式串的指针j移动到next[j]的位置继续进行匹配:若next[j]=-1,则将i右移1位,并将j置0,继续进行比较. 具体思想: 根据定义next[0]=-1,

五一九天假期ACM集训记录【4月27日—第二天】

今天上午九点到下午两点,我们做了山东省第三届ACM原题,整体结果还比较不错,我们队做出了4道题,两道模拟,一道字符串处理,一道高数问题.比赛前期我们配合得不错,比较快速的A出了两道模拟题.但后来状态不太好,主要是我的问题,后来我做的是那道字符串处理的题目,有点麻烦,分割单词,然后在单词中提取数字,数字还不是标准的,做了好长时间,终于测试数据过了,提交了一下,结果果断地返回了一个WA,那种感觉太纠结了,就像当头一棒,千辛万苦弄出来的一个,还是WA,用来队友给我说得考虑空格,两个单词之间可能有一个或

ACM集训的Day3 B。。。盲目搜索之DFS。。。

milk 一.题目描述: gzp有三个容量分别是A,B,C升的桶,A,B,C分别是三个从1到20的整数, 最初,A和B桶都是空的,而C桶是装满牛奶的.有时,农民把牛奶从一个桶倒到 另一个桶中,直到被灌桶装满或原桶空了.当然每一次灌注都是完全的.由于节约, 牛奶不会有丢失 写一个程序去帮助gzp找出当A桶是空的时候,C桶中牛奶所剩量的所有可能性. 二.格式: INPUT FORMAT: 单独的一行包括三个整数A,B和C. OUTPUT FORMAT: 只有一行,升序地列出当A桶是空的时候,C桶牛奶