Codeforces Round #601 (Div. 2) 题解

题目链接:

 

A - Changing Volume

水题

#include
#define x first
#define y second
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair pa;
const int mx = 1e6 + 10;
vector  g[mx];
int main()
{ 
	int t;
	scanf("%d",&t);
	while (t--) {
		int a,b;
		scanf("%d%d",&a,&b);
		if (a == b) {
			puts("0");
			continue;
		}
		int sum = abs(b-a);
		int ans = sum / 5;
		int mv = sum % 5;
		if (mv == 3 || mv == 4)
			ans += 2;
		else if (mv == 1 || mv == 2)
			ans += 1;
		printf("%d\n",ans);
	}
    return 0; 
}

B - Fridge Lockers

水题

#include
#define x first
#define y second
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair pa;
const int mx = 1e5 + 10;
int a[mx];
int main()
{ 
	int t;
	scanf("%d",&t);
	while (t--) {
		int n,m;
		scanf("%d%d",&n,&m);
		int sum = 0;
		for (int i=1;i<=n;i++) {
			scanf("%d",a+i);
			sum += a[i];
		}
		if (n != m || n == 2) {
			puts("-1");
		} else {
			printf("%d\n",sum*2);
			for (int i=1;i<=n;i++) {
				printf("%d %d\n",i,i+1>n?1:i+1);
			}
		}
	}
    return 0; 
}

C - League of Leesins

水题

#include
#define x first
#define y second
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair pa;
const int mx = 1e5 + 10;
int a[mx];
bool vis[mx];
map > mp[mx];
void dfs(int u,int v) {
	vis[v] = 1;
	printf("%d ",v);
	for (int i : mp[u][v]) if (!vis[i]) {
		dfs(v,i);
	}
}
int main()
{ 
	int n;
	scanf("%d",&n);
	int u,v,c;
	for (int i=1;i

D - Feeding Chicken

蛇形走法然后一行一行地放

#include
#define x first
#define y second
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair pa;
const int mx = 100 + 10;
int n,m;
char s[mx];
char mp[mx][mx];
int main()
{ 
	for(int i=0;i<26;i++) s[i] = 'a' + i;
	for(int i=0;i<26;i++) s[i+26] = 'A' + i;
	for(int i=0;i<10;i++) s[i+52] = '0' + i;
	int t; scanf("%d",&t);
	while (t--) {
		int K;
		scanf("%d%d%d",&n,&m,&K);
		int sum = 0;
		for (int i=0;i0) - 1;
						mv--,pos++;
					}
					mp[i][j] = s[pos];
				}
			} else {
				for (int j=m-1;j>=0;j--) {
					ret -= (mp[i][j]=='R');
					if (ret < 0) {
						ret = reg + (mv>0) - 1;
						mv--,pos++;
					}
					mp[i][j] = s[pos];
				}
			}
			puts(mp[i]);
		}
	}
    return 0; 
}

E2 - Send Boxes to Alice (Hard Version)

简单证明可得分成被x整除肯定比被2*x整除步数要少,所以我们只要比较分成和的质因子就好了。

然后先对x取模之后,每连续的x个进行集合,集合的点肯定是中位数那个点,然后模拟一下就好了

#include
#define x first
#define y second
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair pa;
const int mx = 1e6 + 10;
int n,m;
int a[mx],b[mx];
ll pre[mx],cnt[mx];
vector  pri;
vector  g;
bool vis[mx];
void init() {
	for (int i=2;i= mx) break;
			vis[j*i] = 1;
			if (i % j == 0) break;
		}
	}
}
ll get_ans(ll val) {
	ll sum = 0;
	for (int i=1;i<=n;i++) {
		b[i] = a[i] % val;
		sum += b[i];
		pre[i] = pre[i-1] + 1ll*b[i]*i;
		cnt[i] = cnt[i-1] + b[i];
	}
	ll siz = sum / val;
	ll ans = 0,pv = 0,ret = 0;
	int pp = 1;
	for (int i=1;i<=siz;i++,ret+=val) {
		int midk = lower_bound(cnt+pp,cnt+1+n,ret + (val+1)/2) - cnt;
		int k = lower_bound(cnt+midk,cnt+n+1,ret+val) - cnt;
		ll mv = max(0ll,cnt[k]-(ret+val));
		ll lv = pre[midk] - pv;
		ans += midk*(cnt[midk]-ret) - lv;
		ll rv = pre[k] - mv*k - pre[midk];
		ans += rv - midk*(cnt[k]-cnt[midk]-mv);
		pv = pre[k] - mv*k, pp = k;
	}
	return ans;
}
int main()
{ 
	scanf("%d",&n);
	init();
	ll sum = 0;
	for (int i=1;i<=n;i++) {
		scanf("%d",a+i);
		sum += a[i];
	}
	ll tmp = sum,ans = 2e18;
	for (int i : pri) {
		if (tmp % i == 0)
			g.push_back(i);
		while (tmp % i == 0)
			tmp /= i;
	}
	if (tmp != 1) g.push_back(tmp);
	for (ll i : g) {
		ans = min(ans,get_ans(i));
	}
	printf("%lld\n",ans==2e18?-1:ans);
    return 0; 
}

 

你可能感兴趣的:(建设性,思维题,数论)