题目链接:

  https://codeforces.com/contest/1118

A题:

题意:

  q次查询,给你一个n,要你用1和2来凑出n,1的花费为a,2的花费为b,求花费的最小值。

思路:

  我们知道当2*a<=b时全用1来肯定是最优的,当2*a>b时,若n为奇数就是1个1其他全是2,若n为偶数就全都是2这样是最优的。

代码实现如下:

 #include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL; #define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("D://code//in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0) const double eps = 1e-;
const int mod = ;
const int maxn = 2e5 + ;
const double pi = acos(-);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL; int q;
LL n, a, b; int main(){
scanf("%d", &q);
while(q--) {
scanf("%lld%lld%lld", &n, &a, &b);
LL ans = ;
if( * a <= b) {
ans = n * a;
} else {
ans = n / * b + (n % ) * a;
}
printf("%lld\n", ans);
}
return ;
}

B题:

题意:

  给你n个数,要你去掉一个数,然后剩余的n-1个数保持相对顺序不变,问有多少种方案使得剩下的数中奇数项的和等于偶数项的和。

思路:

  我们知道去掉一个数后对前面的数的位置的奇偶性不会产生影响,后面的数的位置的奇偶性变为对立的,因此我们只需要求下奇数项和偶数项的前缀和即可。

代码实现如下:

 #include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL; #define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("D://code//in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0) const double eps = 1e-;
const int mod = ;
const int maxn = 2e5 + ;
const double pi = acos(-);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL; int n;
LL a[maxn], sum1[maxn], sum2[maxn]; int main(){
scanf("%d", &n);
for(int i = ; i <= n; i++) {
scanf("%lld", &a[i]);
sum1[i] = sum1[i-];
sum2[i] = sum2[i-];
if(i & ) sum1[i] += a[i];
else sum2[i] += a[i];
}
int ans = ;
for(int i = ; i <= n; i++) {
if(sum1[i-] + sum2[n] - sum2[i] == sum2[i-] + sum1[n] - sum1[i]) {
ans++;
}
}
printf("%d\n", ans);
return ;
}

C题:

题意:

  给你n*n个数要你构建一个每行每列都是回文的矩阵。

思路:

  模拟即可。

代码实现如下:

 #include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL; #define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("D://code//in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0) const double eps = 1e-;
const int mod = ;
const int maxn = 2e5 + ;
const double pi = acos(-);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL; int n, x;
int mp[][], cnt[]; int main(){
scanf("%d", &n);
for(int i = ; i <= n * n; i++) {
scanf("%d", &x);
cnt[x]++;
}
if(n % == ) {
for(int i = ; i <= ; i++) {
if(cnt[i] % != ) {
return printf("NO\n") * ;
}
}
int pp = ;
for(int i = ; i <= n / ; i++) {
for(int j = ; j <= n / ; j++) {
while(cnt[pp] == ) pp++;
mp[i][j] = mp[i][n-j+] = mp[n-i+][j] = mp[n-i+][n-j+] = pp;
cnt[pp] -= ;
}
}
} else {
int num1 = , num2 = , num3 = ;
for(int i = ; i <= ; i++) {
if(cnt[i] % == ) num1++;
else if(cnt[i] % == ) num2++;
else if(cnt[i] % == ) num3++;
}
if(num2 > (n-) || (num1 > && num3 > ) || (num1 == && num3 != ) || (num3 == && num1 != )) return printf("NO\n") * ;
for(int i = ; i <= ; i++) {
if(cnt[i] % == || cnt[i] % == ) {
mp[n/+][n/+] = i;
cnt[i]--;
break;
}
}
int pp = ;
for(int i = ; i <= n / ; i++) {
for(int j = ; j <= n / ; j++) {
while(cnt[pp] < ) pp++;
mp[i][j] = mp[i][n-j+] = mp[n-i+][j] = mp[n-i+][n-j+] = pp;
cnt[pp] -= ;
}
}
pp = ;
for(int i = ; i <= n / ; i++) {
while(cnt[pp] == ) {
pp++;
if(pp > ) return printf("NO\n") * ;
}
mp[i][n/+] = mp[n-i+][n/+] = pp;
cnt[pp] -= ;
}
for(int i = ; i <= n / ; i++) {
while(cnt[pp] < ) {
pp++;
if(pp > ) return printf("NO\n") * ;
}
if(pp > ) return printf("NO\n") * ;
mp[n/+][i] = mp[n/+][n-i+] = pp;
cnt[pp] -= ;
}
}
printf("YES\n");
for(int i = ; i <= n; i++) {
for(int j = ; j <= n; j++) {
printf("%d ", mp[i][j]);
}
printf("\n");
}
return ;
}

D题:

题意:

  有n杯咖啡,m页作业,每杯咖啡的权值为ai,假如你某一天喝了k杯咖啡那么你写的作业页数为a1,a2-1,a3-2……这里的1,2,3指当天喝的顺序而非原序列的下标,要你用最少的天数写完作业。

思路:

  先将ai从大到小排序,二分天数,然后把所有会产生正贡献的咖啡求和,与m比较即可。

代码实现如下:

 #include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL; #define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("D://code//in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0) const double eps = 1e-;
const int mod = ;
const int maxn = 2e5 + ;
const double pi = acos(-);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL; int n, m;
int a[maxn]; bool check(int x) {
int tmp = ;
LL sum = ;
for(int i = ; i <= min(n, x); i++) {
sum += a[i];
}
for(int i = x + ; i <= n; i++) {
if(i % x == || x == ) tmp++;
if(a[i] - tmp > ) {
sum += a[i] - tmp;
} else {
break;
}
}
return sum >= m;
} bool cmp(int a, int b) {
return a > b;
} int main(){
scanf("%d%d", &n, &m);
LL sum = ;
for(int i = ; i <= n; i++) {
scanf("%d", &a[i]);
sum += a[i];
}
sort(a + , a + n + , cmp);
if(sum < m) {
return printf("-1\n") * ;
}
int ub = inf, lb = , mid, ans = ;
while(ub >= lb) {
mid = (ub + lb) >> ;
if(check(mid)) {
ub = mid - ;
ans = mid;
} else {
lb = mid + ;
}
}
printf("%d\n", ans);
return ;
}

E题:

题意:

  给你n和k,要你构建n对数,假设第i对为ai,bi,第j对为aj,bj,需满足一下条件:

  1. ai!=bi;
  2. ai==aj与bi==bj不能同时成立;
  3. 若j==i+1时,ai!=aj且bi!=bj。

思路:

  第一个数一直是1~k的循环,第二个数则是2~k~1,3~k~2,4~k~2这样循环,我们知道满足题意的总排列对数只有k*(k-1)种(第一位放置方法有k种选择,第二位也有k种,故总的排列方法是k*k,其中只有1-1,2-2这种是不满足要求的),因此当n>k*(k-1)时输出NO。

代码实现如下:

 #include<bits/stdc++.h>
using namespace std;
int n,k;
pair<int,int> ans[];
int main(){
scanf("%d%d",&n,&k);
if(n>1LL*k*(k-)) return puts("NO")*;
int a=,x=,y=;
for(int i = ; i<=n;i++){
ans[i]={x,y};
x++,y++;
if(x>k) x = ;
if(y>k) y =;
if(y==a) {
a++;
if(a>k) a=;
y = a;
}
}
puts("YES");
for(int i =;i<=n;i++){
printf("%d %d\n",ans[i].first,ans[i].second);
}
return ;
}

F1题:

题意:

  给你一棵树,每个结点的颜色可以是0,1,2,要你删除一条边使得得到的两个联通块内不能同时出现1和2。

思路:

  dfs记一下子树中的1和2的个数与总的1和2的个数比较即可。

代码实现如下:

 #include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL; #define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("D://code//in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0) const double eps = 1e-;
const int mod = ;
const int maxn = 3e5 + ;
const double pi = acos(-);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL; int n, u, v, num1, num2;
LL ans = ;
int a[maxn], sum1[maxn], sum2[maxn];
vector<int> G[maxn]; void dfs(int u, int p) {
if(a[u] == ) sum1[u]++;
else if(a[u] == ) sum2[u]++;
for(int i = ; i < (int)G[u].size(); i++) {
int v = G[u][i];
if(v == p) continue;
dfs(v, u);
if((sum1[v] == num1 && sum2[v] == ) || (sum2[v] == num2 && sum1[v] == )) ans++;
sum1[u] += sum1[v], sum2[u] += sum2[v];
}
} int main(){
scanf("%d", &n);
for(int i = ; i <= n; i++) {
scanf("%d", &a[i]);
if(a[i] == ) num1++;
else if(a[i] == ) num2++;
}
for(int i = ; i < n; i++) {
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
dfs(, );
printf("%lld\n", ans);
return ;
}

Codeforces Round #540 (Div. 3)题解的更多相关文章

  1. Codeforces Round #540 (Div. 3) 部分题解

    Codeforces Round #540 (Div. 3) 题目链接:https://codeforces.com/contest/1118 题目太多啦,解释题意都花很多时间...还有事情要做,就选 ...

  2. Codeforces Round #182 (Div. 1)题解【ABCD】

    Codeforces Round #182 (Div. 1)题解 A题:Yaroslav and Sequence1 题意: 给你\(2*n+1\)个元素,你每次可以进行无数种操作,每次操作必须选择其 ...

  3. Codeforces Round #608 (Div. 2) 题解

    目录 Codeforces Round #608 (Div. 2) 题解 前言 A. Suits 题意 做法 程序 B. Blocks 题意 做法 程序 C. Shawarma Tent 题意 做法 ...

  4. Codeforces Round #525 (Div. 2)题解

    Codeforces Round #525 (Div. 2)题解 题解 CF1088A [Ehab and another construction problem] 依据题意枚举即可 # inclu ...

  5. Codeforces Round #528 (Div. 2)题解

    Codeforces Round #528 (Div. 2)题解 A. Right-Left Cipher 很明显这道题按题意逆序解码即可 Code: # include <bits/stdc+ ...

  6. Codeforces Round #466 (Div. 2) 题解940A 940B 940C 940D 940E 940F

    Codeforces Round #466 (Div. 2) 题解 A.Points on the line 题目大意: 给你一个数列,定义数列的权值为最大值减去最小值,问最少删除几个数,使得数列的权 ...

  7. Codeforces Round #677 (Div. 3) 题解

    Codeforces Round #677 (Div. 3) 题解 A. Boring Apartments 题目 题解 简单签到题,直接数,小于这个数的\(+10\). 代码 #include &l ...

  8. Codeforces Round #665 (Div. 2) 题解

    Codeforces Round #665 (Div. 2) 题解 写得有点晚了,估计都官方题解看完切掉了,没人看我的了qaq. 目录 Codeforces Round #665 (Div. 2) 题 ...

  9. Codeforces Round #160 (Div. 1) 题解【ABCD】

    Codeforces Round #160 (Div. 1) A - Maxim and Discounts 题意 给你n个折扣,m个物品,每个折扣都可以使用无限次,每次你使用第i个折扣的时候,你必须 ...

随机推荐

  1. sguf冲销脚本的实现

    1.该脚本为PCISS项目的sguf冲销脚本: DECLARE type typ_sguf_table is table of sguf_rowid_tab_1%rowtype ; sguf_tab ...

  2. Window下JDK安装教程

    1.准备 win10系统,其他windows系统安装过程大同小异官网下载jdk1.8下载地址:https://www.oracle.com/technetwork/java/javase/downlo ...

  3. Spring Cloud之Eureka服务注册与发现

    解决什么问题 ➟阐述微服务以及服务注册发现的部分概念 ➟阐述Eureka服务注册与发现的部分原理及细节 为什么需要服务中心 过去,每个应用都是一个CPU,一个主机上的单一系统.然而今天,随着大数据和云 ...

  4. Apache+Nginx+php共存(一)

    在实际开发中个人的电脑中经常需要安装 WNMRP.WAMRP.LNMRP.LAMRP等各种开发环境来应对不同的开发需求. 此篇主要是对WINDOWS系统下 Apache+Nginx + PHP +My ...

  5. equals比较对象

    object类的equals方法用来比较是否是同一个对象,比较内存地址. jdk中有些类重写了equals方法,只要类型,内容相同,就相等. 类如果涉及到比较应该重写equals方法,比较内存地址没有 ...

  6. poj3061 Subsequence(尺取)

    Description A sequence of N positive integers (10 < N < 100 000), each of them less than or eq ...

  7. 修改pip源到国内的镜像源

    国内网络原因,经常无法访问一些技术网站,pypi.python.org就是其中一个.所以,使用pip给Python安装软件时,经常出现错误.like this: File "/usr/lib ...

  8. BZOJ5323 JXOI2018游戏(线性筛+组合数学)

    可以发现这个过程非常类似埃氏筛,将在该区间内没有约数的数定义为质数,那么也就是求每种方案中选完所有质数的最早时间之和. 于是先求出上述定义中的质数个数,线性筛即可.然后对每个最短时间求方案数,非常显然 ...

  9. C 函数——Day04

    C 函数 函数是一组一起执行一个任务的语句.每个 C 程序都至少有一个函数,即主函数 main() ,所有简单的程序都可以定义其他额外的函数. 您可以把代码划分到不同的函数中.如何划分代码到不同的函数 ...

  10. libuv 简单使用

    libuv 简单使用 来源:https://zhuanlan.zhihu.com/p/50497450 前序:说说为啥要研究libuv,其实在很久之前(大概2年前吧)玩nodejs的时候就对这个核心库 ...