A:你有n个东西,全都分给两个人,确保一个人得到的a,一定比另一个人得到的b多,问:有多少种给的方式?
直接分半就行了,记得判断奇偶数
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while(t--)
{
int n;
cin >> n;
int ans = 0;
if(n % 2) ans = n / 2;
else ans = n / 2 - 1;
cout << ans << endl;
}
return 0;
}
B:给你三个数,n,a,b,要你构造一个长度为n的字符串
满足长度为a的子串中恰好有b个不同的字母。
只需要按要求把前面a个字符(第一个子串)构造出来,然后不断循环子串到n就可以了。
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
map<char,int> m;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while(t--)
{
int n,a,b;
cin >> n >> a >> b;
string s = "";
int i;
int cha = a - b;
for(i = 0; i < a; i++)
{
char c = 'a';
if(i < cha)
s += c;
else
{
s += c + i - cha;
}
}
string ans = "";
ans += s;
int z = 0;
for(int i = a; i < n; i++)
{
ans += s[z];
z++;
if(z >= a) z = 0;
}
cout << ans << endl;
}
return 0;
}
C:给你一个数组,需要你从这个数组当中拿出几个数组成两个相同人数分组,第一组要求每个数字都不相同,第二组要求所有数字都相同,问:分组的最大人数
首先记录一下有多少个不同的数字num,以及相同数字最多能有多少个maxnum,之后,就需要判断num与maxnum的关系了
为什么要判断关系?
假如num = maxnum,那么为了构成num个不同的,我一定要从maxnum相同的里面拿一个出来,那就没办法凑maxnum个相同的了, 所以最大人数就是num-1;
假如num > maxnum,那么我保留maxnum个相同的放在第二组,我可以找其他不同的来凑成maxnum个放在第一组,答案就是maxnum
假如maxnum > num,那么我先保证num个不同的放在第一组,我可以从maxnum个相同的里面拿出一个放在第一组,剩下的也足够我们放在第二组num个,答案就是num
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
const int maxn = 2e5 + 50;
int a[maxn];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while(t--)
{
map<int,int> m;
int n;
cin >> n;
int num = 0;
for(int i = 0; i < n; i++)
{
cin >> a[i];
if(!m[a[i]]) num++;
m[a[i]]++;
}
int maxnum = 0;
int ans = 0;
for(int i = 1; i <= n; i++)
{
maxnum = max(maxnum, m[a[i]]);
}
if(num == maxnum)
ans = num - 1;
else if(maxnum > num)
{
ans = num;
}
else
{
ans = maxnum;
}
//cout << num << " " << maxnum << endl;
cout << ans << endl;
}
return 0;
}
D:没玩过数独的我哭了,首先说一下数独的规则,9 * 9的矩阵,可以分成9个小的3 * 3的矩阵,要满足这9个33矩阵里面包含1,2,3,4,5,6,7,8,9各一个,其次,整个大个的9 * 9矩阵,每一行每一列也都要包含1到9各一个。
题目给你一个9 * 9的数独,让你输出一个反数独(反数独就是9个33里面有两个数字相同,9 * 9每行每列有两个数字相同)
一开始就给你一个数独,你只需要对原来的数独就行操作来打破规则就可以了。
对每一个3*3都选一个位置进行改变即可
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn = 105;
int a[maxn][maxn];
int main()
{
int t;
cin >> t;
while(t--)
{
for(int i=0;i<9;i++)
for(int j=0;j<9;j++)
scanf("%1d",&a[i][j]);
a[5][7]++;
a[6][2]++;
a[0][0]++;
a[1][3]++;
a[2][6]++;
a[3][1]++;
a[4][4]++;
a[7][5]++;
a[8][8]++;
for(int i = 0 ;i < 9; i++)
{
for(int j = 0;j<9;j++)
{
if(a[i][j] == 10)
cout << 1;
else
cout <<a[i][j];
}
cout << endl;
}
}
}
E题,直接讲E2的方法
题目给我一个数组,我从这个数组里面找子数组来组成所谓的三回文串
三回文串的意思就是:aaaaa bbb aaaaa;
整个串分为三部分,首尾的区间相同。且整个串最多两种字符;首尾一种,中间一种。
首先使用前缀和记录每种数字在每个位置的前缀个数(便于进行区间求和操作),还要用一个vector来存每个数字出现的位置
之后开始暴力循环:
第一重循环暴力首尾两个区间取啥数字好
第二重循环暴力首尾两个区间的长度
记录首尾区间取数字在数组的位置,中间剩下的数组就用来构成中间区间
第三重循环中间区间取什么数字好
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string.h>
#include <vector>
using namespace std;
const int maxn = 2e5 + 50;
int a[maxn];
int b[maxn][250];
vector<int> v[250];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while(t--)
{
int n;
cin >> n;
int ans = 0;
for(int i = 1; i <= 200; i++)
v[i].clear();
for(int i = 1; i <= n; i++)
{
cin >> a[i];
for(int j = 1; j <= 200; j++)
{
b[i][j] = b[i-1][j];
}
b[i][a[i]]++;
v[a[i]].push_back(i);
}
// cout << 1 << endl;
for(int i = 1; i <= 200; i++)
{
ans = max(b[n][i],ans);
int x = b[n][i] / 2;
for(int j = 1; j <= x; j++)
{
int s = v[i][j-1];
int e = v[i][b[n][i] - j] - 1;
if(e > s)
{
for(int q = 1; q <= 200; q++)
{
ans = max(ans,2*j + b[e][q] - b[s][q]);
}
}
}
}
cout << ans << endl;
}
return 0;
}