社区讨论

ABC C & D HELP

学术版参与者 5已保存回复 6

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
6 条
当前快照
1 份
快照标识符
@mm6dslu8
此快照首次捕获于
2026/02/28 21:53
上周
此快照最后确认于
2026/03/03 11:30
上周
查看原帖
RT.
C WA * 1 ,D WA * 18
C 没啥好说的,D 贪心越早的 B 越早删,能删就删。
CPP
#include <bits/stdc++.h>

#define int long long
#define up(i ,x ,y) for(int i = x ; i <= y ; i ++)
#define dn(i ,x ,y) for(int i = x ; i >= y ; i --)
#define inf 1e18

using namespace std;

inline int read() {int x = 0 ; char c = getchar() ; bool w = 0;while(c < '0' || c > '9') {	w |= (c == '-') ,c = getchar();} while(c >= '0' && c <= '9') {x = (x << 1) + (x << 3) + (c ^ 48) ,c = getchar();} return w ? -x : x;} 
inline void write(int x) {if(x < 0) x = -x ,putchar('-');if(x > 9) write(x / 10);putchar(x % 10 | 48);}
inline void writesp(int x) {write(x) ,putchar(' ');}
inline void writeln(int x) {write(x) ,putchar('\n');}

const int N = 3e5 + 10;
int ans;

namespace lolcrying{
	
	signed main(){
		string s ,t;
		cin >> s >> t;
		int n = s.size() ,m = t.size();
		
		int p1 = 0 ,p2 = 0 ;
		while(s[p1] == 'A' && p1 < n) ++ p1 ; while(t[p2] == 'A' && p2 < m) ++ p2;

		ans += abs(p1 - p2);
// cout<<p2<<endl;
        if(p2 == m && p1 != n) {puts("-1") ; return 0 ;}
        if(p1 == n && p2 != m) {puts("-1") ; return 0 ;}
        
		int p = 0 ,tot = 0 ,res = 0;
		
		up(i ,p2 ,m - 1) {
			// cout << i << ' ' << t[i] << endl;
			while(s[p] != t[i] && p < n) {
                if(s[p] != 'A') {puts("-1") ; return 0 ; }
                ++ p ;
            }
			if(p >= n) {puts("-1") ; return 0 ;}
			++ p ; while(s[p] == 'A' && p < n) ++ res ,++ p ;
			tot = 0 ;
			while(t[i + 1] == 'A' && i < m) ++ i ,++ tot;
			ans += abs(tot - res) ;
            res = 0 ;
			
		} writeln(ans);
		
		return 0 ;
	}
} signed main() {
	
	int T = 1;
	while(T --) lolcrying :: main();
	
	return 0;
}
CPP
#include <bits/stdc++.h>

#define int long long
#define up(i ,x ,y) for(int i = x ; i <= y ; i ++)
#define dn(i ,x ,y) for(int i = x ; i >= y ; i --)
#define inf 1e18

using namespace std;

inline int read() {int x = 0 ; char c = getchar() ; bool w = 0;while(c < '0' || c > '9') {	w |= (c == '-') ,c = getchar();} while(c >= '0' && c <= '9') {x = (x << 1) + (x << 3) + (c ^ 48) ,c = getchar();} return w ? -x : x;} 
inline void write(int x) {if(x < 0) x = -x ,putchar('-');if(x > 9) write(x / 10);putchar(x % 10 | 48);}
inline void writesp(int x) {write(x) ,putchar(' ');}
inline void writeln(int x) {write(x) ,putchar('\n');}

const int N = 1e6 + 10;
int n ,tr[N] ,nxt[N] ,prea[N] ,succ[N] ,ans;
string s;

namespace lolcrying{
	#define lowbit(i) (i & -i)
	inline void add(int x ,int v){for(int i = x ; i <= n + 1 ; i += lowbit(i)) tr[i] += v;}
    inline int query(int x){int res = 0 ;for(int i = x ; i ; i -= lowbit(i)) res += tr[i] ; return res;}
    
    signed main(){
		cin >> s ; n = s.size() ; s = ' ' + s;

        up(i ,1 ,n) prea[i] = prea[i - 1] + (s[i] == 'A');
        dn(i ,n ,1) succ[i] = succ[i + 1] + (s[i] == 'C');
        
        int lst = n + 1;
        
        dn(i ,n ,1) {
            nxt[i] = lst;
            if(s[i] == 'C') lst = i ,add(n - i + 1 ,1);
        }

        int delta = 0 ;
        up(i ,1 ,n) {
            if(s[i] == 'B') {
                int c = ((prea[i] - delta > 0 && query(n - i + 1) > 0) ? 1 : 0);
                // cout << prea[i] - delta << ' ' << query(n - i + 1) << endl;
                if(nxt[i] != n + 1 && c) add(n - nxt[i] + 1 ,-1);
                ans += c ,delta += c;
            }
        } writeln(ans);
        
		return 0 ;
	}
} signed main() {
	
	int T = 1;
	while(T --) lolcrying :: main();
	
	return 0;
}

回复

6 条回复,欢迎继续交流。

正在加载回复...