kattis Block Game + Chess Tournament + Completing the Square + Millionaire Madness

这几道题都来自同一场比赛,这次把这几道题做一个整理。原题不说了,只说一下个别题的题目大意和思路。
A - Block Game
一个玄学的博弈问题,虽然没看懂,但是做出来了。
这里只说一下必胜态,1如果一个是另一个的倍数(特别的,两者相等)显然知道这是必胜态。2如果某个状态能拿的是两个以上,那么先手就能控制后手的走法,也就是可以给后手留下1次或者0次(也就是后手要拿另一堆),那么只要我们发现这种情况就算胜。否则就轮流拿,看能拿的次数的奇偶看胜负

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
//#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

int main()
{
    LL a, b;
    cin>>a>>b;
    if(a<b)
        swap(a, b);
    int x=a/b;
    int cnt=0;
    while(a!=0&&b!=0)
    {
        if(a<b)
            swap(a, b);
            cnt++;
        if(a%b==0||a/2>=b) break;
        a=a%b;
    }
    if(cnt%2)
        puts("win");
    else
        puts("lose");
    return 0;
}

B - Chess Tournament
有一些棋手,让他们自己汇报他们与其他的棋手的胜负情况,具有传递性(即a>b&&b>c 推出 a>c )问有没有说谎。三种情况胜、负、平。
看得出来是有向图判断成环,看得出是用拓扑排序
注意平局的情况,只能把他们几个棋手作为一个点,不能做条无向边,否则根据拓扑排序的原理知道不行。
可以利用并查集把相等的点变成一个点,把祖先作为点重新建图,(这样会有重边,但是根据拓扑排序的原理知道没事),点的数量就是根的数量(代码中的flag变量)

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
#define x first
#define y second
//#include <bits/stdc++.h>
using namespace std;
#define PII pair<int,int>
typedef long long LL;
const int N = 500010;
int n, m;
int pre[N];
int in[N];
int head[N], tot;
//map<int,int>mp;
//int dian;
queue<int>q;
struct node
{
    int v;
    int next;
} a[N];
vector<PII>big, small, deng;
void add(int u, int v)
{
    a[++tot].v = v, a[tot].next = head[u], head[u] = tot;
}
int fi(int x)
{
    if (pre[x] != x)
        pre[x] = fi(pre[x]);
    return pre[x];
}
void prework()
{
    for (int i = 0; i < N; i++)
        pre[i] = i;
}
void cinn()
{
    char c;
    int a, b;
    for (int i = 0; i < m; i++)
    {
        scanf("%d %c %d", &a, &c, &b);
        if (c == '>')
            big.push_back({ a, b });
        else if (c == '<')
            small.push_back({ a, b });
        else
        {
            int x = fi(a);
            int y = fi(b);
            if (x != y)
                pre[x] = y;
        }
    }
}
bool topo()
{
    int cnt = 0;
    int flag = 0;
    for (int i = 0; i < n; i++)
        if (pre[i] == i)
        {
            flag++;
            if (in[i] == 0) {
                q.push(i);
                cnt++;
            }
        }
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        in[u]--;
        for (int i = head[u]; i; i = a[i].next)
        {
            int v = a[i].v;
            in[v]--;
            if (!in[v])
            {
                q.push(v);
                cnt++;
            }
        }
    }
    if (cnt == flag) return 1;
    else return 0;
}
int main()
{
    scanf("%d %d", &n, &m);
    prework();
    cinn();
    for (int i = 0; i < big.size(); i++)
    {
        int xx = fi(big[i].x);
        int yy = fi(big[i].y);
        if (xx == yy)
        {
            puts("inconsistent");
            return 0;
        }
        add(xx, yy);
        in[yy]++;
    }
    for (int j = 0; j < small.size(); j++)
    {
        int xx = fi(big[j].x);
        int yy = fi(big[j].y);
        if (xx == yy)
        {
            puts("inconsistent");
            return 0;
        }
        add(yy, xx);
        in[xx]++;
    }

    if (topo())
        puts("consistent");
    else
        puts("inconsistent");
    return 0;
}

C - Completing the Square
知道正方形的三个点,找第四个,题目保证都是整数
最后一个点也是整数
求出三点的距离,相等的夹着的点就是对顶点。注意求距离别开方。

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
//#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int x[5], y[5];
int len(int x1, int y1, int x2, int y2)
{
    int ret = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
    return ret;
}
void ans(int a, int b, int c)
{
    //    a--, b--, c--;
    int xx = x[b] + x[c] - x[a];
    int yy = y[b] + y[c] - y[a];
    printf("%d %d\n", xx, yy);
}
int main()
{
    int n = 3;
    for (int i = 0; i < n; i++)
        scanf("%d %d", &x[i], &y[i]);
    int a, b, c;
    a = len(x[0], y[0], x[1], y[1]);
    b = len(x[1], y[1], x[2], y[2]);
    c = len(x[0], y[0], x[2], y[2]);
    if (a == b)
        ans(1, 2, 0);
    else if (b == c)
        ans(2, 1, 0);
    else
        ans(0, 1, 2);
    return 0;
}

G - Millionaire Madness
给一个网格图,要求从左上走到右下,网格有高度,从低到高要用梯子,问梯子的最小高度。
用bfs,队列改为优先队列,按照梯子高度排序,先走梯子矮的,注意非负。
注意到达某个点所需要的梯子的高度可能会有变化,也就是某个点可能从别的方向上来会更好。如果发生这样的情况,那么队列里就会有多个相同点,但是这几个点的高度属性不同(易知其中必定有最低的那个路径),同时又必定是低的先进去,所以处理完一个点之后标记已经走过即可。

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
//#include <bits/stdc++.h>
#define x first
#define y second
#define PII pair<int,int>
using namespace std;
typedef long long LL;
int sx[] = { 1, 0, -1, 0 };
int sy[] = { 0, 1, 0, -1 };
int a[1002][1002];
bool vis[1002][1002];
int n, m;
int maxx;
priority_queue<pair<int, PII> >q;
void bfs(PII start)
{
    q.push({ 0,start });
    while (!q.empty())
    {
        PII u = q.top().y;
        int heigh = -q.top().x;
        q.pop();
        if (vis[u.x][u.y]) continue;
        vis[u.x][u.y] = 1;
        //cout << "**" << u.x << " " << u.y << " " << maxx << endl;
        if (maxx < heigh) maxx = heigh;
        if (u.x == n - 1 && u.y == m - 1) return;
        for (int i = 0; i < 4; i++)
        {
            int xx = u.x + sx[i];
            int yy = u.y + sy[i];
            if (xx < 0 || yy < 0 || xx >= n || yy >= m ) continue;
            if (vis[xx][yy]) continue;
            PII v = make_pair(xx, yy);
            q.push({ -max(a[xx][yy] - a[u.x][u.y], 0), v });//注意负号
        }
    }
}
int main()
{
    cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
            scanf("%d", &a[i][j]);
    }
    bfs({ 0, 0 });
    printf("%d\n", maxx);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值