D - Tokitsukaze and Multiple

时间:2020-08-01
本文章向大家介绍D - Tokitsukaze and Multiple,主要包括D - Tokitsukaze and Multiple使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
 
Tokitsukaze has a sequence of length nn, denoted by aa.
Tokitsukaze can merge two consecutive elements of aa as many times as she wants. After each operation, a new element that equals to the sum of the two old elements will replace them, and thus the length of aa will be reduced by 11.
Tokitsukaze wants to know the maximum possible number of elements that are multiples of pp she can get after doing some operations (or doing nothing) on the sequence aa.
InputThere are several test cases.

The first line contains an integer T(1T20)(1≤T≤20), denoting the number of test cases. Then follow all the test cases.
For each test case, the first line contains two integers nn and p(1n,p105)(1≤n,p≤105), denoting the length of the sequence and the special number, respectively.
The second line contains nn integers, where the ii-th integer aiai (1ai105)(1≤ai≤105) is the ii-th element of aa.
It is guaranteed that the sum of nn in all test cases is no larger than 106106.
OutputFor each test case, output in one line the maximum possible number of elements that are multiples of pp after doing some operations.
Sample Input

2
5 3
2 1 3 2 1
3 1
123 456 789

Sample Output

3
3

Sponsor

题意:

  一个序列,每次可以将序列相邻的两个数相加生成一个新的和替代这两个元素,求最终序列里为 p 的倍数的元素最多可能为多少个

思路:

  前缀和

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#include <vector>
#include <iterator>
#include <utility>
#include <sstream>
#include <limits>
#include <numeric>
#include <functional>
using namespace std;
#define gc getchar()
#define mem(a) memset(a,0,sizeof(a))
//#define sort(a,n,int) sort(a,a+n,less<int>())

#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef char ch;
typedef double db;

const double PI=acos(-1.0);
const double eps=1e-6;
const int inf=0x3f3f3f3f;
const int maxn=1e5+10;
const int maxm=100+10;
const int N=2e5+10;
const int mod=1e9+7;

int n = 0;
int p = 0;
int counter[N] = {0};
int sum[N] = {0};
int ans[N] = {0};
int K[N] = {0};
void init_K()
{
    for(int i = 0;i<p;i++)
	{
	 	K[i] = -1;
	}
}
int main()
{
    int T;
    cin >> T;
    while(T--)
	{
        scanf("%d%d",&n ,&p);
        init_K();
        for(int i = 1;i<=n;i++)
		{
			int temp = 0;
			cin >> temp;
        	counter[i] = (counter[i-1] + temp)%p;
        }
        for(int i = 0;i<=n;i++)
		{
            sum[i] = K[counter[i]];
            K[counter[i]] = i;
        }
        for(int i = 1;i<=n;i++)
		{
            if(sum[i] != -1)
            {
                ans[i] = max(ans[i-1] , ans[sum[i]] + 1);
            }
            else 
			{
			 	ans[i] = ans[i-1];
			}
        }
        printf("%d\n",ans[n]);
    }
    return 0;
}

  

附第一遍错误思路OT代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#include <vector>
#include <iterator>
#include <utility>
#include <sstream>
#include <limits>
#include <numeric>
#include <functional>
using namespace std;
#define gc getchar()
#define mem(a) memset(a,0,sizeof(a))
//#define sort(a,n,int) sort(a,a+n,less<int>())

#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef char ch;
typedef double db;

const double PI=acos(-1.0);
const double eps=1e-6;
const int inf=0x3f3f3f3f;
const int maxn=1e5+10;
const int maxm=100+10;
const int N=2e5+10;
const int mod=1e9+7;

int counter[100005] = {0};
void init()
{
    for(int i = 0;i < 100005;i++)
		counter[i] = 0;
}
int main()
{
	int T = 0;
	int n  = 0, p = 0;
	int temp = 0;
	int ans = 0;
	cin >> T;
	while(T--)
	{
		init();
		ans = 0;
		cin >> n >> p;
		for(int i = 0;i<n;i++)
		{
			cin >> temp;
			counter[temp % p] += 1;
		}
		ans += counter[0];
		for(int i = 1;i <= p/2;i++)
		{
			ans += min(counter[i],counter[p-i]);
		}
		if(p % 2 == 0)
		{
			ans += counter[p/2]/2;
		}
		cout << ans << endl;
	} 
	return 0;
}

  

$flag 上一页 下一页