zzythebest

博客园 首页 新随笔 联系 订阅 管理
#include <bits/stdc++.h>

using namespace std;

int tc, n;
string s;

int main(){
    ios::sync_with_stdio(false); cin.tie(0);

    cin >> tc;
    while(tc--){
        cin >> n >> s;
        int res = n - 1;
        for(int i = 0; i < n - 2; i++){
            if(s[i] == s[i + 2]) res--;
        }
        cout << res << '\n';
    }
    return 0;
}
  • 1500 贪心 https://codeforces.com/contest/1154/problem/D
    题意:
    现在有一个机器人,他有两块电池,电池和蓄电池,其中电池不可以充电,蓄电池可以充电。
    现在有n段长度为1的路si, si=1表示这段路有阳光,可以给蓄电池充电,si=0表示这段路没有阳光,不可以给蓄电池充电。
    走一段路就要消耗1点电量,当走过有阳光的路,如果用电池,那么蓄电池就可以充电,电池电量-1,蓄电池电量+1;如果用蓄电池,那么蓄电池电量-1,不会充电。
    走过没阳光的路,随便用那个电池,电量-1;
    蓄电池的电量不能超过初始值(最大值)。也就是充满了就充不进去了。
    现在问最多能走几段路?

思路:
贪心
如果能给蓄电池充电,就给蓄电池充电,否则就尽可能地放电池的电。

#include <bits/stdc++.h>

using namespace std;

const int N = 2 * (int)1e5 + 100;
int n, b, a, s[N];

int main(){
    ios::sync_with_stdio(false); cin.tie(0);

    cin >> n >> b >> a;
    int a_max = a;
    for(int i = 1; i <= n; i++) cin >> s[i];
    int res = 0;
    for(int i = 1; i <= n; i++){
        if(!s[i]){ //没有太阳,优先用蓄电池的电
            if(a) a--;
            else if(b) b--;
            else break;
        } else{ //有太阳
            if(a < a_max){ //冲的进电,优先用电池
                if(b) {b--; a++;}
                else if(a) a--;
                else break;
            } else{ //充不进电,优先用蓄电池
                assert(a == a_max);
                if(a) a--;
                else if(b) b--;
                else break;
            }
        }
        res = i;
    }
    cout << res << '\n';
    return 0;
}
  • 1800 构造题 https://codeforces.com/contest/1328/problem/D
    题意:
    有不同种类的动物围成一圈,现在要用尽可能少种类的颜色来染色,有一个限制,就是如果两个相邻的动物种类不一样,那么他们染色的颜色也要不一样。
    现在问最少能用几种颜色染色,并输出其中一种可能的染色。

思路:
分类讨论。

  1. 如果动物的种类都一样,那么就用一种颜色就可以染色
  2. 有动物颜色不一样
    2/1 长度为偶数,用2种颜色,1 2 1 2.。。
    2/2 长度为奇数
    2/2/1 t[0] == t[n - 1],首位相同,可以染成同一个颜色,1 2 1 2.。。
    2/2/2 t[0] != t[n - 1],不可以染成同一个颜色
    2/2/2/1 围成一圈的动物中,有两个同一种类的动物相邻,那么这两个动物可以染成同一个颜色,其他动物还是1 2 1 2。。染色
    2/2/2/2 不存在两个同一种类的动物相邻,那就需要3种颜色染色,1 2 1 2 .。。 3
#include <bits/stdc++.h>

using namespace std;

const int N = 2 * (int)1e5 + 100;
int q, n, t[N];

int main(){
    ios::sync_with_stdio(false); cin.tie(0);

    cin >> q;
    while(q--){
        cin >> n;
        for(int i = 0; i < n; i++) cin >> t[i];
        //case 1
        bool all_same = true;
        for(int i = 1; i < n; i++) all_same &= (t[i] == t[i - 1]); 
        if(all_same){
            cout << 1 << '\n';
            for(int i = 0; i < n; i++) cout << 1 << ' ';
            cout << '\n';
            continue;
        }
        //case 2
        if(n % 2 == 0){
            cout << 2 << '\n';
            int now = 0;
            for(int i = 0; i < n; i++){
                cout << now + 1 << ' ';
                now ^= 1;
            } cout << '\n';
            continue;
        }
        //case 3
        assert(n % 2 == 1);
        if(t[0] == t[n - 1]){  //首位相同,染色可以相同
            cout << 2 << '\n';
            int now = 0;
            for(int i = 0; i < n; i++){
                cout << now + 1 << ' ';
                now ^= 1;
            } cout << '\n';
        } else{             //首位不同,染色一定不同
            int p = -1;
            for(int i = 1; i < n; i++){
                if(t[i] == t[i - 1]){
                    p = i;
                    break;
                }
            }
            if(p == -1){
                cout << 3 << '\n';
                int now = 0;
                for(int i = 0; i < n - 1; i++){
                    cout << now + 1 << ' ';
                    now ^= 1;
                } 
                cout << 3;
                cout << '\n';
            } else{
                cout << 2 << '\n';
                int now = 0;
                for(int i = 0; i < n; i++){
                    cout << now + 1 << ' ';
                    if(i != p - 1) now ^= 1;
                } cout << '\n';
            }
        }
    }
    return 0;
}
posted on 2023-03-27 16:36  whoamiiii  阅读(48)  评论(0)    收藏  举报