A. Politics
思路:不难发现,跟1号(领导)表决权一样的留下,不一样的滚蛋
#include <bits/stdc++.h>
#pragma GCC optimize(3,"Ofast","inline")
#pragma GCC optimize(2)
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 100010;
void solve()
{
int n, k;
cin >> n >> k;
string s;
cin >> s;
int cnt = 1;
for(int i = 1; i < n; i ++ ){
string x;
cin >> x;
if(s == x) cnt ++;
}
cout << cnt << endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);cout.tie(nullptr);
int t = 1;
cin >> t;
while(t -- ) solve();
system("pause");
return 0;
}
B. Indivisible
思路:其实写的时候我想了很多种构造方法都不太对,最后直接全排列数去check,找出了无比简单的规律。
其实道理还是挺简单的:
n是奇数由于全加起来一定会被n整除所以是-1
偶数:奇偶数相间排布保证连续的两个数不能被2整除,若按顺序奇偶排布,假设从头开始三个为一组,每组去均值之后是1 2 3,这样不合法。每个奇数和它后面的偶数交换位置,每组去均值之后是2 1 4,这样可以。这样排布,若以四个为一组,每组去均值之后都是2 1 4 3,和为10,不能被4整除……同理可证
#include <bits/stdc++.h>
#pragma GCC optimize(3,"Ofast","inline")
#pragma GCC optimize(2)
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 100010;
int n;
//2 1 4 3
//2 1 4 3 6 5
//2 1 4 3 6 5 8 7
//2 1 4 3 6 5 8 7 10 9
void solve()
{
cin >> n;
int sum = (n + 1)*n / 2;
if(n == 1){
cout << 1 <<endl;
return;
}
if(sum % n == 0){
cout << -1 << endl;
return;
}
int a[n + 1];
for(int i = 0; i < n; i ++ ) a[i] = i + 1;
for(int i = 0; i < n; i += 2){
swap(a[i], a[i + 1]);
}
for(int i = 0; i < n; i ++ ) cout << a[i] << ' ';
cout << endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);cout.tie(nullptr);
int t = 1;
cin >> t;
while(t -- ) solve();
system("pause");
return 0;
}
C. Almost Increasing Subsequence
思路:l~r的子序列长度是r-l+1,如果遇到一个almost-increasing就–。暴力一定会超时,所以需要首先预处理出来所有almost-increasing的区间(其实不用PII存也可以,因为almost-increasing的区间长度一定是3,如果有重叠就按两个区间来算)
然后就是二分查询的l和r之间有多少(m)个almost-increasing,答案就是r-l+1-m。
#include <bits/stdc++.h>
#pragma GCC optimize(3,"Ofast","inline")
#pragma GCC optimize(2)
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 200010;
int n, q;
int a[N], idx;
PII b[N];
void solve()
{
cin >> n >> q;
for(int i = 0; i < n; i ++ ) cin >> a[i];
for(int i = 2; i < n; i ++ ){
if(a[i-2]>= a[i-1] && a[i-1] >= a[i]){
b[idx].first = i-2;
b[idx++].second = i;
}
}
while(q -- ){
int l, r;
cin >> l >> r;
if(l == r){
cout << 1 << endl;
continue;
}
if(r-l == 1){
cout << 2 << endl;
continue;
}
l--,r--;
//二分出第一个大于l的b.first,第一个小于r的b.second
//判断是否在l~r这个范围内
int l1 = 0, r1 = idx - 1;
int mid1;
while(l1 < r1){
mid1 = l1 + r1 >> 1;
if(b[mid1].first < l) l1 = mid1 + 1;
else r1 = mid1;
}
int l2 = 0, r2 = idx - 1;
int mid2;
while(l2 < r2){
mid2 = l2 + r2 + 1>> 1;
if(b[mid2].second > r) r2 = mid2 - 1;
else l2 = mid2;
}
if(b[mid1].first < l) mid1 ++;
if(b[mid2].second > r) mid2 --;
int ma = 0;
if(b[mid1].second > r) ma = 0;
else if(b[mid2].first < l) ma = 0;
else{
ma = max(0, mid2 - mid1 + 1);
}
// cout << mid1 << ' ' << mid2 << ' ' << ma << endl;
cout << r - l + 1 - ma << endl;
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);cout.tie(nullptr);
int t = 1;
// cin >> t;
while(t -- ) solve();
system("pause");
return 0;
}