比赛传送门

A. Make a triangle!

题目大意:给你三根木棒,选出其中一根木棒增加它的长度,使构成三角形,问增加的长度最小是多少。

思路:签到题,根据样例/三角形性质不难发现,答案就是最长边减剩下两边之和加一。

 #include<cstdio>
#include<algorithm> using namespace std; int seq[]; int main()
{
for(int i=;i<=;i++) scanf("%d",&seq[i]);
sort(seq+,seq++);
if(seq[]+seq[]>seq[]){printf("");return ;}
printf("%d",seq[]-(seq[]+seq[])+);
return ;
}

A

B. Equations of Mathematical Magic

题目大意:多组数据,每组给你一个$a$,求$a-(a xor x)-x=0$方程的解的个数。

思路:开始发现答案范围在0~a用二进制表示的几位最大的数(即a在二进制下有x位,则上限为$2^x$。)于是打了个200的表,发现与a的二进制表示下有多少个1有关。但是验证这个想法的时候,实现出锅了,于是耽搁了好久...后来发现自己的想法是正确的。设a的二进制表示下有x个1,那么最终答案就是$2^x$。

 #include<cstdio>
#include<algorithm> using namespace std; int T,a; int work(int x)
{
int tmp=x,cnt=;
while(tmp)
{
if(tmp%==) cnt++;
tmp/=;
}
return cnt;
} int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d",&a);
int hu=work(a);
int ans=(<<hu);
printf("%d\n",ans);
}
return ;
}

B

C. Oh Those Palindromes

题目大意:给你一个字符串,你可以对其中的字符进行任意调换顺序,求变换后的一个字符串,使得新字符串有最多的回文子串。(注意,可能有多个,输出一个即可)

思路:想这个题一个多小时都没搞出来...结束后发现核心代码只有5行??被样例蒙蔽了...感觉还需要不同的字母间匹配什么的,其实只需要把字符串中相同的字符排在一起就行了。

One possible solution is just to sort the string.

Why so?

Note that each palindrome have equal character at their ends. Suppose this character is cc with xx number of occurences. Then there are at most x(x+1)/2 palindromes with this character.

So we have a clear upper bound on answer. It is easy to see, that the sorted string fulfills that bound and hence(因此) it is the optimal(理想的) answer.(官方题解)

至于为什么是x(x+1)/2....

Since 'c' has x number of occurrences, thus palindrome with 'c' are x , palindrome which are 'cc' are x-1 , palindrome which are 'ccc' are x-2 and so on.

so we get x+x-1+....+2+1 = x(x+1)/2 for 'c' which has frequency x.

简单的加法原理==。

唉被这道题坑了,于是我现在就是真·pupil了...

 #include<cstdio>
#include<algorithm>
#include<iostream> using namespace std; int n;
string x; int main()
{
scanf("%d",&n);
cin>>x;
sort(x.begin(),x.end());
cout<<x;
return ;
}

C

D. Labyrinth

题目大意:在一个棋盘中,你从起点出发,问能到达多少点,但有向左走不超过$x$步,向右走不超过$y$步的限制。

思路1:如果没有那个限制,就是简单的$bfs$了。然后看了一位Chinese大神写的,每次我们要想使答案最优,一定要尽量走左右移动少的。

于是就可以用优先队列维护$l+r$少的状态,进行$bfs$。

 #include<cstdio>
#include<algorithm>
#include<queue> using namespace std; int n,m,r,c,llim,rlim,ans;
int dx[]={,-,,,};
int dy[]={,,,,-};
bool vis[][];
char tmp[],mapp[][];
struct node{
int x,y,l,r;
};
bool operator < (const node &x,const node &y)
{
return x.l+x.r>y.l+y.r;
} bool valid(int x,int y)
{
if(x>=&&x<=n&&y>=&&y<=m) return ;
return ;
} void bfs(int r,int c)
{
priority_queue<node>q;
node p;
p.x=r,p.y=c,p.l=,p.r=;
q.push(p);vis[r][c]=;
while(!q.empty())
{
node u=q.top();q.pop();
for(int i=;i<=;i++)
{
int nowx=u.x+dx[i];
int nowy=u.y+dy[i];
int nowl=u.l+(dy[i]==-);
int nowr=u.r+(dy[i]==);
if(!valid(nowx,nowy)||mapp[nowx][nowy]=='*') continue;
if(nowl>llim||nowr>rlim) continue;
if(!vis[nowx][nowy])
{
vis[nowx][nowy]=;
node tmp;
tmp.x=nowx,tmp.y=nowy;
tmp.l=nowl,tmp.r=nowr;
q.push(tmp);
}
}
}
} int main()
{
scanf("%d%d",&n,&m);
scanf("%d%d",&r,&c);
scanf("%d%d",&llim,&rlim);
for(int i=;i<=n;i++)
{
scanf("%s",tmp+);
for(int j=;j<=m;j++) mapp[i][j]=tmp[j];
}
bfs(r,c);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
if(vis[i][j]) ans++;
printf("%d",ans);
return ;
}

D

还有一个很妙的想法:https://www.cnblogs.com/zwfymqz/p/9789569.html

这次是真惨..C题没做出来,成功成为了pupil....

话说我怎么这么菜啊

Codeforces Round #516 Div2 (A~D)By cellur925的更多相关文章

  1. Codeforces Round#498(Div.3)D. Two Strings Swaps

    题目 题意是给了两个字符串a和b,然后可以对这两个字符串有三种操作来使这两个字符串相等,一是交换a[i]和b[i],二是交换a[i]和a[n-i+1],三是交换b[i]和b[n-i+1],这三个操作都 ...

  2. CodeForces Round #516 Div2 题解

    A. Make a triangle! 暴力... 就是给你三个数,你每次可以选一个加1,问最少加多少次能构成三角形 #include <bits/stdc++.h> #define ll ...

  3. Codeforces Round #539 div2

    Codeforces Round #539 div2 abstract I 离散化三连 sort(pos.begin(), pos.end()); pos.erase(unique(pos.begin ...

  4. 【前行】◇第3站◇ Codeforces Round #512 Div2

    [第3站]Codeforces Round #512 Div2 第三题莫名卡半天……一堆细节没处理,改一个发现还有一个……然后就炸了,罚了一啪啦时间 Rating又掉了……但是没什么,比上一次好多了: ...

  5. Codeforces 828B Black Square(简单题)

    Codeforces 828B Black Square(简单题) Description Polycarp has a checkered sheet of paper of size n × m. ...

  6. Codeforces Round#320 Div2 解题报告

    Codeforces Round#320 Div2 先做个标题党,骗骗访问量,结束后再来写咯. codeforces 579A Raising Bacteria codeforces 579B Fin ...

  7. Codeforces C. Maximum Value(枚举二分)

    题目描述: Maximum Value time limit per test 1 second memory limit per test 256 megabytes input standard ...

  8. Codeforces Round #564(div2)

    Codeforces Round #564(div2) 本来以为是送分场,结果成了送命场. 菜是原罪 A SB题,上来读不懂题就交WA了一发,代码就不粘了 B 简单构造 很明显,\(n*n\)的矩阵可 ...

  9. Codeforces Round #516 (Div. 2, by Moscow Team Olympiad) D. Labyrinth(重识搜索)

    https://codeforces.com/contest/1064/problem/D 题意 给你一个有障碍的图,限制你向左向右走的次数,问你可以到达格子的个数 思路 可以定义状态为vi[x][y ...

随机推荐

  1. cocos2d-x 3.2 移植到android

    前人栽树,后人乘凉,这句话有点过了,只是想感谢一下为了移植cocos2d-x到android的"大婶"们所做出的贡献.          首先android环境需要配置好,需要的文 ...

  2. HDU 5478 Can you find it

    Can you find it Time Limit: 8000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  3. vs2010配置VL_FEAT库

    VL_FEAT库是计算机视觉中的一个开源库,支持C/C++,Matlab,可以在http://www.vlfeat.org/下载. 本文主要讲一下VS2010中如何配置vl_feat库(算是对原文的一 ...

  4. ORACLE 8i 遇到报错:ORA-01631: max # extents (505) reached in table

    近期在客户的一个8i生产库上使用statspack.发现alert中有报错: Mon Jun 16 13:17:52 2014 Errors in file /oracle/8.1.7/admin/p ...

  5. strsep strpbrk

    #include <stdio.h> #include <string.h> int main(void) { char s[] = "aa,bb,cc.11,22, ...

  6. Python调用C/Fortran混合的动态链接库--中篇

    接下来,介绍一个简单的例子,从fortran中传递并返回一维自定义结构体数组到python注意点:1.fortran新标准支持可分配数组作为变量传入并在subroutine或function分配后返回 ...

  7. Ctrl+Enter 选中文本提交

    <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> <bod ...

  8. 记一次OGG数据写入HBase的丢失数据原因分析

    一.现象二.原因排查2.1 SparkStreaming程序排查2.2 Kafka数据验证2.3 查看OGG源码2.3.1 生成Kafka消息类2.3.2 Kafka配置类2.3.3 Kafka 消息 ...

  9. #import @import #include

    1.在xcode5以后 ,Replace #import <Cocoa/Cocoa.h> with @import Cocoa; 在这之前 必须手动设置一下才能用. 2.#import 与 ...

  10. 书写优雅的shell脚本(五)- shell中(())双括号运算符

    在使用shell的逻辑运算符"[]"使用时候,必须保证运算符与算数之间有空格. 四则运算也只能借助:let,expr等命令完成. 今天讲的双括号"(())"结构 ...