比赛传送门

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. java zip压缩文件和文件夹

    public class FileUtil { /** * 压缩文件-File * @param out zip流 * @param srcFiles 要压缩的文件 * @param path 相对路 ...

  2. 在linux中安装.sh 文件

    有一些linux的安装包是.sh后缀的安装包,可以通过$bash xxx.sh的命令来进行安装.

  3. Intel graphics processing units

    http://en.wikipedia.org/wiki/Comparison_of_Intel_graphics_processing_units Comparison of Intel graph ...

  4. (转)C中的volatile用法

    volatile 影响编译器编译的结果,指出,volatile 变量是随时可能发生变化的,与volatile变量有关的运算,不要进行编译优化,以免出错,(VC++ 在产生release版可执行码时会进 ...

  5. Linux Linker

    文章原文:http://zhidao.baidu.com/link?url=U2Mtcc6BKi4vuQ1MO8U6s9gNm4y9Epphz03veA2lVpRWMozyVdj0PYvw1ZU9qj ...

  6. safi 中placeholder不垂直居中

    用css hack将line-height 设置为1 例子: input{height: 32px; line-height: 32px; [;line-height: 1px;]};

  7. em和i , b和Strong 的区别

    这两对标签最大区别就是一个给搜索引擎看的,一个是给用户看的. b标签和strong标签给我们的主观感受都是加粗,但对搜索引擎来说b标签和普通的文字并没有什么区别,而strong标签却是起强调作用的. ...

  8. mysql_proxy

    mysql_proxy中间件实现:读写分离.负载均衡. mysql_proxy中间件实现:读写分离.负载均衡. 负载均衡:给多台数据库,看能不能均匀的分给不同的数据库. 客户端连的是proxy,此时的 ...

  9. Spring Boot 整合Filter

    两种方法 方法一: 正常创建好Filter类,配置完成 package clc.user.filter; import javax.servlet.Filter; import javax.servl ...

  10. Delphi通过POST传递参数给PHP

    Delphi代码 ******************************************************************************************* ...