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的思路,而且是每次切成两半向下递归.好像在哪里见过类似的,貌似是紫书的样子. 再想想好像就很显然的 ...
随机推荐
- 简单JavaScript小程序
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> ...
- sudo详细介绍
目录参数所在/etc/sudoers 1.Host_Alias定义主机别名 例:Host_Alias FILESERVERS = fs1,fs2 #注意“=”号两边要有空格隔开 ***由于现今li ...
- Python-基础-day2
Python环境的安装 安装Python: windows: 1.下载安装包 https://www.python.org/downloads/ 2.安装 默认安装路径:C:\pyth ...
- 00064_字符串缓冲区_StringBuffer类
1.StringBuffer类 (1)StringBuffer又称为可变字符序列,它是一个类似于 String 的字符串缓冲区,通过某些方法调用可以改变该序列的长度和内容. (2)tringBuffe ...
- AJAX发送POST请求,请求提交后Method从POST变成GET
服务器如果返回301或者302状态码,所有请求方法都会切换成GET头部的location如果要保证重定向后的请求方法,需要在服务端返回307(临时)或者308(永久)状态码,这两个状态码不会更改原请求 ...
- ASP.NET学习笔记01
ASP.NET初级工程师的核心要求:能够实现一个基本的网站. ASP.NET初级工程师面试主要要求: 1.基础的数据结构和算法 2.C#编程语言基础 3.网站基础(HTML,CSS,Javascrip ...
- 洛谷 U6850 手机密码
U6850 手机密码 题目背景 小明的手机上设了一个由四个数字组成的密码,但是小明自己的记性不好,但又不想把密码直接记在纸上,于是便想了一个方法. 题目描述 小明有四行数字,每行数字都有n[i](&l ...
- 【云快讯】之四十八《IBM和Cisco最新收购,加强Openstack易用能力》
2015-06-08 张晓东 东方云洞察 点击上面的链接文字,能够高速关注"东方云洞察"公众号 本周宣布的两起收购引人注意.思科购买Piston云计算公司.同期IBM的收购Blue ...
- Mysql第四天 数据库设计
不考虑主备.集群等方案,基于业务上的设计主要是表结构及表间关系的设计. 而关于表中字段主要是依据业务来进行定义,我们能够指定的大概有这么几项: 存储引擎 一般用InnoDB,特殊需求特殊选用 字符集和 ...
- HDU 3861--The King’s Problem【scc缩点构图 && 二分匹配求最小路径覆盖】
The King's Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...