Codeforces Round #540 (Div. 3) A,B,C,D2,E,F1
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 = ;i <= n;i ++){
cin>>m>>a>>b;
if(a* <= b)
cout<<m*a<<endl;
else{
if(m%==) cout<<m/*b<<endl;
else cout<<m/*b+a<<endl;
}
}
return ;
}
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+;
int x[M],a[M],b[M],n;
int main()
{
cin>>n;
for(int i = ;i <= n;i ++){
cin>>x[i];
}
for(int i = n;i >= ;i --){
a[i] = a[i+]; b[i] = b[i+];
if(i%==)
a[i] += x[i];
else
b[i] += x[i];
}
int ans = ;
for(int i = ;i <= n;i ++){
int ans1 = a[] - a[i];
int ans2 = b[] - b[i];
//cout<<ans1<<" "<<ans2<<endl;
ans1 += b[i+];
ans2 += a[i+];
// cout<<ans1<<" "<<ans2<<endl;
if(ans1 == ans2) ans++;
}
cout<<ans<<endl;
return ;
}
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+;
queue<int>v;
queue<int>v2;
int mp[M],n,x;
int g[][];
int v1[M];
int main()
{
int flag = ,tot = ;
cin>>n;
for(int i = ;i <= n*n;i ++){
cin>>x;
mp[x] ++;
if(mp[x] == ) v.push(x),v1[++tot]=x;
}
if(n%==){
for(int i = ;i <= n/;i ++){
for(int j = ;j <= n/;j ++){
int now = v.front();
if(mp[now]<){
flag = ;break;
}
else if(mp[now] == ){
v.pop();mp[now] = ;
}
else
mp[now] -= ;
g[i][j] = now; g[n-i+][j] = now;
g[n-i+][n-j+] = now; g[i][n-j+]=now;
}
}
}
else{
int cnt = ,cnt1 = ;
for(int j = ;j <= tot;j ++){
int i = v1[j];
if(mp[i]% == ){
cnt++,g[n/+][n/+] = i;
if(mp[i]!=) mp[i]-=;
}
if(mp[i]% == ){
cnt1++;v2.push(i);
}
}
if(cnt > ||cnt1 > n-) flag = ;
for(int i = ;i <= tot;i ++){
if(mp[v1[i]]%==)
v2.push(v1[i]);
}
for(int i = ;i <= n/;i ++){
int now = v2.front();
if(mp[now] == ){
v2.pop(); mp[now] = ;
}
else if(mp[now]%==&&mp[now]>){
mp[now] -= ;v2.pop(); v2.push(now);
}
else{
mp[now] -= ;
}
g[i][n/+] = now;
g[n-i+][n/+] = now;
}
for(int i = ;i <= n/;i ++){
int now = v2.front();
if(mp[now] == ){
v2.pop(); mp[now] = ;
}
else if(mp[now]%==&&mp[now]>){
mp[now] -= ,v2.pop(),v2.push(now);
}
else {
mp[now]-=;
}
g[n/+][i] = now;
g[n/+][n-i+] = now;
}
for(int i = ;i <= n/;i ++){
for(int j = ;j <= n/;j ++){
int now = v2.front();
if(mp[now] == ) {
flag = ;break;
}
else if(mp[now] == ){
mp[now] = ; v2.pop();
}
else {
mp[now] -= ;
} g[i][j] = now;g[n-i+][j] = now;
g[n-i+][n-j+]=now; g[i][n-j+]=now;
}
}
}
if(flag){
cout<<"YES"<<endl;
for(int i = ;i <= n;i ++){
for(int j D2. Coffee and Coursework (Hard Version)= ;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 + ;
ll v[M],a[M];
bool cmp(ll x,ll y){
return x > y;
}
int main()
{
ll n,m,cnt = ,ans = ;
cin>>n>>m;
for(ll i = ;i <= n;i ++){
cin>>a[i];
ans += a[i];
}
if(ans < m){
cout<<-<<endl;
return ;
}
ll en = n;
sort(a+,a+n+,cmp);
ll l = ,r = n;
while(l < r){
ll mid = (l + r) >> ;
ll num = n/mid;
for(ll i = ;i <= num-;i ++){
for(ll j = ;j <= mid;j ++){
v[++cnt] = i;
}
}
for(ll i = ;i <= (n%mid);i ++)
v[++cnt] = num;
sort(v+,v++cnt);
ll kk = ;
for(ll i = ;i <= cnt;i ++){
kk += max(*1LL,a[i]-v[i]);
}
for(ll i = ;i<= cnt;i ++)
v[i] = ;
cnt = ;
if(kk >= m) r = mid,en = mid;
else l = mid+;
}
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 = ;
cin>>n>>m;
if(n > m*(m-)) cout<<"NO"<<endl;
else {
cout<<"YES"<<endl;
for(ll i = ;i <= m;i ++){
for(ll j = i+;j <= m;j ++){
cout<<i<<" "<<j<<endl;
cnt++;
if(cnt == n) return ;
cout<<j<<" "<<i<<endl;
cnt++;
if(cnt == n) return ;
}
}
}
return ;
}
F1. Tree Cutting (Easy Version)
链接:http://codeforces.com/contest/1118/problem/F1
题意:给你一棵树,树上的点分别为无色,红色,蓝色,现在删掉一条边,树变成两棵树,这两棵树不能同时有红蓝两种颜色,问有几条这样的边
思路:从树的叶子节点往上更新信息(子树中红色点的数量和蓝色点的数量),如果某个点的子树红色点数量等于整棵树的红色点数量且没有蓝色点,那么这个点与父节点的边是可以被删除的(蓝色同理),我们直接用dfs跑一遍就好了。
实现代码:
#include<bits/stdc++.h>
using namespace std;
const int M = 3e5+;
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]==) q[u].x++;
else if(a[u]==) q[u].y++;
for(int i = ;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==)||(q[u].y==b1&&q[u].x==))
cnt++;
return ;
} int main()
{
ios::sync_with_stdio();
cin.tie(); cout.tie();
cin>>n;
for(int i = ;i <= n;i ++){
cin>>a[i];
if(a[i] == ) a1++;
else if(a[i] == ) b1 ++;
}
int x,y;
for(int i = ;i <= n;i ++){
cin>>x>>y;
g[x].push_back(y);
g[y].push_back(x);
}
dfs(,);
cout<<cnt<<endl;
}
Codeforces Round #540 (Div. 3) A,B,C,D2,E,F1的更多相关文章
- Codeforces Round #540 (Div. 3) 部分题解
Codeforces Round #540 (Div. 3) 题目链接:https://codeforces.com/contest/1118 题目太多啦,解释题意都花很多时间...还有事情要做,就选 ...
- Codeforces Round #540 (Div. 3)--1118C - Palindromic Matrix
https://codeforces.com/contest/1118/problem/C 在查找元素的时候,必须按4,2,1的顺序进行.因为,如果先找1,可能就把原来的4拆散了,然后再找4,就找不到 ...
- Codeforces Round #540 (Div. 3)--1118F1 - Tree Cutting (Easy Version)
https://codeforces.com/contest/1118/problem/F1 #include<bits/stdc++.h> using namespace std; in ...
- Codeforces Round #540 (Div. 3)--1118D2 - Coffee and Coursework (Hard Version)
https://codeforces.com/contest/1118/problem/D2 和easy version的主要区别是,数据增加了. easy version采用的是线性查找,效率低 在 ...
- Codeforces Round #540 (Div. 3)--1118D1 - Coffee and Coursework (Easy version)
https://codeforces.com/contest/1118/problem/D1 能做完的天数最大不超过n,因为假如每天一杯咖啡,每杯咖啡容量大于1 首先对容量进行从大到小的排序, sor ...
- Codeforces Round #540 (Div. 3)题解
题目链接: https://codeforces.com/contest/1118 A题: 题意: q次查询,给你一个n,要你用1和2来凑出n,1的花费为a,2的花费为b,求花费的最小值. 思路: 我 ...
- Codeforces Round #540 (Div. 3) F1. Tree Cutting (Easy Version) 【DFS】
任意门:http://codeforces.com/contest/1118/problem/F1 F1. Tree Cutting (Easy Version) time limit per tes ...
- Codeforces Round #540 (Div. 3) D1. Coffee and Coursework (Easy version) 【贪心】
任意门:http://codeforces.com/contest/1118/problem/D1 D1. Coffee and Coursework (Easy version) time limi ...
- Codeforces Round #540 (Div. 3) C. Palindromic Matrix 【暴力】
任意门:http://codeforces.com/contest/1118/problem/C C. Palindromic Matrix time limit per test 2 seconds ...
随机推荐
- Create a toolwindow for the VBA editor with .NET(C#).
原始出处:http://www.cnblogs.com/Charltsing/p/VBEtoolwindow.html 最近有人问起使用C#在VBE插件中创建toolwindow的事情,由于VBE窗口 ...
- iOS 判断当前网络状态的三种方法
http://www.cocoachina.com/ios/20171103/21039.html 在项目中,为了好的用户体验,有些场景必须线判断网络状态,然后才能决定改干嘛.比如视频播放,需要线判断 ...
- 【转】给word中的代码着色
基本操作 1)用Notepad++直接编辑代码文件,注意文件后缀,比如.cpp是C++程序,.m是Matlab,linux文件是.sh,写对后缀表示的文件类型,才有对应的语法高亮效果. 2)选中需要的 ...
- Linux之查看系统配置命令
1.查看CPU个数: [root@ifusion ~]# lscpu 2.查看CPU进程数.核数: [root@ifusion ~]# cat /proc/cpuinfo 3.查看内存总量: [roo ...
- js手机短信验证
贴代码之前,我们先讲一下这里我们用到的技术主要有1个.setInterval(),这个方法可以实现倒计时的效果. css: .weui_btn_disabled.weui_btn_default { ...
- webservice服务的提供及调用完整代码示例
服务提供方: applicationContext.xml applicationContext-webService.xml 服务调用方:
- WebService实例-CRM系统提供WebService实现用户注册功能
<—start—> 编写crm的webservice接口,实现客户信息保存操作.在CustomerService接口中新增一个服务接口,用于添加客户注册的信息. @Path("/ ...
- input type=date时,时间数据回填,报错The specified value "2019-0404-18" does not conform to the required format, "yyyy-MM-dd".
<input autocomplete id="start-time" name="start_time" type="date" c ...
- PS中如何把图片颜色加到字体上去
1.在PS中的图层中,将图片置于文字层的上方,同时按ctrl+alt+g键,这样就将文字范围以外的图像给剪切掉了.见附图下方的效果. 2.最终效果如下图: 参见:https://zhidao.baid ...
- java学习之—链表(2)
/** * 双端链表操作 * Create by Administrator * 2018/6/14 0014 * 下午 2:05 **/ class Link1 { public long dDat ...