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;
}