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的更多相关文章

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

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

  2. Codeforces Round #540 (Div. 3)--1118C - Palindromic Matrix

    https://codeforces.com/contest/1118/problem/C 在查找元素的时候,必须按4,2,1的顺序进行.因为,如果先找1,可能就把原来的4拆散了,然后再找4,就找不到 ...

  3. 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 ...

  4. Codeforces Round #540 (Div. 3)--1118D2 - Coffee and Coursework (Hard Version)

    https://codeforces.com/contest/1118/problem/D2 和easy version的主要区别是,数据增加了. easy version采用的是线性查找,效率低 在 ...

  5. Codeforces Round #540 (Div. 3)--1118D1 - Coffee and Coursework (Easy version)

    https://codeforces.com/contest/1118/problem/D1 能做完的天数最大不超过n,因为假如每天一杯咖啡,每杯咖啡容量大于1 首先对容量进行从大到小的排序, sor ...

  6. Codeforces Round #540 (Div. 3)题解

    题目链接: https://codeforces.com/contest/1118 A题: 题意: q次查询,给你一个n,要你用1和2来凑出n,1的花费为a,2的花费为b,求花费的最小值. 思路: 我 ...

  7. 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 ...

  8. 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 ...

  9. Codeforces Round #540 (Div. 3) C. Palindromic Matrix 【暴力】

    任意门:http://codeforces.com/contest/1118/problem/C C. Palindromic Matrix time limit per test 2 seconds ...

随机推荐

  1. Sagheer and Nubian Market CodeForces - 812C (二分)

    On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friend ...

  2. Linux下安装redis的详细过程(redis版本为4.0.10)

    1.安装redis步骤 1.推荐进入到linux路径/usr/local/src 2.$ wget http://download.redis.io/releases/redis-4.0.10.tar ...

  3. Memcached 集群架构与memcached-session-manager

    Memcached 集群架构方面的问题_知识库_博客园https://kb.cnblogs.com/page/69074/ memcached-session-manager配置 - 学习中间件调优管 ...

  4. JavaMail入门第一篇 邮件简介及API概述

    现如今,电子邮件在我们的生活当中扮演着越来越重要的角色,我们每个人几乎都会与其打交道(至少时不时我们都会接收到莫名其妙的垃圾邮件),在工作中,使用邮件进行交流沟通,可以使我们的工作有迹可循,也显的较为 ...

  5. Microsoft Visual Studio Ultimate 2013密钥

    Visual Studio Ultimate 2013 KEY(密钥):BWG7X-J98B3-W34RT-33B3R-JVYW9Visual Studio Premium 2013 KEY(密钥): ...

  6. Spring boot 全局配置文件application.properties

    #更改Tomcat端口号 server.port=8090 #修改进入DispatcherServlet的规则为:*.htmlserver.servlet-path=*.html#这里要注意高版本的s ...

  7. WPF 如何创建自己的WPF自定义控件库

    在我们平时的项目中,我们经常需要一套自己的自定义控件库,这个特别是在Prism这种框架下面进行开发的时候,每个人都使用一套统一的控件,这样才不会每个人由于界面不统一而造成的整个软件系统千差万别,所以我 ...

  8. 从git中删除 .idea 目录

    将.idea目录加入ignore清单: $ echo '.idea' >> .gitignore   从git中删除idea: $ git rm —cached -r .idea 3 将. ...

  9. Maven 项目 无缘无故报错:版本冲突,其他机器上正常-提交的时候报冲突怎么也解决不掉

    2018年: maven突然之间报错了,显示版本冲突,但是其他的机器是好的, 使用命令:mvn compile -P dev -e; 看看测试环境有没有问题,还是有问题.而且,刚开始只是报错:erro ...

  10. Ajax之Jquery封装使用举例

    <html> <head> <meta charset="UTF-8"> <title>登陆页面</title> < ...