本文最后更新于 321 天前,其中的信息可能已经有所发展或是发生改变。
题目大意:
思想:
min(cnt, abs(t - p) + 1)
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define IOS ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
#define re register
#define fi first
#define se second
#define endl '\n'
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
const int N = 1e6 + 10;
const int INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6, PI = acos(-1);
int a[N], b[N];
void solve(){
int n; cin >> n;
int p = 0, t = 0;
for(int i = 0; i < n; i ++){
cin >> a[i];
if(a[i] == 1) p ++;
}
for(int i = 0; i < n; i ++){
cin >> b[i];
if(b[i] == 1) t ++;
}
int cnt = 0;
for(int i = 0; i < n; i ++){
if(a[i] != b[i]) cnt ++;
}
cout << min(cnt, abs(t - p) + 1) << endl;
}
int main(){
IOS;
int _ = 1;
cin >> _;
while(_ --){
solve();
}
return 0;
}
题目大意:
思想:
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define IOS ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
#define re register
#define fi first
#define se second
#define endl '\n'
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
const int N = 1e6 + 10;
const int INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6, PI = acos(-1);
LL gcd(LL a, LL b){
return b ? gcd(b, a % b) : a;
}
int a[N];
void solve(){
int n; cin >> n;
for(int i = 0; i < n; i ++) cin >> a[i];
bool flag = 1;
if(n > 2){
int b = a[0] * a[1] / gcd(a[0], a[1]);
for(int i = 1; i + 1 < n; i ++){
int t = a[i] * a[i + 1] / gcd(a[i], a[i + 1]);
if(gcd(b, t) != a[i]){
flag = 0;
break;
}
else b = t;
}
}
if(flag) cout << "YES" << endl;
else cout << "NO" << endl;
}
int main(){
IOS;
int _ = 1;
cin >> _;
while(_ --){
solve();
}
return 0;
}
题目大意:
思想
i
和 j
,i
表示以i
为起点的序列的数量,j
表示从i
开始的最长序列的位置。j
是第一个不符合条件的位置时,则当前符合条件的区间长度为 j - i
。i
向右走一格,显然起点后移,原来符合条件的位置同样符合条件,因此此时 j
不需要左移,继续右移即可。j
不满足条件时区间长度之和即可。代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define IOS ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
#define re register
#define fi first
#define se second
#define endl '\n'
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
const int N = 1e6 + 3;
const int INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6, PI = acos(-1);
int a[N];
void solve(){
int n; cin >> n;
for(int i = 1; i <= n; i ++) cin >> a[i];
LL sum = 0;
for(int i = 1, j = 1; i <= n; i ++){
while(j <= n && a[j] >= j - i + 1){
sum += j - i + 1;
j ++;
}
}
cout << sum << endl;
}
int main(){
IOS;
int _ = 1;
cin >> _;
while(_ --){
solve();
}
return 0;
}