D. Magical Array(思维)

原题链接:Problem - D - Codeforces

目录

题意:

思路:

代码:


Eric has an array bb of length mm, then he generates nn additional arrays c1,c2,…,cnc1,c2,…,cn, each of length mm, from the array bb, by the following way:

Initially, ci=bci=b for every 1≤i≤n1≤i≤n. Eric secretly chooses an integer kk (1≤k≤n)(1≤k≤n) and chooses ckck to be the special array.

There are two operations that Eric can perform on an array ctct:

  • Operation 1: Choose two integers ii and jj (2≤i<j≤m−12≤i<j≤m−1), subtract 11 from both ct[i]ct[i] and ct[j]ct[j], and add 11 to both ct[i−1]ct[i−1] and ct[j+1]ct[j+1]. That operation can only be used on a non-special array, that is when t≠kt≠k.;
  • Operation 2: Choose two integers ii and jj (2≤i<j≤m−22≤i<j≤m−2), subtract 11 from both ct[i]ct[i] and ct[j]ct[j], and add 11 to both ct[i−1]ct[i−1] and ct[j+2]ct[j+2]. That operation can only be used on a special array, that is when t=kt=k.

    Note that Eric can't perform an operation if any element of the array will become less than 00 after that operation.

Now, Eric does the following:

  • For every non-special array cici (i≠ki≠k), Eric uses only operation 1 on it at least once.
  • For the special array ckck, Eric uses only operation 2 on it at least once.

Lastly, Eric discards the array bb.

For given arrays c1,c2,…,cnc1,c2,…,cn, your task is to find out the special array, i.e. the value kk. Also, you need to find the number of times of operation 22 was used on it.

Input

The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases. Description of test cases follows.

The first line of each test case contains two integers nn and mm (3≤n≤1053≤n≤105, 7≤m≤3⋅1057≤m≤3⋅105) — the number of arrays given to you, and the length of each array.

The next nn lines contains mm integers each, ci,1,ci,2,…,ci,mci,1,ci,2,…,ci,m.

It is guaranteed that each element of the discarded array bb is in the range [0,106][0,106], and therefore 0≤ci,j≤3⋅10110≤ci,j≤3⋅1011 for all possible pairs of (i,j)(i,j).

It is guaranteed that the sum of n⋅mn⋅m over all test cases does not exceed 106106.

It is guaranteed that the input is generated according to the procedure above.

Output

For each test case, output one line containing two integers — the index of the special array, and the number of times that Operation 2 was performed on it. It can be shown that under the constraints given in the problem, this value is unique and won't exceed 10181018, so you can represent it as a 6464-bit integer. It can also be shown that the index of the special array is uniquely determined.

In this problem, hacks are disabled.

Example

input

Copy

7
3 9
0 1 2 0 0 2 1 1 0
0 1 1 1 2 0 0 2 0
0 1 2 0 0 1 2 1 0
3 7
25 15 20 15 25 20 20
26 14 20 14 26 20 20
25 15 20 15 20 20 25
3 9
25 15 20 15 25 20 20 20 20
26 14 20 14 26 20 20 20 20
25 15 20 15 25 15 20 20 25
3 11
25 15 20 15 25 20 20 20 20 20 20
26 14 20 14 26 20 20 20 20 20 20
25 15 20 15 25 20 15 20 20 20 25
3 13
25 15 20 15 25 20 20 20 20 20 20 20 20
26 14 20 14 26 20 20 20 20 20 20 20 20
25 15 20 15 25 20 20 15 20 20 20 20 25
3 15
25 15 20 15 25 20 20 20 20 20 20 20 20 20 20
26 14 20 14 26 20 20 20 20 20 20 20 20 20 20
25 15 20 15 25 20 20 20 15 20 20 20 20 20 25
3 9
909459 479492 676924 224197 162866 164495 193268 742456 728277
948845 455424 731850 327890 304150 237351 251763 225845 798316
975446 401170 792914 272263 300770 242037 236619 334316 725899

output

Copy

3 1
3 10
3 15
3 20
3 25
3 30
1 1378716

Note

In the first test case, the secret array bb is [0,1,1,1,1,1,1,1,0][0,1,1,1,1,1,1,1,0]. Array c1c1 and array c2c2 are generated by using operation 1. Array c3c3 is generated by using operation 2.

For Array c1c1,you can choose i=4i=4 and j=5j=5 perform Operation 1 one time to generate it. For Array c2c2, you can choose i=6i=6 and j=7j=7 perform Operation 1 one time to generate it. For Array c3c3,you can choose i=4i=4 and j=5j=5 perform Operation 2 one time to generate it.

In the second test case, the secret array bb is [20,20,20,20,20,20,20][20,20,20,20,20,20,20]. You can also find that array c1c1 and array c2c2 are generated by using Operation 1. Array c3c3 is generated by using Operation 2.

In the third test case, the secret array bb is [20,20,20,20,20,20,20,20,20][20,20,20,20,20,20,20,20,20]. You can also find that array c1c1 and array c2c2 are generated by using Operation 1. Array c3c3 is generated by using Operation 2.

题意:

定义一个特殊行,执行操作2,其它执行操作1,都至少有一次

要求输出的答案是操作2的执行次数

思路:

从要求中推导公式,因为和位置有关所以可以想到下标和数的乘积关系,平时练习差好多

操作1的影响:原来:i*ai+j*aj现在i*(ai-1)+j*(aj-1)+(i-1)*(a(i-1)+1)+(j+1)*(a(j+1)+1)=>-i+-j+(i-1)+(j+1)=>x*ax的和不变

操作2的影响:i*ai+j*aj,现在i*(ai-1)+j*(aj-1)+(i-1)*(a(i-1)+1)+(j+2)*(a(j+2)+1)=>-i+-j+(i-1)+(j+2)=>+1操作一次,和加1,从而得出答案

代码:

#include<bits/stdc++.h>
using namespace std;
#define int long long

signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);//按要求推导公式
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);//a为add,,w
#endif
	int t;cin>>t;
    while(t--){
        int n,m;cin>>n>>m;
        vector<int>ans;
        int sum=0;
        for(int i=1;i<=n;++i){
            sum=0;
            for(int j=1;j<=m;++j){
                int x;cin>>x;
                sum+=x*j;
            }
            ans.emplace_back(sum);
        }
        int mx=0,pos=0;
        for(int i=0;i<ans.size();++i){
            if(ans[i]>mx)mx=ans[i],pos=i+1;

        }
        sort(ans.begin(),ans.end());
        cout<<pos<<' '<<ans[ans.size()-1]-*ans.begin()<<'\n';
    }

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
为了解决这个问题,我们可以先对两个数组进行排序,然后用双指针法来找到所有相似的子数组。具体实现如下: ```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)。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值