codeforces #797 div3

在这里插入图片描述
A 签到

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

using namespace std;

void solve()
{
    int n;
    cin >> n;
    
    int a = n / 3;
    n %= 3;
    if(n == 2) cout << a + 1 << " " <<  a + 2 << " " << a - 1;
    else if(n == 1) cout << a << " " << a + 2 << " " << a - 1;
    else cout << a << " " <<  a + 1 << " " << a - 1; 
    
    puts("");
}
int main()
{
    int t;
    cin >> t;
    
    while (t --) solve();
    return 0;
}

B签到

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

using namespace std;
const int N = 2e5 + 5;
int a[N],b[N];

void solve()
{
    int n;
    cin >> n;
    
    for (int i = 1; i <= n; i ++ ) cin >> a[i];
    for (int i = 1; i <= n; i ++ ) cin >> b[i];
    bool ok = true;
    int k = 0,last = -1;
    
    for(int i = 1;i <= n;i ++)
        if(b[i] == 0) k = max(a[i],k);
        else {
            if(a[i] < b[i]) ok = false;
            if(last == -1) last = a[i] - b[i];
            else if(last != a[i] - b[i]) ok = false;
        }
    if(k > last && last != -1) ok = false;
    if(ok) puts("Yes");
    else puts("No");
}
int main()
{
    int t;
    cin >> t;
    
    while (t --) solve();
    return 0;
}

C签到

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

using namespace std;
const int N = 2e5 + 5;
int a[N],b[N];

void solve()
{
    int n;
    cin >> n;
    
    for (int i = 1; i <= n; i ++ ) cin >> a[i];
    for (int i = 1; i <= n; i ++ ) cin >> b[i];
    
    vector<int> c(1);
    c[0] = 1;
    c.push_back(b[1] - a[1]);
    //cout << b[1] << " " << a[1] <<endl;
    
    for(int i = 2;i <= n;i ++){
        if(a[i] > b[i - 1]) c.push_back(b[i] - a[i]);
        else if(a[i] <= b[i - 1]) c.push_back(max(0,b[i] - b[i - 1]));
        
        //cout << a[i] << " " << b[i - 1] << endl;
    }
    
    for(int i = 1;i <= n;i ++)
        cout << c[i] << " ";
    
    cout << endl;
}
int main()
{
    int t;
    cin >> t;
    
    while (t --) solve();
    return 0;
}

D签到

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

using namespace std;
const int N = 2e5 + 5;
char s[N];
int g[N];

void solve()
{
    int n,k;
    cin >> n >> k;
    int cnt = 0,ans = 0;
    
    for(int i = 1;i <= n;i ++){
        cin >> s[i];
        if(s[i] == 'B') cnt ++,ans = max(ans,cnt);
        else cnt = 0;
        g[i] = g[i - 1] + (s[i] == 'W');
    }
   // cout << ans << " " << k << endl; 
    if(ans >= k) {
        puts("0");
        return ;
    }
    
    cnt = k;
    for(int i = 1;i + k - 1 <= n;i ++)
        cnt = min(cnt,g[i + k - 1] - g[i - 1]);
    cout << cnt << endl;    
}
int main()
{
    int t;
    cin >> t;
    
    while (t --) solve();
    return 0;
}

E贪心
一个数对答案的贡献是x/k,额外贡献为每个数的余数
将每个数的余数按照大小排序
小的尽量跟打的进行配对

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

using namespace std;
typedef long long LL;

const int N = 2e5 + 5;
int a[N];
int t[1001];

void solve()
{
    int n,k;
    cin >> n >> k;
    LL ans = 0;
    
    for(int i = 1;i <= n;i ++) {
        cin >> a[i];
        ans += a[i] / k;
        a[i] %= k;
        t[a[i]] ++;
    }
    // cout << ans << endl;
    // for(int i = 1;i <= n;i ++) cout << a[i] << " ";
    
    for(int i = 1;i <= n;i ++){
        if(a[i] == 0) continue;
        if(t[a[i]] <= 0) continue;
        int j = k - a[i];
        t[a[i]] --;
        while(j < k && t[j] <= 0) j ++;
        if(t[j] > 0) t[j] --,ans ++;
    }
    
    cout << ans << endl;
}

int main()
{
    int t;
    cin >> t;
    
    while (t --) solve();
    return 0;
}

F
独立环,最小循环节 —————— kmp求最小循环节

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

using namespace std;
typedef long long LL;

const int N = 2e5 + 5;
char s[N];
int p[N];
bool st[N];
int pos[N];

LL gcd(LL a, LL b)  // 欧几里得算法
{
    return b ? gcd(b, a % b) : a;
}

// 一开始错误的思路针对一个字符进行判断 --- 判断什么时候走到相同字符的位置
// 正解应该按照环来判断
// void dfs(int cnt,char a,int b)
// {
//     if(a == s[b])
//     {
//         S.insert(cnt + 1);
//         return ;
//     }
    
//     dfs(cnt + 1,a,pos[b]);
// }
int n;
int ne[N];

LL lcm(LL a,LL b){
    return a / gcd(a,b) * b;
}

void solve()
{
    cin >> n;
    
    for(int i = 1;i <= n;i ++) cin >> s[i];
    for(int i = 1;i <= n;i ++) cin >> p[i];
    for(int i = 1;i <= n;i ++) st[i] = false;
    // 3 1 
    LL ans = 1;
    for(int i = 1;i <= n;i ++)
        if(!st[i]){
            string now = " ";
            int len =  0;
            int u = i;
            while(!st[u]){
                st[u] = true;
                now += s[u];
                u = p[u];
                len ++;
            }
            
            for(int i = 2,j = 0;i <= len;i ++){
                while(j && now[i] != now[j + 1]) j = ne[j];
                if(now[i] == now[j + 1]) j ++;
                ne[i] = j;
            }   
            int x = len - ne[len];
            if(len % x == 0) ans = lcm(ans,x);
            else ans = lcm(ans,len);
        }
    cout << ans << endl;    
}

int main()
{
    int t;
    cin >> t;
    
    while (t --) solve();
    return 0;
}

G
题目需要一种数据结构 集合可以支持排序,可以支持O(1)插入,删除,动态修改集合的大小; 用map模拟即可

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

using namespace std;
const int N = 2e5 + 5;
int a[N];

void solve()
{
    int n,k;
    cin >> n >> k;
    map<int,int> mp;
    
    for(int i = 1;i <= n;i ++) cin >> a[i];
    function<void (int,int)> add = [&mp](int i,int x){
        mp[i] = x;
        auto it = mp.find(i);
        if(it != mp.begin() && prev(it) -> second <= x) {
            mp.erase(it);
            return ;
        }
        
        while(next(it) != mp.end() && next(it) -> second >= x)
            mp.erase(next(it));
    };
    
    for(int i = 1;i <= n;i ++)
        add(i,a[i]);
    
    while(k --){
        int pos,x;
        cin >> pos >> x;
        a[pos] -= x;
        add(pos,a[pos]);
        
        
        cout << mp.size() << " " ;
    }    
    puts("");
}

int main()
{
    int t;
    cin >> t;
    
    while(t --) solve();
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值