2019 HZNU Winter Training Day 13 Comprehensive Training

A.Jongmah   CodeForces-1110D

  题意:你在玩一个数字游戏,有一堆写在瓦片上的数字,希望你能组成最多的三元组(三个数字相同,或顺子)。

  这题用到的方法是动态规划.f[i][j][k]表示为i的数字中,属于组成三个连续数字三元组的开头的有k 个,属于组成三个连续数字的三元组的中间的有j个,由于连续三个的数字组成三元组的数量不会大于等于三个,如果大于等于的话就是不会更优的,那么转移方程为:

    f[i+1][j][k]=max(f[i+1][j][k],f[i][k][l]+l+(a[i]−(j+k+l))/3)

  a[i] 表示i 这个数字的出现次数,最后f[m][0][0]就是答案。

  代码:

 1 /*
 2  I have a dream!A AC deram!!
 3  orz orz orz orz orz orz orz orz orz orz orz
 4  orz orz orz orz orz orz orz orz orz orz orz
 5  orz orz orz orz orz orz orz orz orz orz orz
 6 
 7  */
 8 
 9 #include<iostream>
10 #include<cstdio>
11 #include<cstdlib>
12 #include<cstring>
13 #include<cmath>
14 #include<string>
15 #include<algorithm>
16 #include<vector>
17 #include<queue>
18 #include<set>
19 #include<stack>
20 #include<map>
21 using namespace std;
22 typedef long long ll;
23 const int maxn = 1e6 + 5;
24 const int N = 105;
25 ll f[maxn][3][3], a[maxn];
26 inline int read() 
27 {
28     int x = 0, f = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar())if (ch == '-')f = -1;
29     for (; isdigit(ch); ch = getchar())x = x * 10 + ch - '0'; return x * f;
30 }
31 
32 int main() 
33 {
34     int n=read(), m=read();
35     for (int i = 1; i <= n; i++)
36         a[read()]++;
37     memset(f, -0x3f, sizeof(f));
38     f[0][0][0] = 0;
39     for (int i = 1; i <= m; i++)
40         for (int x = 0; x < 3; x++)
41             for(int y = 0; y<3; y++)
42                 for (int z = 0; z < 3; z++)
43                 {
44                     if (a[i] < x + y + z)
45                         continue;
46                     f[i][x][y] = max(f[i][x][y], f[i - 1][z][x] + (a[i] - x - y - z) / 3 + z);
47                 }
48     printf("%lld
", f[m][0][0]);
49     //system("pause");
50     return 0;
51 }
View Code

B.Ball   CodeForces-12D

  题意:一群女士来到国王的宫殿参加聚会,每位女士都拥有三个属性值:美貌,财富和智慧。当出现一位A女士的每个属性值严格大于另一位B女士的时候,B女士就会跳窗,即满足如下等式:Bi < Bj, Ii < Ij, Ri < Rj  。

  方法:线段树降维。先将 y 属性离散化,按照离散后的值作为下标建立线段树。接着按照 x 属性从大到小排序,然后依次遍历,这时可以肯定的是当前正在处理到的人她的x值肯定小于之前被更新到线段树里的人的x值。更新线段树时,每更新到一个人 i ,就在她对应的 yi 下标赋值 zi。这样我们在查询一个人 j 是否会狗带时,只需要查询区间[yj+1,Size]的最大值,这个最大值和 zj 进行比较就可以啦。好巧妙的处理方法呀,相当于人是排队依次进来的,降低了1维后变成二维,再在y的位置上放置这个高度为z的人。

作者:Amovement
来源:CSDN
原文:https://blog.csdn.net/amovement/article/details/86568141
版权声明:本文为博主原创文章,转载请附上博文链接!

  代码:

/*
 I have a dream!A AC deram!!
 orz orz orz orz orz orz orz orz orz orz orz
 orz orz orz orz orz orz orz orz orz orz orz
 orz orz orz orz orz orz orz orz orz orz orz

 */

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<stack>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N = 2100;
#define pa pair<int,int>
inline int read()
{
    int x = 0, f = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar())if (ch == '-')f = -1;
    for (; isdigit(ch); ch = getchar())x = x * 10 + ch - '0'; return x * f;
}


int n;
const int maxn = 5e5 + 50;
struct Node
{
    int x, y, z;
}a[maxn];
int h[maxn];


struct Tree
{
    int l, r, maxx;
};
Tree node[maxn << 2];        //node[maxn]为线段树处理数组
void PushUp(int i)
{
    node[i].maxx = max(node[i << 1].maxx, node[(i << 1) | 1].maxx);
}
void build(int i, int l, int r)
{
    node[i].l = l; node[i].r = r;
    if (l == r)
    {
        node[i].maxx = 0;
        return;
    }
    int mid = (l + r) / 2;
    build(i << 1, l, mid);
    build((i << 1) | 1, mid + 1, r);
    PushUp(i);
}

int getmax(int i, int l, int r)
{
    if (node[i].l == l && node[i].r == r)
        return node[i].maxx;
    int mid = (node[i].l + node[i].r) / 2;
    if (r <= mid) return getmax(i << 1, l, r);
    else if (l > mid) return getmax((i << 1) | 1, l, r);
    else return max(getmax(i << 1, l, mid), getmax((i << 1) | 1, mid + 1, r));
}
void add(int i, int k, int v)
{
    if (node[i].l == k && node[i].r == k)
    {
        node[i].maxx = max(node[i].maxx, v);
        return;
    }
    int mid = (node[i].l + node[i].r) / 2;
    if (k <= mid) add(i << 1, k, v);
    else add((i << 1) | 1, k, v);
    PushUp(i);
}

bool cmp(Node xx, Node yy)
{
    if (xx.x != yy.x)return xx.x > yy.x;
    else if (xx.y != yy.y)return xx.y > yy.y;
    else return xx.z > yy.z;
}

int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)scanf("%d", &a[i].x);
    for (int i = 1; i <= n; i++)scanf("%d", &a[i].y), h[i] = a[i].y;
    for (int i = 1; i <= n; i++)scanf("%d", &a[i].z);
    sort(h + 1, h + 1 + n);
    int Size = unique(h + 1, h + 1 + n) - h - 1;
    build(1, 1, Size + 1);

    sort(a + 1, a + 1 + n, cmp);
    int preval = a[1].x;
    int prei, ans = 0;
    //printf("----------
");
    for (int i = 1; i <= n;)
    {
        prei = i;
        for (; a[i].x == a[prei].x&&i <= n; i++)
        {
            //printf("%d %d %d
",a[i].x,a[i].y,a[i].z);
            a[i].y = lower_bound(h + 1, h + 1 + Size, a[i].y) - h;
            //printf("%d %d
",a[i].y+1,Size+1);
            if (getmax(1, a[i].y + 1, Size + 1) > a[i].z)ans++;
        }

        for (; prei < i; prei++)
        {
            add(1, a[prei].y, a[prei].z);
        }
    }
    printf("%d
", ans);
}
View Code

C.Robot   Gym-101915G

  题意:机器人在树上进行比赛,一棵树拥有n个结点和n-1条边,每条边拥有权重Wi。每个机器人有一个能力值Xi,只有当Xi>Wi的时候,机器人才能通过这条路径。

     当然,如果出现几条路径都可以通过的时候,机器人会选择较大的一条路径。

        最后求的是每个机器人最后到达的结点的和。

  方法:贪心。

  代码:

D.String of CCPC ZOJ-3985

  题意:在一串字符串中数有几个CCPC,一个CCPC具有一个价值,你也可以购买C和P,第i次购买会花费i-1个价值。所以最好的情况就是只够买一次。

  方法:先遍历一遍字符串,记录CCPC的数量,然后在遍历一次,找到几个特殊情况进行判断。

  代码:

  

 1 /*
 2  I have a dream!A AC deram!!
 3  orz orz orz orz orz orz orz orz orz orz orz
 4  orz orz orz orz orz orz orz orz orz orz orz
 5  orz orz orz orz orz orz orz orz orz orz orz
 6 
 7  */
 8 
 9 #include<iostream>
10 #include<cstdio>
11 #include<cstdlib>
12 #include<cstring>
13 #include<cmath>
14 #include<string>
15 #include<algorithm>
16 #include<vector>
17 #include<queue>
18 #include<set>
19 #include<stack>
20 #include<map>
21 using namespace std;
22 typedef long long ll;
23 const int maxn = 1e5 + 5;
24 const int N = 105;
25 char s[250000];
26 int main() 
27 {
28     int T;
29     scanf("%d", &T);
30     while(T--)
31     {
32         int len,flag,ans=0;
33         scanf("%d", &len);
34         scanf("%s", s);
35         for (int i = 0; i < len; i++)
36         {
37             if (s[i] == 'C'&&s[i + 1] == 'C'&&s[i + 2] == 'P'&&s[i + 3] == 'C')
38                 ans++;
39         }
40         for (int i = 0; i < len; i++)
41         {
42             flag = 0;
43             if (s[i] == 'C'&&s[i + 1] == 'P'&&s[i + 2] == 'C')
44             {
45                 if (i - 1 < 0 || s[i - 1] != 'C')
46                     flag++;
47             }
48             else if (s[i] == 'C'&&s[i + 1] == 'C'&&s[i + 2] == 'P'&&s[i + 3] != 'C')
49                 flag++;
50             else if (s[i] == 'C'&&s[i + 1] == 'C'&&s[i + 2] == 'C')
51             {
52                 if (s[i + 3] == 'P'&&s[i + 4] == 'C')
53                     flag--;
54                 flag++;
55             }
56             if (flag > 0)
57             {
58                 ans++;
59                 break;
60             }
61         }
62         printf("%d
", ans);
63     }
64     return 0;
65 }
View Code

E.Drazil and Tiles   CodeForces-516B

  题意:给你一块n*m的格子,希望你用1*2的砖块铺满。瓷砖拥有的形态是“<>”和“^v”,图上“.”表示格子为空,需要被铺上瓷砖,“*”表示已经被铺上,不需要再铺。如果拥有独一无二的铺满的方法就画出铺上瓷砖的格子,如果不存在铺满的情况或者有多种情况出现,就输出“Not unique”。

  方法:先统计每个空格子旁边有几个空格子,用二维数组d[][]记录(即有几种方法填满)。然后开一个队列,将只有一种方法铺满的格子扔进去,更新邻近的点,更新d[][]。最后如果仍有“.”,即说明不满足题目要求。

  代码:

 1 /*
 2  I have a dream!A AC deram!!
 3  orz orz orz orz orz orz orz orz orz orz orz
 4  orz orz orz orz orz orz orz orz orz orz orz
 5  orz orz orz orz orz orz orz orz orz orz orz
 6 
 7  */
 8 
 9 #include<iostream>
10 #include<cstdio>
11 #include<cstdlib>
12 #include<cstring>
13 #include<cmath>
14 #include<string>
15 #include<algorithm>
16 #include<vector>
17 #include<queue>
18 #include<set>
19 #include<stack>
20 #include<map>
21 using namespace std;
22 typedef long long ll;
23 const int maxn = 1e6 + 5;
24 const int N = 2100;
25 #define pa pair<int,int>
26 inline int read() 
27 {
28     int x = 0, f = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar())if (ch == '-')f = -1;
29     for (; isdigit(ch); ch = getchar())x = x * 10 + ch - '0'; return x * f;
30 }
31 
32 int n, m, dx[] = { 1,-1,0,0 }, dy[] = { 0,0,1,-1 }, d[N][N];
33 char mp[N][N];
34 queue<pa>q;
35 
36 int main() 
37 {
38     n = read(), m = read();
39     for (int i = 1; i <= n; i++)
40         scanf("%s", mp[i] + 1);
41     for(int x=1;x<=n;x++)
42         for (int y = 1; y <= m; y++)
43         {
44             if (mp[x][y] == '*')
45                 d[x][y] = 0;
46             else
47                 for (int i = 0; i < 4; i++)
48                 {
49                     int xx = x + dx[i], yy = y + dy[i];
50                     if (xx<1 || xx>n || yy<1 || yy>m || mp[xx][yy] == '*')
51                         continue;
52                     d[x][y]++;
53                 }
54         }
55     for (int i = 1; i <= n; i++)
56         for (int j = 1; j <= m; j++)
57             if (d[i][j] == 1)
58                 q.push(make_pair(i, j));
59     while (!q.empty())
60     {
61         int x = q.front().first, y = q.front().second;
62         q.pop();
63         if (d[x][y] == 0) continue;
64         for (int i = 0; i < 4; i++)
65         {
66             int xx = x + dx[i], yy = y + dy[i];
67             if (xx<1 || xx>n || yy<1 || yy>m || mp[xx][yy] != '.')
68                 continue;
69             d[x][y] = d[xx][yy] = 0;
70             if (i == 0) mp[x][y] = '^', mp[xx][yy] = 'v';
71             if (i == 1) mp[x][y] = 'v', mp[xx][yy] = '^';
72             if (i == 2) mp[x][y] = '<', mp[xx][yy] = '>';
73             if (i == 3) mp[x][y] = '>', mp[xx][yy] = '<';
74             for (int j = 0; j < 4; j++)
75             {
76                 int xxx = xx + dx[j], yyy = yy + dy[j];
77                 if (xxx<1 || xxx>n || yyy<1 || yyy>m || mp[xxx][yyy] != '.')
78                     continue;
79                 if (--d[xxx][yyy] == 1)
80                     q.push(make_pair(xxx, yyy));
81             }
82         }
83     }
84     for (int i = 1; i <= n; i++)
85         for(int j = 1; j <= m; j++)
86             if (mp[i][j] == '.')
87             {
88                 printf("Not unique
");
89                 return 0;
90             }
91     for (int i = 1; i <= n; i++)
92         puts(mp[i] + 1);
93     system("pause");
94     return 0;
95 }
View Code

F.How can I satisfy thee? Let me count the ways... ZOJ - 3025 

  题意:输入一个式子,里面具有变量,常量和运算符号,变量P,Q,R取值为0,1,2这三个常量,然后有-,*,+三个运算规则。最后问让一个式子最后答案为2的方法有多少种(即让变量取不同的值)。

  方法:暴力。先把式子转成后缀表达式。然后总共的可能就是3*3*3,进行暴力求解即可。

  代码:

原文地址:https://www.cnblogs.com/Tangent-1231/p/10386781.html