链接:传送门

题意:给出3 × 3的方块,其中任意2 × 2的方块和左上角2 × 2的和相等,还给出9个格子中的4个——a,b,c,d ,在1~n中选择一些数(可重复)填入剩下5个格子中,问有多少种填法

思路:设5个 ?分别为x1,x2,x3,x4,x5 ,最后合并整理可以求得两个式子:

  1. x4 - x2 = a - b + c - d
  2. 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的更多相关文章

  1. codeforces 675B B. Restoring Painting(暴力枚举)

    题目链接: B. Restoring Painting time limit per test 1 second memory limit per test 256 megabytes input s ...

  2. Codeforces Round #353 (Div. 2) B. Restoring Painting 水题

    B. Restoring Painting 题目连接: http://www.codeforces.com/contest/675/problem/B Description Vasya works ...

  3. Codeforces Round #353 (Div. 2)Restoring Painting

    Vasya works as a watchman in the gallery. Unfortunately, one of the most expensive paintings was sto ...

  4. 【Codeforces 1132C】Painting the Fence

    Codeforces 1132 C 题意:给一些区间\([l_i,r_i]\),从中删掉两个,求剩下的区间最多能够覆盖的格子数量. 思路:首先枚举第一个删掉的区间,然后我们可以通过差分来求出每个格子被 ...

  5. Codeforces 898F - Restoring the Expression(字符串hash)

    898F - Restoring the Expression 思路:字符串hash,base是10,事实证明对2e64取模会T(也许ull很费时),对1e9+7取模. 代码: #include< ...

  6. Codeforces 448 C. Painting Fence

    递归.分治. . . C. Painting Fence time limit per test 1 second memory limit per test 512 megabytes input ...

  7. 【codeforces 509B】Painting Pebbles

    [题目链接]:http://codeforces.com/contest/509/problem/B [题意] 给n鹅卵石染色; 有k种颜色可供选择; 问你有没有染色方案; 使得各个堆的鹅卵石里面,第 ...

  8. codeforces#1187E. Tree Painting(树换根)

    题目链接: http://codeforces.com/contest/1187/problem/E 题意: 给出一颗树,找到一个根节点,使所有节点的子节点数之和最大 数据范围: $2 \le n \ ...

  9. Codeforces - 1198D - Rectangle Painting 1 - dp

    https://codeforces.com/contest/1198/problem/D 原来是dp的思路,而且是每次切成两半向下递归.好像在哪里见过类似的,貌似是紫书的样子. 再想想好像就很显然的 ...

随机推荐

  1. XSS Chanllenges 11-15

    Stage #11 根据提示,发现正则匹配,过滤掉了很多关键字 除on 事件和script 事件外,能执行js 代码的还有a 标签构造的超链接 构造 "><a href=java ...

  2. [ZOJ]3541 Last Puzzle (区间DP)

    ZOJ 3541 题目大意:有n个按钮,第i个按钮在按下ti 时间后回自动弹起,每个开关的位置是di,问什么策略按开关可以使所有的开关同时处于按下状态 Description There is one ...

  3. 【codeforces 807C】Success Rate

    [题目链接]:http://codeforces.com/contest/807/problem/C [题意] 给你4个数字 x y p q 要求让你求最小的非负整数b; 使得 (x+a)/(y+b) ...

  4. ElasticSearch[v6.2] 在实际项目中的应用

    摘要:本文所讲述的内容,为ElasticSearch(以下简称ES)全文搜索引擎在实际大数据项目的应用:ES的底层是开源库 Lucene.但是,你没法直接用 Lucene,必须自己写代码去调用它的接口 ...

  5. 线程锁的机制Lock

    java.util.concurrent.locks 接口Lock publci interface Lock Lock 实现提供了比使用synchronized方法和语句可获得的更加广泛的锁定操作, ...

  6. CF787A - The Monster

    /* CF787A - The Monster http://codeforces.com/contest/787/problem/A 数学 扩展欧几里得 注意x或y为0的时候要特判 并且结果要大于b ...

  7. Qt Quick 之 QML 与 C++ 混合编程具体解释

    Qt Quick 技术的引入.使得你能够高速构建 UI ,具有动画.各种绚丽效果的 UI 都不在话下.但它不是万能的.也有非常多局限性,原来 Qt 的一些技术,比方低阶的网络编程如 QTcpSocke ...

  8. 折腾开源WRT的AC无线路由之路-5

    -在Mac上设置无password连接SSH 1. 生成SSH密钥对 <pre name="code" class="html">ssh-keyge ...

  9. 2015.03.12,外语,读书笔记-《Word Power Made Easy》 10 “如何讨论交谈习惯”学习笔记 SESSION 25

    1.about keeping one's mouth shut taciturn,名词形式taciturnity,沉默寡言. 美国第30任总统库里奇,以沉默寡言著称.他来自新英格兰,那里视tacit ...

  10. poj3685(嵌套二分)

    Matrix Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 4658   Accepted: 1189 Descriptio ...