A:4482. 分组
关键词:贪心
题意:将一个数组分成尽可能少的数组,并且每个数组内数字不重复
思路:贪心,即求出现最多数的出现次数
AC代码:
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 110;
int n;
int cnt[N];
int main()
{
cin >> n;
int x;
int res =0;
for (int i = 0; i < n; i ++)
{
cin >> x;
cnt[x] ++;
res = max(res , cnt[x]);
}
cout << res <<endl;
return 0;
}
关键词:贪心、双指针、二分
题意:若两人权值相差<=k 且不相等,则可以删除权值小的那个人,给定一批人的权值,求最后最少能剩几人
思路:贪心,排序后从小到大进行淘汰,利用双指针进行维护一个a[j]使得a[j]为最小的 x ,x 刚好满足x - a[i] > k
AC代码:
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 200010;
int n,k;
int a[N];
int main( )
{
cin >> n >> k;
for (int i = 0; i < n; i ++)
cin >> a[i];
sort(a, a + n);
int res = n;
for (int i = 0, j = 0; i < n; i ++)
{
while (j < n && a[j] - a[i] <= k) j ++;
if (a[j - 1] != a[i]) res --;
}
cout << res <<endl;
return 0;
}
关键词:数论
题意:给定三个整数 p,q,b,请你计算十进制表示下的 p/q 的结果在 b 进制下是否为有限小数。
思路:整数部分小数部分进制间转换
AC代码:
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
LL gcd(LL a, LL b)
{
return b ? gcd(b, a % b) : a;
}
int main()
{
int T;
scanf("%d", &T);
while (T -- )
{
LL p, q, b;
scanf("%lld%lld%lld", &p, &q, &b);
LL d = gcd(p, q);
q /= d;
while (q > 1)
{
d = gcd(q, b);
if (d == 1) break;
while (q % d == 0) q /= d;
}
if (q == 1) puts("YES");
else puts("NO");
}
return 0;
}