A. Water Buying

链接:http://codeforces.com/contest/1118/problem/A

实现代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{ll n,a,b,m;cin>>n;for(int i = 1;i <= n;i ++){cin>>m>>a>>b;if(a*2 <= b)cout<<m*a<<endl;else{if(m%2==0) cout<<m/2*b<<endl;else cout<<m/2*b+a<<endl;}}return 0;
}

B. Tanya and Candies

链接:http://codeforces.com/contest/1118/problem/B

题意:给你一串序列,去掉其中一个数字,剩下的序列满足奇数数字之和等于偶数数字之和,问有多少个这样的数字

思路:从后往前用前缀和处理下,得到当前点到序列尾的奇数和与偶数和,去掉当前点造成的影响其实就是后面的奇数和与偶数和互换,再分别加上当前点前面点的奇数和与偶数和,判断下是否相等就好了。

实现代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int M = 2e5+10;
int x[M],a[M],b[M],n;
int main()
{cin>>n;for(int i = 1;i <= n;i ++){cin>>x[i];}for(int i = n;i >= 1;i --){a[i] = a[i+1]; b[i] = b[i+1];if(i%2==0)a[i] += x[i];elseb[i] += x[i];}int ans = 0;for(int i = 1;i <= n;i ++){int ans1 = a[1] - a[i];int ans2 = b[1] - b[i];//cout<<ans1<<" "<<ans2<<endl;ans1 += b[i+1];ans2 += a[i+1];// cout<<ans1<<" "<<ans2<<endl;if(ans1 == ans2)  ans++;}cout<<ans<<endl;return 0;
}

C. Palindromic Matrix

链接:http://codeforces.com/contest/1118/problem/C

题意:给出一段序列,让你用序列中的数构造一个矩阵,这个矩阵的行列颠倒也不会发生变化,这种矩阵被称为回文矩阵

思路:当n为偶数时,序列中每个数出现次数必须为4的倍数,如果不是那就无法构造,奇数是,序列中的数出现次数%4==2的不能超过n个||奇数次数的不能超过一个||数先构造中间的十字架构造周围四个矩阵时如果出现某个数出现次数不为4的次数时也无法构造。主要就是奇数构造起来有点麻烦,我们先用%4==2与奇数次数的那些数构造中间的十字架,构造完就根据左上角的矩阵依次填数,每填一个就把左下,右上,右下三个区域对应的点也填上这个数,保证对应。

实现代码:

#include<bits/stdc++.h>
using namespace std;
const int M = 1e3+10;
queue<int>v;
queue<int>v2;
int mp[M],n,x;
int g[30][30];
int v1[M];
int main()
{int flag = 1,tot = 0;cin>>n;for(int i = 1;i <= n*n;i ++){cin>>x;mp[x] ++;if(mp[x] == 1) v.push(x),v1[++tot]=x;}if(n%2==0){for(int i = 1;i <= n/2;i ++){for(int j = 1;j <= n/2;j ++){int now = v.front();if(mp[now]<4){flag = 0;break;}else if(mp[now] == 4){v.pop();mp[now] = 0;}elsemp[now] -= 4;g[i][j] = now; g[n-i+1][j] = now;g[n-i+1][n-j+1] = now; g[i][n-j+1]=now;}}}else{int cnt = 0,cnt1 = 0;for(int j = 1;j <= tot;j ++){int i = v1[j];if(mp[i]%2 == 1){cnt++,g[n/2+1][n/2+1] = i;if(mp[i]!=1)  mp[i]-=1;}if(mp[i]%4 == 2){cnt1++;v2.push(i);}}if(cnt > 1||cnt1 > n-1) flag = 0;for(int i = 1;i <= tot;i ++){if(mp[v1[i]]%4==0)v2.push(v1[i]);}for(int i = 1;i <= n/2;i ++){int now = v2.front();if(mp[now] == 2){v2.pop(); mp[now] = 0;}else if(mp[now]%4==2&&mp[now]>2){mp[now] -= 2;v2.pop(); v2.push(now);}else{mp[now] -= 2;}g[i][n/2+1] = now;g[n-i+1][n/2+1] = now;}for(int i = 1;i <= n/2;i ++){int now = v2.front();if(mp[now] == 2){v2.pop(); mp[now] = 0;}else if(mp[now]%4==2&&mp[now]>2){mp[now] -= 2,v2.pop(),v2.push(now);}else {mp[now]-=2;}g[n/2+1][i] = now;g[n/2+1][n-i+1] = now;}for(int i = 1;i <= n/2;i ++){for(int j = 1;j <= n/2;j ++){int now = v2.front();if(mp[now] == 2) {flag = 0;break;}else if(mp[now] == 4){mp[now] = 0; v2.pop();}else {mp[now] -= 4;}g[i][j] = now;g[n-i+1][j] = now;g[n-i+1][n-j+1]=now; g[i][n-j+1]=now;}}}if(flag){cout<<"YES"<<endl;for(int i = 1;i <= n;i ++){for(int j D2. Coffee and Coursework (Hard Version)= 1;j <= n;j ++){cout<<g[i][j]<<" ";}cout<<endl;}}else {cout<<"NO"<<endl;}
}

D1. Coffee and Coursework (Hard Version)

同下

D2. Coffee and Coursework (Hard Version)

链接:http://codeforces.com/contest/1118/problem/D2

题意:喝咖啡,喝一杯咖啡可以工作ai个作业,一共有n杯咖啡,m个作业,一天内喝多杯咖啡,咖啡作用会减弱,完成这些作业最快需要多少天。

思路:直接二分需要的天数,判断下当前天数能否完成,判断时,最优的喝法肯定是每天喝的杯数尽量一样(这样咖啡作用被减弱的最少),每天先喝作用大的咖啡,作用小的留到后面,这样可以i得到当前天数能完成最多的作业,与需要完成的作业对比下,大于等于的话就往左区间找,小于的话就往右区间找。

实现代码;

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll M = 2e5 + 10;
ll v[M],a[M];
bool cmp(ll x,ll y){return x > y;
}
int main()
{ll n,m,cnt = 0,ans = 0;cin>>n>>m;for(ll i = 1;i <= n;i ++){cin>>a[i];ans += a[i];}if(ans < m){cout<<-1<<endl;return 0;}ll en = n;sort(a+1,a+n+1,cmp);ll l = 1,r = n;while(l < r){ll mid = (l + r) >> 1;ll num = n/mid;for(ll i = 0;i <= num-1;i ++){for(ll j = 1;j <= mid;j ++){v[++cnt] = i;}}for(ll i = 1;i <= (n%mid);i ++)v[++cnt] = num;sort(v+1,v+1+cnt);ll kk = 0;for(ll i = 1;i <= cnt;i ++){kk += max(0*1LL,a[i]-v[i]);}for(ll i = 1;i<= cnt;i ++)v[i] = 0;cnt = 0;if(kk >= m) r = mid,en = mid;else l = mid+1;}cout<<en<<endl;
}

E. Yet Another Ball Problem

链接:http://codeforces.com/contest/1118/problem/E

思路:沙雕构造题,和a题一个难度,看完四个条件和样例基本就能秒出了。

实现代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){ll n,m,cnt = 0;cin>>n>>m;if(n > m*(m-1)) cout<<"NO"<<endl;else {cout<<"YES"<<endl;for(ll i = 1;i <= m;i ++){for(ll j = i+1;j <= m;j ++){cout<<i<<" "<<j<<endl;cnt++;if(cnt == n) return 0;cout<<j<<" "<<i<<endl;cnt++;if(cnt == n) return 0;}}}return 0;
}

F1. Tree Cutting (Easy Version)

链接:http://codeforces.com/contest/1118/problem/F1

题意:给你一棵树,树上的点分别为无色,红色,蓝色,现在删掉一条边,树变成两棵树,这两棵树不能同时有红蓝两种颜色,问有几条这样的边

思路:从树的叶子节点往上更新信息(子树中红色点的数量和蓝色点的数量),如果某个点的子树红色点数量等于整棵树的红色点数量且没有蓝色点,那么这个点与父节点的边是可以被删除的(蓝色同理),我们直接用dfs跑一遍就好了。

实现代码:

#include<bits/stdc++.h>
using namespace std;
const int M = 3e5+10;
vector<int>g[M];
struct node{int x,y;
}q[M];
int n,m,a1,b1,a[M],cnt;
void dfs(int u,int fa){if(a[u]==1) q[u].x++;else if(a[u]==2) q[u].y++;for(int i = 0;i < g[u].size();i ++){int v = g[u][i];if(v == fa) continue;dfs(v,u);q[u].x += q[v].x; q[u].y += q[v].y;}if((q[u].x == a1&&q[u].y==0)||(q[u].y==b1&&q[u].x==0))cnt++;return ;
}int main()
{ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);cin>>n;for(int i = 1;i <= n;i ++){cin>>a[i];if(a[i] == 1) a1++;else if(a[i] == 2) b1 ++;}int x,y;for(int i = 2;i <= n;i ++){cin>>x>>y;g[x].push_back(y);g[y].push_back(x);}dfs(1,0);cout<<cnt<<endl;
}

转载于:https://www.cnblogs.com/kls123/p/10450557.html

Codeforces Round #540 (Div. 3) A,B,C,D2,E,F1相关推荐

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

    链接:http://codeforces.com/contest/1118 来源:Codeforces 文章目录 A. Water Buying B. Tanya and Candies(前缀和) D ...

  2. Codeforces Round #540 (Div. 3)--A. Water Buying(简单思维题-有点坑)

    A. Water Buying 题目链接http://codeforces.com/problemset/problem/1118/A time limit per test:1 second mem ...

  3. Codeforces Round #540 (Div. 3) D. Coffee and Coursework 二分

    题解 题目大意,有若干杯咖啡,每杯咖啡有一个收益a[i],不限制每天喝多少杯,但是每天的第k杯收益会减少k-1,问总收益大于n的所需最少天数. 使用二分答案求解,每次喝肯定是挑剩余最大的去喝,chec ...

  4. Codeforces Round #540 (Div. 3) Coffee and Coursework

    由于我是直接写的hard版本的,而hard版本和easy版本只有数据范围的差距,所以在此只阐述hard版本的题解(easy版也能过,但是easy的数据范围应该还有其他方法可以偷跑过去) 假设x天可以完 ...

  5. 【Codeforces Round #540 (Div. 3)】 A B C D1 D2 E F1

    A 题意 给你一升水和两升水的价格 问你怎么买便宜 做法 模拟即可 #include <cstdio> #include <cstring> #include <iost ...

  6. Codeforces Round #700 (Div. 2)(B,C,D1,D2详细题解)

    C C C是一个非常有意思的题(赛后被hack哈哈哈哈) 其他似乎就没什么好玩的了 1480 B.The Great Hero(模拟) 由于需要打死每个怪物,打死第 i i i个怪物需要攻击 k i ...

  7. Codeforces Round #506 (Div. 3)

    Codeforces Round #506 (Div. 3) 实习期间事不多,对div3 面向题解和数据编程了一波 A. Many Equal Substrings 题目链接 A题就是找后缀和前缀重合 ...

  8. Codeforces Round #563 (Div. 2)/CF1174

    Codeforces Round #563 (Div. 2)/CF1174 CF1174A Ehab Fails to Be Thanos 其实就是要\(\sum\limits_{i=1}^n a_i ...

  9. 构造 Codeforces Round #302 (Div. 2) B Sea and Islands

    题目传送门 1 /* 2 题意:在n^n的海洋里是否有k块陆地 3 构造算法:按奇偶性来判断,k小于等于所有点数的一半,交叉输出L/S 4 输出完k个L后,之后全部输出S:) 5 5 10 的例子可以 ...

最新文章

  1. 连续发表三篇核酸研究数据库文章
  2. HotSpotOverview.pdf
  3. 判断远程文件是否存在
  4. NDK相关概念与NDK开发步骤
  5. Linux系统捕获数据包流程
  6. pycharm 配置引用 docker 中的环境
  7. [react] 什么是React的实例?函数式组件有没有实例?
  8. VS Code 常规配置和一些插件 - JavaScript
  9. php项目实战流程_一个完整的php流程管理实例代码分享
  10. 【Go语言】集合与文件操作
  11. java 基础类型 包装类型
  12. 使用ThreadLocal和AtomicInteger将int属性改为线程安全的计数器
  13. 深度学习自学(二十八):Altas人脸SDK实现之-回调函数
  14. 计算机的网络测速,电脑怎么网络测速
  15. 氚云SaaS介绍文档
  16. CSDN:2021博客之星年度总评选大赛投票
  17. 全国高校计算机能力挑战赛
  18. jquery设置checkbox选中和未选中的方式
  19. python单词个数统计_Python 统计文本中单词的个数
  20. requirements.txt 是什么? 有什么用? 怎么用?

热门文章

  1. 你不可不知的家庭装修禁忌
  2. VMware下Windows2003R2虚拟机磁盘扩容方法
  3. 项目小结:日立OA系统(Asp.net)
  4. 报错-Unknown class in Interface Builder file
  5. 显示串中只出现一次的字符.
  6. s:iterator获取遍历数据的索引下标
  7. acctmod-ftp.sh
  8. gcc的安装----rpm包安装顺序
  9. 数据库高可用性——SQL Server 2005数据库复制简单图解
  10. H.264---CABAC---基础---二进制算术编码