《看了受制了》第十八天,3道题,合计77道题

发布时间 2023-09-17 12:59:42作者: wxzcch

2023年9月16日

今天因为acwing题简单,第一次正式AK

ACWING5149 简单计算

题目理解

真的很简单,就是循环

代码实现

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int main()
{
    int T;
    cin >> T;

    while(T--)
    {
        int a, b, c;
        cin >> a >> b >> c;

        cout << ((c - b) / a) * a + b << endl;
    }
    return 0;
}

ACWING5150 顶牛

题目理解

判断a[i][j],不能等于13。不然i号牛就不是强牛。

代码实现

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;

const int N = 110;

int a[N][N];
int n;
vector<int> q;

int main()
{
    cin >> n;

    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
            cin >> a[i][j];

    int res = 0;

    for(int i = 1; i <= n; i++)
    {
        int flag = 0;
        for(int j = 1; j <= n; j++)
        {
            if(a[i][j] == 1 || a[i][j] == 3)
            {
                flag = 1;
                break;
            }
        }
        if(!flag)
        {
            res++;
            q.push_back(i);
        }
    }

    if(res == 0)
        cout << 0;
    else{
        cout << res << endl;

        for(int i = 0 ; i < q.size(); i++)
            cout << q[i] << " ";
    }

    return 0;
}

ACWING5151 程序调用

题目理解

其实这个题目,并没我写的这么复杂。一个数组存位置,一个存是谁就好。哎。我想成双向链表了。

代码实现

双向链表实现

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <unordered_map>
#include <cmath>
using namespace std;

unordered_map<int, int> now, front, behind; //存当前的地方

const int N = 2e5 + 10;

int a[N];
int n, m, k;

int main()
{
    cin >> n >> m >> k;    

    for(int i = 1; i <= n; i++)
    {
        cin >> a[i];

    }

    for(int i = 1; i <= n; i++)
    {
        now[a[i]] = i;              //当前
        front[a[i]] = a[i - 1];     //前面
        if(i != n)
            behind[a[i]] = a[i + 1];    //后面
        else
            behind[a[i]] = n + 10;
    }


    long long res = 0;
    for(int i = 1; i <= m; i++)
    {
        int b;
        cin >> b;

        res += (long long)ceil(1.0 * now[b] / k);

        long long p = now[b];

        if(now[b] != 1)
        {
            now[front[b]] += 1;
            now[b] -= 1;
            if(now[b] == 0)
                now[b] = 1;

            front[behind[b]] = front[b];
            behind[front[b]] = behind[b];  //1
            behind[b] = front[b];
            behind[front[front[b]]] = b;
            front[b] = front[front[b]];
            front[behind[b]] = b;
        }
    }

    cout << res;
    return 0;
}

简单实现

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;


const int N = 1e5 + 10;

int p[N], q[N];
int n, m, k;

int main()
{
    cin >> n >> m >> k;
    
    for (int i = 1; i <= n; i ++ )
    {
        cin >> p[i];
        q[p[i]] = i;
    }
    
    long long res = 0;
    while (m -- ){
        int b;
        cin >> b;
        
        res += ceil(1.0 * q[b] / k) ;
    

        if(q[b] != 1)
        {
            // q[b] 是位置, q[b] - 1是前面那个
            // p[q[b]] 是这个数是谁, p[q[b] - 1]前面那个数是谁
            
            
            swap(q[b], q[p[q[b] - 1]]);     //交换位置
            swap(p[q[b]], p[q[b] + 1]);     //交换数
        }
    }
    
    cout << res;
    return 0;
}