B. M-arrays

B. M-arrays

B.M-阵列

time limit per test: 1 second
每次测试的时间限制:1秒

memory limit per test: 256 megabytes
每次测试的内存限制:256兆字节

input: standard input
输入:标准输入

output: standard output
产出:标准产出

You are given an array a1, a2,…, an consisting of n positive integers and a positive integer m.
给你一个数组A1,a2,.,一个由n个正整数和一个正整数m组成的数组。

You should divide elements of this array into some arrays. You can order the elements in the new arrays as you want.
您应该将这个数组的元素划分为一些数组。您可以根据需要对新数组中的元素排序。

Let’s call an array m-divisible if for each two adjacent numbers in the array (two numbers on the positions i andi + 1 are called adjacent
如果对于数组中的每两个相邻数,我们调用一个m-可除的数组(i和di+1位置上的两个数字称为相邻的)。

for each i) their sum is divisible by m. An array of one element is m-divisible.
对于每个i),它们的和可被m整除。一个元素的数组是m-可除的。

Find the smallest number of m-divisible arrays that a1, a.,…, an is possible to divide into.
找到最小数目的m-可除数组,其中a1,a,.an是可以划分的。

Input
输入

The first line contains a single integert (1 < t≤1000)一the number of test cases.
第一行包含一个整数(1<t≤1000),一测试用例数。

The first line of each test case contains two integersn, m(1≤n≤10,1≤m≤105).
每个测试用例的第一行包含两个整数,m(1≤n≤10,1≤m≤105)。

The second line of each test case contains n integers a,a2,…,an(1≤qi≤109).
每个测试用例的第二行包含n个整数a,a2,.,an(1≤qi≤109)。

It is guaranteed that the sum of n and the sum of m over all test cases do not exceed 10.
保证所有测试用例的n和m和不超过10。

Output
输出量

For each test case print the answer to the problem.
对于每个测试用例,打印出问题的答案。

余数系统一组,分组时互补的记一个

int main()
{
    int t, n, m, x;
    cin>>t;
    while (t--)
    {
        int sum = 0;
        memset(a, 0, sizeof(a));
        cin>>n>>m;
        for (int i = 0; i < n; ++i)
        {
            cin>>x;
            ++a[x % m];
        }
        if (a[0] != 0)
            ++sum;
        for (int i = 1; i <= m / 2; ++i)
        {

            if (a[i] == a[m - i]&&a[i])
                ++sum;
            else
                sum += abs(a[i] - a[m - i]);
        }
        cout<<sum<<endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为了解决这个问题,我们可以先对两个数组进行排序,然后用双指针法来找到所有相似的子数组。具体实现如下: ```c #include <stdio.h> #include <stdlib.h> #define MAX_N 1000000 int M[MAX_N], N[MAX_N]; int m, n; int compare(const void *a, const void *b) { return *(int*)a - *(int*)b; } int count_similar() { int i = 0, j = 0; int count = 0; while (i < m && j < n) { if (M[i] == N[j]) { // 找到相同的元素 int k = j; while (k < n && N[k] == N[j]) { k++; } count += k - j; // 加上与N[j]相同的元素的个数 i++; j = k; } else if (M[i] < N[j]) { // M[i]较小,需要增加N中的元素 i++; } else { // M[i]较大,需要减少N中的元素 j++; } } return count; } int main() { scanf("%d%d", &m, &n); for (int i = 0; i < m; i++) { scanf("%d", &M[i]); } for (int i = 0; i < n; i++) { scanf("%d", &N[i]); } qsort(M, m, sizeof(int), compare); qsort(N, n, sizeof(int), compare); printf("%d\n", count_similar()); return 0; } ``` 首先读入两个数组的长度和元素,然后对它们进行排序。然后用双指针法找到所有相似的子数组,具体方法如下: 1. 初始化两个指针i和j,分别指向M和N的开头; 2. 如果M[i]等于N[j],说明找到了一个相似的子数组,接着向右移动j,直到N[j]不等于N[j+1]; 3. 如果M[i]小于N[j],说明N[j]需要增加,所以向右移动i; 4. 如果M[i]大于N[j],说明N[j]需要减少,所以向右移动j; 5. 重复2~4步,直到i到达M的末尾或j到达N的末尾。 遍历过程中,我们用一个计数器count来累计相似的子数组的个数,每当找到一个相似的子数组时,我们就把与N[j]相同的元素的个数加到count中。最后返回count即可。 时间复杂度为O(MlogM + NlogN),空间复杂度为O(M + N)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值