牛客练习赛33 B tokitsukaze and RPG (类埃筛)

链接:https://ac.nowcoder.com/acm/contest/308/B
来源:牛客网

tokitsukaze and RPG
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
tokitsukaze最近沉迷一款RPG。
这个RPG一天有k分钟,每一天从第1分钟开始。
有n种怪物,第i种怪物每天第一次出现的时间为Xi分钟,第二次出现的时间为2Xi分钟,第三次出现的时间为3Xi分钟......同一时刻出现的怪物种类越多,打怪获得的经验也越高。
为了高效练级,tokitsukaze想知道在一天内出现怪物种类最多的时间点会出现多少种怪物,这样的时间点有多少个。
输入描述:
第一行包括2个正整数n,k(1≤n≤105,1≤k≤106),表示有n种怪物,一天有k分钟。
接下来一行包括n个正整数X(1≤Xi≤10^6),含义如题面所示。
输出描述:
输出一行,包括两个整数a,b。
a表示怪物种类数,b表示时间点的个数。
示例1
输入
复制
3 6
2 2 3
输出
复制
3 1
说明
在第6分钟时,3种怪物都出现了。
示例2
输入
复制
3 5
2 2 3
输出
复制
2 2
说明
在第2分钟和第4分钟时,第一种和第二种怪物出现了。
示例3
输入
复制
6 10
1 2 3 4 5 6
输出
复制
4 1
说明
在第6分钟时,出现了第一种、第二种、第三种、第六种怪物。
示例4
输入
复制
3 5
6 6 6
输出
复制
0 5
说明
在第1分钟、第2分钟、第3分钟、第4分钟、第5分钟,都没有出现怪物。

题意:

思路:

记录 每一个x的个数,然后去重x数组,用类埃筛的方式去维护 数组 num[i] ,num[i] 代表 在i这个时间点有多少个怪兽出现。

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#include <bits/stdc++.h>
//#include <bits/unordered_map.h>

#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int* p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/

unordered_map<int, int> m;
int n, k;

ll num[maxn];
int main()
{
	//freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
	//freopen("D:\\common_text\code_stream\\out.txt","w",stdout);
	gbtb;
	cin >> n >> k;
	int x;
	vector<int> v;
	repd(i, 1, n)
	{
		cin >> x;
		m[x]++;
		v.push_back(x);
	}
	sort(ALL(v));
	v.erase(unique(ALL(v)), v.end()); // 去重
	for (auto x : v)
	{
		for (int i = x; i <= k; i += x)
		{
			num[i] += m[x];
		}
	}
	ll ans1 = -1ll;
	ll ans2;
	repd(i, 1, k)
	{
		if (num[i] > ans1)
		{
			ans1 = num[i];
			ans2 = 1;
		} else if (num[i] == ans1)
		{
			ans2++;
		}
	}
	cout << ans1 << " " << ans2 << endl;



	return 0;
}

inline void getInt(int* p) {
	char ch;
	do {
		ch = getchar();
	} while (ch == ' ' || ch == '\n');
	if (ch == '-') {
		*p = -(getchar() - '0');
		while ((ch = getchar()) >= '0' && ch <= '9') {
			*p = *p * 10 - ch + '0';
		}
	}
	else {
		*p = ch - '0';
		while ((ch = getchar()) >= '0' && ch <= '9') {
			*p = *p * 10 + ch - '0';
		}
	}
}




posted @ 2019-07-13 23:06  茄子Min  阅读(193)  评论(0编辑  收藏  举报