http://codeforces.com/contest/1335
A.
题意:给出一个数n 问有多少种方法构成n = a + b 并且 a > b
思路:签到 /2就行 如果是偶数的话 还要-1 因为不能相等
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define mes(x,a) memset(x,a,sizeof(x));
#define sca(a) scanf("%d",&a)
#define lowbit(x) x & (-x)
#define fi first
#define se second
#define pii pair<int, int>
inline int read()
{
int x=0,flag_read=1;
char c=getchar();
while(c<'0'||c>'9')
{
if(c=='-')
flag_read=-1;
c=getchar();
}
while(c>='0'&&c<='9')
{
x=(x<<3)+(x<<1)+c-'0';
c=getchar();
}
return x*flag_read;
}
const double eps=1e-9;
const double pi=acos(-1);
const int N = 3e5+5;
const int M = 1e7+5;
const int INF = 0x3f3f3f3f;
const int mod=2e6+5;
int main()
{
int t ;
cin >> t;
while(t--)
{
LL n;
cin >> n;
LL res = n/2;
if(n%2 == 0)
res -- ;
cout << res << '\n';
}
return 0;
}
B.
题意:构造一个字符串 使得长度为n的字符串使得每个长度为a的区间内 有b个不同的字符
思路:范围只有26 暴力构造就好
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define mes(x,a) memset(x,a,sizeof(x));
#define sca(a) scanf("%d",&a)
#define lowbit(x) x & (-x)
#define fi first
#define se second
#define pii pair<int, int>
inline int read()
{
int x=0,flag_read=1;
char c=getchar();
while(c<'0'||c>'9')
{
if(c=='-')
flag_read=-1;
c=getchar();
}
while(c>='0'&&c<='9')
{
x=(x<<3)+(x<<1)+c-'0';
c=getchar();
}
return x*flag_read;
}
const double eps=1e-9;
const double pi=acos(-1);
const int N = 3e5+5;
const int M = 1e7+5;
const int INF = 0x3f3f3f3f;
const int mod=2e6+5;
int main()
{
int t ;
cin >> t;
while(t--)
{
int n ,a ,b ;
cin >> n >> a >> b ;
string res = "";
int i ;
for(i = 'a';i < 'a' + b;i ++)
res += i;
i -- ;
for(int j = 0;j < a - b;j ++)
res += i;
int k = n/a;
int d = n%a;
for(int i = 0;i < k;i ++)
cout << res ;
for(int i = 0;i < d;i ++)
cout << res[i] ;
cout << '\n';
}
return 0;
}
C.
题意:给出n个人 每个人只能被使用一次 希望构建出两个队伍 一个队伍全部数值相等 一个队伍全部数值不等 问队伍最大规模是多少
思路:计算不同数字个数sum 和 最大相同数字的个数ma sum里是包含ma的 如果相等的话那么答案就是sum-1 因为要减去ma也包括在内 如果ma更大的话 很明显答案就是 sum ma可以给出一个给sum
如果是sum更大的话 答案就是ma
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define mes(x,a) memset(x,a,sizeof(x));
#define sca(a) scanf("%d",&a)
#define lowbit(x) x & (-x)
#define fi first
#define se second
#define pii pair<int, int>
inline int read()
{
int x=0,flag_read=1;
char c=getchar();
while(c<'0'||c>'9')
{
if(c=='-')
flag_read=-1;
c=getchar();
}
while(c>='0'&&c<='9')
{
x=(x<<3)+(x<<1)+c-'0';
c=getchar();
}
return x*flag_read;
}
const double eps=1e-9;
const double pi=acos(-1);
const int N = 2e5+5;
const int M = 1e7+5;
const int INF = 0x3f3f3f3f;
const int mod=2e6+5;
int vis[N];
int a[N];
int main()
{
int t ;
cin >> t;
while(t--)
{
memset(vis,0,sizeof(vis));
int sum = 0;
int n;
cin >> n;
int ma = 0;
for(int i = 0;i < n;i ++)
{
cin >> a[i];
if(!vis[a[i]])
sum ++ ;
vis[a[i]] ++ ;
ma = max(ma, vis[a[i]]);
}
if(n == 1)
{
cout << 0 << '\n';
continue;
}
if(n == 2)
{
cout << 1 << '\n';
continue;
}
int res = 0;
if(ma == sum)
res = ma - 1;
else
{
if(ma > sum)
res = sum ;
else
{
res = ma;
if(ma == 1)
res = 1;
}
}
cout << res << '\n';
}
return 0;
}
D.
题意:给你9x9的数独 让你最多修改9次 使得它变成非数独 即每行 每列 3x3 均有数字出现两次
思路:考虑到数独的特点 并且只允许修改九次 那么我们修改九次 不同行 不同列 不同块的数值 使他们的值+1 来满足条件 注意不能超过10
可以直接用数组模拟 记得输入的时候用%1d 不然程序会认为每一行就只是一个数字
#include <bits/stdc++.h>
using namespace std;
int a[15][15];
int main()
{
int t;
cin >> t;
while(t --)
{
for(int i = 1; i <= 9; i ++)
{
for(int j = 1; j <= 9; j ++)
{
scanf("%1d",&a[i][j]);
}
}
a[1][1] = a[1][1] % 9 + 1;
a[2][4] = a[2][4] % 9 + 1;
a[3][7] = a[3][7] % 9 + 1;
a[4][2] = a[4][2] % 9 + 1;
a[5][5] = a[5][5] % 9 + 1;
a[6][8] = a[6][8] % 9 + 1;
a[7][3] = a[7][3] % 9 + 1;
a[8][6] = a[8][6] % 9 + 1;
a[9][9] = a[9][9] % 9 + 1;
for(int i = 1; i <= 9; i ++)
{
for(int j = 1; j <= 9; j ++)
{
cout << a[i][j] ;
}
cout << '\n';
}
}
return 0;
}
E1.
题意:给出长度为n的序列 要你构造一个类似 “xyx”的回文序列 x,y块内的数字均要相同
思路:观察到值均小于26 我们构造一个前缀qij代表前i个数里有多少个j构造一个后缀代表后i个数里有多少个j 之后暴力遍历计算答案 复杂度nn26
#include <bits/stdc++.h>
using namespace std;
const int N = 2005;
int a[N],vis[N],ql[N][30],qr[N][30];
void init()
{
memset(vis,0,sizeof vis);
memset(ql,0,sizeof(ql));
memset(qr,0,sizeof(qr));
}
int main()
{
int t;
ios::sync_with_stdio(false);
cin >> t;
while(t --)
{
int n ;
init();
cin >> n ;
for(int i = 1;i <= n;i ++)
cin >> a[i];
for(int i = 1;i <= n;i ++)
{
for(int j = 1;j <= 26;j ++)
ql[i][j] = vis[j];
vis[a[i]] ++ ;
}
memset(vis,0,sizeof(vis));
for(int i = n;i >= 1;i --)
{
for(int j = 1;j <= 26;j ++)
qr[i][j] = vis[j];
vis[a[i]] ++ ;
}
int res = 0;
int cnt = 0;
for(int i = 1;i <= n;i ++)
{
cnt = 0;
for(int j = i;j <= n;j ++)
{
if(a[i] == a[j])///中间块的规模
cnt ++ ;
for(int k = 1;k <= 26;k ++)
res = max(res,cnt + min(ql[i][k] , qr[j][k]) * 2);//左右块的规模 乘2是因为左右都要有
}
}
cout << res << '\n';
}
return 0;
}