题解 Read

传送门

思路题,卡空间

  • 摩尔投票法

image

Code:
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define N 100010
#define ll long long
// #define int long long

inline int read() {
	int ans=0, f=1; char c=getchar();
	while (!isdigit(c)) {if (c=='-') f=-f; c=getchar();}
	while (isdigit(c)) {ans=(ans<<3)+(ans<<1)+(c^48); c=getchar();}
	return ans*f;
}

int m, k;
int cnt[1010];
ll x[1010], y[1010], z[1010];

#if 0
namespace task1{
	int buc[1050000];
	void solve() {
		int n=0; ll s=(1<<k)-1;
		ll now, lst;
		for (int i=1; i<=m; ++i) {
			++n;
			// now=x[i];
			++buc[x[i]];
			lst=x[i];
			for (int j=1; j<cnt[i]; ++j) {
				lst = (lst*y[i]+z[i])&s;
				++n;
				// now=lst;
				++buc[lst];
				// cout<<lst<<' ';
			}
		}
		int maxcnt=0;
		for (int i=0; i<=s; ++i) maxcnt=max(maxcnt, buc[i]);
		int other=n-maxcnt;
		printf("%d\n", max(maxcnt-other-1, 0));
		exit(0);
	}
}
#endif

namespace task2{
	struct hash_map{
		static const int SIZE=920000;
		int head[SIZE], tot;
		struct node{int val, cnt, next;}e[1050000];
		hash_map(){memset(head, -1, sizeof(head));}
		inline void add(ll pos) {
			ll t=131313131ll*pos%SIZE;
			for (int i=head[t]; ~i; i=e[i].next) if (e[i].val==pos) {
				++e[i].cnt;
				return ;
			}
			if (tot>=1050000) return ;
			e[++tot].val=pos; e[tot].cnt=1; e[tot].next=head[t]; head[t]=tot;
		}
		inline void clear() {tot=0; memset(head, -1, sizeof(head));}
		inline int calc() {int ans=0; for (int i=1; i<=tot; ++i) ans=max(ans, e[i].cnt); return ans;}
	}mp;
	void solve() {
		// cout<<double(sizeof(mp))/1024/1024<<endl;
		int n=0; ll s=(1<<k)-1;
		ll now, lst;
		for (int i=1; i<=m; ++i) {
			++n;
			// now=x[i];
			mp.add(x[i]);
			lst=x[i];
			for (int j=1; j<cnt[i]; ++j) {
				lst = (lst*y[i]+z[i])&s;
				++n;
				// now=lst;
				mp.add(lst);
			}
		}
		int maxcnt=mp.calc();
		// cout<<"tot: "<<mp.tot<<endl;
		int other=n-maxcnt;
		printf("%d\n", max(maxcnt-other-1, 0));
		exit(0);
	}
}

namespace task{
	int id, cnt2;
	void solve() {
		int n=0; ll s=(1<<k)-1;
		ll now, lst, mcnt=0;
		for (int i=1; i<=m; ++i) {
			++n;
			// now=x[i];
			if (!cnt2) id=x[i], cnt2=1;
			lst=x[i];
			for (int j=1; j<cnt[i]; ++j) {
				lst = (lst*y[i]+z[i])&s;
				++n;
				// now=lst;
				if (!cnt2) id=lst, cnt2=1;
				else if (id==lst) ++cnt2;
				else --cnt2;
			}
		}
		n=0;
		for (int i=1; i<=m; ++i) {
			++n;
			// now=x[i];
			if (x[i]==id) ++mcnt;
			lst=x[i];
			for (int j=1; j<cnt[i]; ++j) {
				lst = (lst*y[i]+z[i])&s;
				++n;
				// now=lst;
				if (id==lst) ++mcnt;
			}
		}
		printf("%lld\n", max(mcnt-(n-mcnt)-1, 0ll));
	}
}

signed main()
{
	freopen("b.in", "r", stdin);
	freopen("b.out", "w", stdout);

	m=read(); k=read();
	for (int i=1; i<=m; ++i) cnt[i]=read();
	for (int i=1; i<=m; ++i) x[i]=read();
	for (int i=1; i<=m; ++i) y[i]=read();
	for (int i=1; i<=m; ++i) z[i]=read();
	// cout<<double(sizeof(x)*4 + sizeof(task2::mp))/1024/1024<<endl;
	task::solve();
	
	return 0;
}
posted @ 2021-09-26 21:09  Administrator-09  阅读(2)  评论(0编辑  收藏  举报