csp202009

称检测点查询

实现

#include
using namespace std;
int n,X,Y;
const int N = 210;
struct node
{
    int x,y;
    int d;
    int id;
    bool operator< (const node& t) const
    {
        if(d!=t.d)
        {
            return d<t.d;
        }
        else return id<t.id;
    }
}nodes[N];
int main()
{
    cin>>n>>X>>Y;
    int a,b;
    for(int i=0;i<n;i++)
    {
        scanf("%d%d",&a,&b);
        nodes[i].x = a;
        nodes[i].y = b;
        nodes[i].id = i+1;
        nodes[i].d = (a-X)*(a-X)+(b-Y)*(b-Y);
        
    }
    sort(nodes,nodes+n);
    for(int i=0;i<3;i++) cout<<nodes[i].id<<endl;
    
}

风险人群筛查

实现

#include
#include
#define x first
#define y second

using namespace std;

typedef pair<int, int> PII;

int n, k, t, x1, y1, x2, y2;

int main()
{
    cin >> n >> k >> t >> x1 >> y1 >> x2 >> y2;

    int res1 = 0, res2 = 0;
    while (n -- )
    {
        bool r1 = 0, r2 = 0;
        int s = 0;
        for (int i = 0; i < t; i ++ )
        {
            int x, y;
            cin >> x >> y;
            if (x >= x1 && x <= x2 && y >= y1 && y <= y2)
            {
                s ++ ;
                r1 = true;
                if (s >= k) r2 = true;
            }
            else s = 0;
        }
        if (r1) res1 ++ ;
        if (r2) res2 ++ ;
    }
    cout << res1 << endl << res2 << endl;
    return 0;
}

点亮数字人生

思路

来自acwing-yxc https://www.acwing.com/activity/content/code/content/935832/

  • 判断环和计算的顺序需要拓扑排序
  • 需要将输入值和输出器件的编号读入vector中

实现

#include
using namespace std;

const int N = 3010, M = N * 5;

int m, n;
int w[N], f[N];
int h[N], e[M], ne[M], idx;
int q[N], d[N];
vector<int> in[M], out[M];

int get(char* str)
{
    string names[] = {
        "AND", "OR", "NOT", "XOR", "NAND", "NOR"
    };
    for (int i = 0; i < 6; i ++ )
        if (names[i] == str)
            return i;
    return -1;
}

void add(int a, int b)
{
    e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
    d[b] ++ ;
}

bool topsort()   //数组q中保存拓扑排序序列
{
    int hh = 0, tt = -1;
    for (int i = 1; i <= m + n; i ++ )
        if (!d[i])
            q[ ++ tt] = i;
    while (hh <= tt)
    {
        int t = q[hh ++ ];
        for (int i = h[t]; ~i; i = ne[i])
        {
            int j = e[i];
            if ( -- d[j] == 0)
                q[ ++ tt] = j;
        }
    }
    return tt == m + n - 1;
}

int main()
{
    int T;
    scanf("%d", &T);
    while (T -- )
    {
        scanf("%d%d", &m, &n);
        memset(h, -1, sizeof h);
        idx = 0;
        memset(d, 0, sizeof d);
        char str[100];
        for (int i = 1; i <= n; i ++ )
        {
            int cnt;
            scanf("%s%d", str, &cnt);
            f[m + i] = get(str);
            while (cnt -- )
            {
                scanf("%s", str);
                int t = atoi(str + 1);
                if (str[0] == 'I') add(t, m + i);
                else add(m + t, m + i);
            }
        }
        int Q;
        scanf("%d", &Q);
        for (int i = 0; i < Q; i ++ )
        {
            in[i].clear();
            for (int j = 0; j < m; j ++ )
            {
                int x;
                scanf("%d", &x);
                in[i].push_back(x);
            }
        }
        for (int i = 0; i < Q; i ++ )
        {
            out[i].clear();
            int cnt;
            scanf("%d", &cnt);
            while (cnt -- )
            {
                int x;
                scanf("%d", &x);
                out[i].push_back(x);
            }
        }
        if (!topsort()) puts("LOOP");
        else
        {
            for (int i = 0; i < Q; i ++ )
            {
                for (int j = 0; j < m; j ++ ) w[j + 1] = in[i][j];
                for (int j = m + 1; j <= m + n; j ++ )
                    if (f[j] == 0 || f[j] == 5) w[j] = 1;  //AND和NOR时需要把初值设置为1
                    else w[j] = 0;
                for (int j = 0; j < m + n; j ++ )
                {
                    int t = q[j], v = w[t]; //从q中取数,按照拓扑排序计算
                    for (int k = h[t]; ~k; k = ne[k])
                    {
                        int u = e[k];
                        if (f[u] == 0) w[u] &= v;
                        else if (f[u] == 1) w[u] |= v;
                        else if (f[u] == 2) w[u] = !v;
                        else if (f[u] == 3) w[u] ^= v;
                        else if (f[u] == 4) w[u] |= !v;
                        else w[u] &= !v;
                    }
                }
                for (auto x: out[i])
                    printf("%d ", w[m + x]);
                puts("");
            }
        }
    }
    return 0;
}

你可能感兴趣的:(csp历年题,c++,算法)