Codeforces 675B Restoring Painting
链接:传送门
题意:给出3 × 3的方块,其中任意2 × 2的方块和左上角2 × 2的和相等,还给出9个格子中的4个——a,b,c,d ,在1~n中选择一些数(可重复)填入剩下5个格子中,问有多少种填法
思路:设5个 ?分别为x1,x2,x3,x4,x5 ,最后合并整理可以求得两个式子:
- x4 - x2 = a - b + c - d
- x5 - x1 = a + b - c - d
先遍历x4 然后二分 x2 ,O(2nlog(n))的复杂度,理论上是没问题的
/*************************************************************************
> File Name: test1.cpp
> Author: WArobot
> Blog: http://www.cnblogs.com/WArobot/
> Created Time: 2017年04月17日 星期一 19时53分46秒
************************************************************************/
#include<bits/stdc++.h>
using namespace std;
int n,a,b,c,d;
int x1,x2,x4,x5;
int s1,s2;
int flag[100010];
int main(){
for(int i=0;i<100010;i++) flag[i] = i+1;
while(scanf("%d%d%d%d%d",&n,&a,&b,&c,&d)!=EOF){
int cnt1 = 0 , cnt2 = 0;
s1 = a-b+c-d; s2 = a+b-c-d;
for(x4=1;x4<=n;x4++){
if( binary_search(flag,flag+n,x4-s1) )
cnt1++;
}
for(x5=1;x5<=n;x5++){
if( binary_search(flag,flag+n,x5-s2) )
cnt2++;
}
printf("cnt1 = %d , cnt2 = %d\n",cnt1,cnt2);
int t = min(cnt1,cnt2);
long long ans = t;
ans *= n;
cout<<ans<<endl;
}
return 0;
}
这样就完了?这样就结束了?还有更好的方法吗?当然有......
对于x4 - x2 = s1,有必要算x2吗?很显然没必要,只需要判断x2在符合的区间内就ok了,O(2n)还二分?被嘲讽为草履虫了......
/*************************************************************************
> File Name: d.cpp
> Author: WArobot
> Blog: http://www.cnblogs.com/WArobot/
> Created Time: 2017年04月17日 星期一 23时55分05秒
************************************************************************/
#include<bits/stdc++.h>
using namespace std;
int n,a,b,c,d,s1,s2,x4,x5;
int main(){
while(scanf("%d%d%d%d%d",&n,&a,&b,&c,&d)!=EOF){
s1 = a-b+c-d; s2 = a+b-c-d;
int cnt1 = 0 , cnt2 = 0;
// x4 - x2 = s1
for(x4=1;x4<=n;x4++)
if(x4-s1>=1 && x4-s1<=n) cnt1++;
// x5 - x1 = s2
for(x5=1;x5<=n;x5++)
if(x5-s2>=1 && x5-s2<=n) cnt2++;
long long ans = min(cnt1,cnt2);
ans *= n;
cout<<ans<<endl;
}
return 0;
}
Codeforces 675B Restoring Painting的更多相关文章
- codeforces 675B B. Restoring Painting(暴力枚举)
题目链接: B. Restoring Painting time limit per test 1 second memory limit per test 256 megabytes input s ...
- Codeforces Round #353 (Div. 2) B. Restoring Painting 水题
B. Restoring Painting 题目连接: http://www.codeforces.com/contest/675/problem/B Description Vasya works ...
- Codeforces Round #353 (Div. 2)Restoring Painting
Vasya works as a watchman in the gallery. Unfortunately, one of the most expensive paintings was sto ...
- 【Codeforces 1132C】Painting the Fence
Codeforces 1132 C 题意:给一些区间\([l_i,r_i]\),从中删掉两个,求剩下的区间最多能够覆盖的格子数量. 思路:首先枚举第一个删掉的区间,然后我们可以通过差分来求出每个格子被 ...
- Codeforces 898F - Restoring the Expression(字符串hash)
898F - Restoring the Expression 思路:字符串hash,base是10,事实证明对2e64取模会T(也许ull很费时),对1e9+7取模. 代码: #include< ...
- Codeforces 448 C. Painting Fence
递归.分治. . . C. Painting Fence time limit per test 1 second memory limit per test 512 megabytes input ...
- 【codeforces 509B】Painting Pebbles
[题目链接]:http://codeforces.com/contest/509/problem/B [题意] 给n鹅卵石染色; 有k种颜色可供选择; 问你有没有染色方案; 使得各个堆的鹅卵石里面,第 ...
- codeforces#1187E. Tree Painting(树换根)
题目链接: http://codeforces.com/contest/1187/problem/E 题意: 给出一颗树,找到一个根节点,使所有节点的子节点数之和最大 数据范围: $2 \le n \ ...
- Codeforces - 1198D - Rectangle Painting 1 - dp
https://codeforces.com/contest/1198/problem/D 原来是dp的思路,而且是每次切成两半向下递归.好像在哪里见过类似的,貌似是紫书的样子. 再想想好像就很显然的 ...
随机推荐
- 实验一:JAVA实验环境搭建 ,JDK下载与安装及 Eclipse下载与安装
一.搭建JAVA实验环境 1.JDK的下载 (1)打开 IE 浏览器,输入网址“http://www.oracle.com/index.html”,浏览 Oracle 官方主页.鼠标双击Downloa ...
- Project Euler 38 Pandigital multiples
题意: 将192分别与1.2.3相乘: 192 × 1 = 192192 × 2 = 384192 × 3 = 576 连接这些乘积,我们得到一个1至9全数字的数192384576.我们称192384 ...
- CentOS 笔记(六) 历史命令 自动补充
history #出现历史的命令 #执行具体的一条历史命令 !458 ②安装自动补充功能 yum install -y bash-completion #输入命令 按Tab 会显示所有内容 syste ...
- gpio_request 原形代码
http://blog.csdn.net/maopig/article/details/7428561 其原型为 int gpio_request(unsigned gpio, const char ...
- 【hdu 6351】Beautiful Now
[链接] 我是链接,点我呀:) [题意] 你可以最多交换k次数字. 让你组成一个最大的和一个最小的数字. [题解] 直接写个bfs.求出所有状态的最小交换次数. 但是最大值和最小值分开写. 做最大值的 ...
- PHP学习总结(10)——PHP入门篇之自定义网站根目录
- mongodb--linux下的安装
linux下ubuntu的安装及启动 sudo apt-get upgrade sudo apt-get udpate sudo apt-get install mongodb 用 命令查看一下mon ...
- HDU——T1231 最大连续子序列
http://acm.hdu.edu.cn/showproblem.php?pid=1231 Problem Description 给定K个整数的序列{ N1, N2, ..., NK },其任意连 ...
- (转)关于使用iText导出pdf
一.iText简介 iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成PDF或rtf的文档,而且可以将XML.Html文 ...
- 【转】C# ArcgisEngine开发中,对一个图层进行过滤,只显示符合条件的要素
有时候,我们要对图层上的地物进行有选择性的显示,以此来满足实际的功能要求. 按下面介绍的方法可轻松实现图层属性过滤显示: 1.当图层已经加载时 private void ShowByFilter(Ax ...