Codeforces Round 871 (Div. 4)(A~H)

目录

比赛链接

[A. Love Story](#A. Love Story)

[B. Blank Space](#B. Blank Space)

[C. Mr. Perfectly Fine](#C. Mr. Perfectly Fine)

[D. Gold Rush](#D. Gold Rush)

[E. The Lakes](#E. The Lakes)

[F. Forever Winter](#F. Forever Winter)

[G. Hits Different](#G. Hits Different)

[H. Don't Blame Me](#H. Don't Blame Me)


比赛链接

Dashboard - Codeforces Round 871 (Div. 4) - Codeforces

A. Love Story

找到与codeforces 有多少个不同的字符。

cpp 复制代码
#include<bits/stdc++.h>
#define int long long  
#define TEST int T; cin >> T; while (T--)
#define ios ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
#define pll pair<int,int>
const int N = 1e6 + 30;
const int M = 1e3 + 10;
const int inf = 512785182741247112;
const int mod = 1e9 + 7;
using namespace std;
string s = "codeforces";
void solve() {
    string st;
    cin >> st;
    int ans = 0;
    for (int i = 0; i < st.size(); i++)
    {
        if (st[i] != s[i])ans++;
    }
    cout << ans << "\n";
    
}
signed main() {

    ios;
    TEST
    solve();
    return 0;
}

B. Blank Space

找连续最长的0.

cpp 复制代码
#include<bits/stdc++.h>
#define int long long  
#define TEST int T; cin >> T; while (T--)
#define ios ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
#define pll pair<int,int>
const int N = 1e6 + 30;
const int M = 1e3 + 10;
const int inf = 512785182741247112;
const int mod = 1e9 + 7;
using namespace std;

void solve() {
    int n;
    cin >> n;
    int cnt = 0, ans = 0;
    for (int i = 1; i <= n; i++)
    {
        int x;
        cin >> x;
        if (x == 0) cnt++;
        else
        {
            ans = max(ans, cnt);
            cnt = 0;
        }
    }
    ans = max(ans, cnt);
    cout << ans << "\n";
}
signed main() {

    ios;
    TEST
    solve();
    return 0;
}

C. Mr. Perfectly Fine

分成01、10、11的三种情况,找全部的最小值,前面两个的最小值相加与第三个的最小值比较出最小值。

cpp 复制代码
#include<bits/stdc++.h>
#define int long long  
#define TEST int T; cin >> T; while (T--)
#define ios ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
#define pll pair<int,int>
const int N = 1e6 + 30;
const int M = 1e3 + 10;
const int inf = 512785182741247112;
const int mod = 1e9 + 7;
using namespace std;

void solve() {
    int n;
    cin >> n;
    int c1 = 1e9, c2 = 1e9, c3 = 1e9;
    for (int i = 1; i <= n; i++)
    {
        int t;
        cin >> t;
        string s;
        cin >> s;
        if (s == "01") c2 = min(c2, t);
        if (s == "10") c1 = min(c1, t);
        if (s == "11") c3 = min(c3, t);
    }
    int res = min(c1 + c2, c3);
    if (res != 1e9)
        cout << min(c1 + c2, c3) << "\n";
    else cout << "-1\n";
}
signed main() {

    ios;
    TEST
    solve();
    return 0;
}

D. Gold Rush

按照题意可以将一堆分成三堆,看看目标是否出现即可,搜索。

cpp 复制代码
#include<bits/stdc++.h>
#define int long long  
#define TEST int T; cin >> T; while (T--)
#define ios ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
#define pll pair<int,int>
const int N = 1e6 + 30;
const int M = 1e3 + 10;
const int inf = 512785182741247112;
const int mod = 1e9 + 7;
using namespace std;

void solve() {
    int n, m;
    cin >> n >> m;
    if (m > n)
    {
        cout << "NO\n";
        return;
    }
    if (n == m)
    {
        cout << "YES\n";
        return;
    }
    queue<int>q;
    q.push(n);
    while (q.size())
    {
        int k = q.front();
        q.pop();
        if (k == m)
        {
            cout << "YES\n";
            return;
        }
        if (k < m|| k % 3 != 0) continue;
        int t=k / 3;
        q.push(t);
        q.push(t * 2);
        
    }
    cout << "NO\n";
}
    
signed main() {

    ios;
    TEST
    solve();
    return 0;
}

E. The Lakes

dfs染色法。

cpp 复制代码
#include<bits/stdc++.h>
#define int long long  
#define TEST int T; cin >> T; while (T--)
#define ios ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
#define pll pair<int,int>
const int N = 1e6 + 30;
const int M = 1e3 + 10;
const int inf = 512785182741247112;
const int mod = 1e9 + 7;
using namespace std;
int vis[1001][1001];
int a[1001][1001];
int q[N];
int dx[] = { 1,0,-1,0 };
int dy[] = { 0,1,0,-1 };
int n, m;
int sum = 0;
int ans = 0;
void dfs(int x, int y, int tag)
{
    vis[x][y] = tag;
    sum += a[x][y];
    ans = max(ans, sum);
    for (int i = 0; i < 4; i++)
    {
        int tx = x + dx[i];
        int ty = y + dy[i];
        if (tx<1 || ty<1 || tx>n || ty>m) continue;
        if (a[tx][ty] == 0) continue;
        if (vis[tx][ty]) continue;
        dfs(tx, ty, tag);
    }
}
void solve() {
   
    cin >> n >> m;
    ans = 0;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cin >> a[i][j];
        }
    }
    int cnt = 0;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            if (a[i][j] == 0) continue;
            if (vis[i][j]) continue;
            sum = 0;
            ++cnt;
            dfs(i, j, cnt);
        }
    }
    cout << ans << "\n";
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            vis[i][j] = 0;
        }
    }
}
    
signed main() {

    ios;
    TEST
    solve();
    return 0;
}

F. Forever Winter

模拟一下,与出入度为1的点连接的就是与主节点连接的点,输出他们的出入度即可,与主节点连接的点,出入度需要减一。

cpp 复制代码
#include<bits/stdc++.h>
#define int long long  
#define TEST int T; cin >> T; while (T--)
#define ios ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
#define pll pair<int,int>
const int N = 1e6 + 30;
const int M = 1e3 + 10;
const int inf = 512785182741247112;
const int mod = 1e9 + 7;
using namespace std;
void solve() {
    int n, m;
    cin >> n >> m;
    vector<vector<int>>f(n + 1);
    for (int i = 1; i <= m; i++)
    {
        int u, v;
        cin >> u >> v;
        f[u].push_back(v);
        f[v].push_back(u);
    }
    set<int>q;
    for (int i = 1; i <= n; i++)
    {
        if (f[i].size() == 1)
        {
            q.insert(f[i][0]);
        }
    }
    int ans1;
    for (auto x : q)
    {
        ans1 = f[x].size() - 1;
        break;
    }
    int ans2;
    for (int i = 1; i <= n; i++)
    {
        set<int>tt;
        for (auto x : f[i]) tt.insert(x);
        if (tt == q)
        {
            ans2 = f[i].size();
            break;
        }
    }
    cout << ans2 << ' ' << ans1 << "\n";
}

signed main() {

    ios;
    TEST
        solve();
    return 0;
}

G. Hits Different

一开始用搜索,优化半天还是超时,正解是二位数组前缀和dp。

cpp 复制代码
#include<bits/stdc++.h>
#define int long long  
#define TEST int T; cin >> T; while (T--)
#define ios ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
#define pll pair<int,int>
const int N = 1e6 + 30;
const int M = 1e3 + 10;
const int inf = 512785182741247112;
const int mod = 1e9 + 7;
using namespace std;
int a[1500][1500];
int cnt=1;
int ans[N];
void solve() {
    int n;
    cin >> n;
    cout << ans[n] << "\n";
}
    
signed main() {


    for (int i = 1; i < 1500; i++)
    {
        for (int j = i - 1; j >= 1; j--)
        {
            a[j][i - j] = a[j - 1][i - j] + a[j][i - j - 1] - a[j - 1][i - j - 1] + cnt * cnt;
            ans[cnt] = a[j][i - j];
            cnt++;
        }
    }
    ios;
    TEST
    solve();
    return 0;
}

H. Don't Blame Me

找子序列所有元素的与和的二进制里1的个数等于k,数位dp。

cpp 复制代码
#include<bits/stdc++.h>
#define int long long  
#define TEST int T; cin >> T; while (T--)
#define ios ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
#define pll pair<int,int>
const int N = 1e6 + 30;
const int M = 1e3 + 10;
const int inf = 512785182741247112;
const int mod = 1e9 + 7;
using namespace std;
int dp[N][64];
int a[N];
void solve()
{
	int n, k;
	cin >> n >> k;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
	}
	for (int i = 1; i <= n; i++)
	{
		dp[i][a[i]] = 1;
		for (int j = 0; j <= 63; j++)
		{
			dp[i][j] = (dp[i][j] + dp[i - 1][j]) % mod;
			dp[i][j & a[i]] = (dp[i][j & a[i]] + dp[i - 1][j]) % mod;
		}
	}
	int ans = 0;
	for (int i = 0; i <= 63; i++)
	{
		int cnt = 0;
		for (int j = 0; j < 6; j++)
		{
			if ((i >> j) & 1)
			{
				cnt++;
			}
		}
		if (cnt == k)
		{
			ans = (ans + dp[n][i]) % mod;
		}
	}
	cout << ans << "\n";

	for (int i = 1; i <= n; i++)
	{
		for (int j = 0; j <= 63; j++)
		{
			dp[i][j] = 0;
		}
	}
}
signed main() {

    ios;
    TEST
        solve();
    return 0;
}
相关推荐
Alidme几秒前
cs106x-lecture14(Autumn 2017)-SPL实现
c++·学习·算法·codestepbystep·cs106x
小王努力学编程1 分钟前
【算法与数据结构】单调队列
数据结构·c++·学习·算法·leetcode
最遥远的瞬间3 分钟前
15-贪心算法
算法·贪心算法
ZxsLoves13 分钟前
【【Systemverilog学习参考 简单的加法器验证-含覆盖率】】
学习·fpga开发
明阳mark36 分钟前
Ansible 学习笔记
笔记·学习·ansible
维齐洛波奇特利(male)1 小时前
(动态规划 完全背包 **)leetcode279完全平方数
算法·动态规划
~kiss~1 小时前
python的thrift2pyi学习
windows·python·学习
Evaporator Core1 小时前
MATLAB学习之旅:数据建模与仿真应用
开发语言·学习·matlab
Zfox_1 小时前
【QT】信号与槽 & 窗口坐标
开发语言·c++·qt·qt5
大米洗澡1 小时前
数字签名技术基础
python·学习·程序人生·面试·职场和发展