Problem - C - Codeforces
时间复杂度 O ( n + q ) 时间复杂度O(n + q) 时间复杂度O(n+q)
时间复杂度 O ( n + q l o g q ) 时间复杂度O(n+qlogq) 时间复杂度O(n+qlogq)
#include
using namespace std;
#define fi first
#define se second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define int long long
const int INF = 9e18;
const int N = 2e5 + 10;
const int mod = 1e9 + 7;
typedef pair<int,int>PII;
int n , m , t;
string s , res;
/*
000100010
*/
signed main(){
IOS
cin >> t;
while(t --) {
set<pair<int , int>>st;
cin >> n >> m >> s;
s = '?' + s;
vector<int>cnt(n + 1) , cntl(n + 1) , cntr(n + 1);
for(int i = 1 ; i <= n ; i ++) {
cnt[i] = cnt[i - 1] + (s[i] == '0');
}
int now = 0;
for(int i = 1 ; i <= n ; i ++) {
cntl[i] = now;
if(s[i] == '1') now = 0;
if(s[i] == '0') now += 1;
}
now = 0;
for(int i = n ; i >= 1 ; i --){
cntr[i] = now;
if(s[i] == '1') now += 1;
if(s[i] == '0') now = 0;
}
int l , r;
for(int i = 1 ; i <= m ; i ++) {
cin >> l >> r;
int cntt = cnt[r] - cnt[l - 1];
if(cntt == cnt[l - 1 + cntt] - cnt[l - 1]) {
st.insert({-1 , -1});
} else {
st.insert({l - cntl[l] , r + cntr[r]});
}
}
cout << st.size() << "\n";
}
return 0;
}
//freopen("文件名.in","r",stdin);
//freopen("文件名.out","w",stdout);
Problem - A - Codeforces
#include
using namespace std;
#define fi first
#define se second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define int long long
const int INF = 9e18;
const int N = 2e6 + 10;
const int mod = 1e9 + 7;
typedef pair<int,int>PII;
string s , t;
signed main(){
cin >> s >> t;
int n = s.size();
for(int i = n - 1 ; i >= 0 ; i --) {
if(s[i] == 'z') {
s[i] = 'a';
} else {
s[i] = char(s[i] + 1);
break;
}
}
if(s == t) {
cout << "No such string\n";
} else {
cout << s << "\n";
}
return 0;
}
//freopen("文件名.in","r",stdin);
//freopen("文件名.out","w",stdout);
Problem - C - Codeforces
时间复杂度 O ( n 2 ) 时间复杂度O(n^2) 时间复杂度O(n2)
#include
using namespace std;
#define fi first
#define se second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define int long long
const int INF = 9e18;
const int N = 2e5 + 10;
const int mod = 1e9 + 7;
typedef pair<int,int>PII;
int n ;
string s;
bool vis[N];
vector<int>pos[30];
int pre[N] , nex[N];
signed main(){
cin >> n >> s;
s = '?' + s;
for(int i = 1 ; i <= n ; i ++) pre[i] = (i == 1) ? -1 : i - 1;
for(int i = n ; i >= 1 ; i --) nex[i] = (i == n) ? -1 : i + 1;
for(int i = 1 ; i <= n ; i ++) pos[s[i] - 'a' + 1].push_back(i);
int cnt = n;
while(cnt) {
bool tag = 0;
for(int i = 26 ; i >= 2 ; i --) {
int j = pos[i].size();
for(int k = 0 ; k < j ; k ++) {
int now = pos[i][k];
if(vis[now]) continue;
if(pre[now] != -1 && s[pre[now]] + 1 == s[now]) {
nex[pre[now]] = nex[now];
pre[nex[now]] = pre[now];
vis[now] = 1;
cnt -= 1;
tag = 1;
continue;
}
if(nex[now] != -1 && s[nex[now]] + 1 == s[now]) {
nex[pre[now]] = nex[now];
pre[nex[now]] = pre[now];
vis[now] = 1;
cnt -= 1;
tag = 1;
continue;
}
}
}
if(!tag) break;
}
cout << n - cnt << "\n";
return 0;
}
//freopen("文件名.in","r",stdin);
//freopen("文件名.out","w",stdout);
Problem - C - Codeforces
∑ 1 < = l < r < = n ∑ l < = x < y < = r ( a x = a y ) \sum_{1<=l
时间复杂度 O ( n l o g n ) 时间复杂度O(nlogn) 时间复杂度O(nlogn)
#include
using namespace std;
#define fi first
#define se second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define int long long
const int INF = 9e18;
const int N = 2e6 + 10;
const int mod = 1e9 + 7;
typedef pair<int,int>PII;
int t , n , now;
signed main(){
IOS
cin >> t;
while(t --) {
cin >> n;
int res = 0;
map<int,int>pre;
for(int i = 1 ; i <= n ; i ++) {
cin >> now;
res += pre[now] * (n - i + 1);
pre[now] += i;
}
cout << res << "\n";
}
return 0;
}
//freopen("文件名.in","r",stdin);
//freopen("文件名.out","w",stdout);