UVA 12520 Square Garden
https://vjudge.net/problem/UVA-12520
题意:n*n网格中染色m个格子,染色格子的最长轮廓线
贪心
将格子分为4类
1、隔一个选一个,互不相邻的格子
2、4个角上的格子
3、边界除角的格子
4、内部的格子
4类从上到下依次选
1对答案有4的贡献
2对答案无贡献
3对答案有-2的贡献
4对答案有-4的贡献
对n奇偶分类讨论
当n为奇数时,在分左上角的格子在第1类还是第2类讨论
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
freopen("data.txt","r",stdin);
freopen("my.txt","w",stdout);
long long ans1,ans2,n,sum,rest,now,tmp;
while(scanf("%lld%lld",&n,&sum)!=EOF)
{
if(!n) return ;
if(n&)
{
if(sum<=n*n>> || sum<=n*n/+)
{
printf("%lld\n",sum<<);
continue;
}
if(sum<=n*n/+) ans1=n*n/*;
else
{
tmp=sum--n*n/;
ans1=n*n/<<;
rest=n/-<<;
if(tmp<=rest) ans1-=tmp<<;
else
{
ans1-=rest<<;
tmp-=rest;
ans1-=tmp<<;
}
}
ans2=n*n/+<<;
now=n*n/+;
rest=n/<<;
if(sum-now<=rest) ans2-=sum-now<<;
else
{
ans2-=rest<<;
now+=rest;
ans2-=sum-now<<;
}
printf("%lld\n",max(ans1,ans2));
}
else
{
if(sum<=n*n>>)
{
printf("%lld\n",sum<<);
continue;
}
if(sum<=n*n/+)
{
printf("%lld\n",n*n/*);
continue;
}
ans1=n*n>><<;
tmp=sum-n*n/-;
rest=n/-<<;
if(tmp<=rest) ans1-=tmp<<;
else
{
ans1-=rest<<;
tmp-=rest;
ans1-=tmp<<;
}
printf("%lld\n",ans1);
}
}
}
同一种思路大佬的写法就是短
#include<iostream>
#include<cstdio>
using namespace std;
typedef long long ll;
ll l,n,a,b,c,ans;
ll cal()
{
if(n<=a) return n*;
if(n<=a+b) return a*;
if(n<=a+b+c) return a*-(n-a-b)*;
return a*-c*-(n-a-b-c)*;
}
int main()
{
freopen("data.txt","r",stdin);
freopen("std.txt","w",stdout);
while(cin>>l>>n,l||n)
{
if(l%==)
{
a=l*l/;
b=;
c=l/-<<;
ans=cal();
}
else
{
a=l*l/;
b=l== ? :;
c=l== ? :(l-)/*;
ans=cal();
a=l*l/+;
b=;
c=(l-)/*;
ans=max(ans,cal());
}
cout<<ans<<endl;
}
}
UVA 12520 Square Garden的更多相关文章
- UVA 11542 - Square(高斯消元)
UVA 11542 - Square 题目链接 题意:给定一些数字.保证这些数字质因子不会超过500,求这些数字中选出几个,乘积为全然平方数,问有几种选法 思路:对每一个数字分解成质因子后.发现假设要 ...
- UVa 11461 - Square Numbers【数学,暴力】
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- UVA 11461 - Square Numbers 数学水题
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- xor方程组消元 UVA 11542 Square
题目传送门 题意:给n个数,选择一些数字乘积为平方数的选择方案数.训练指南题目. 分析:每一个数字分解质因数.比如4, 6, 10, 15,, , , , 令,表示选择第i个数字,那么,如果p是平方数 ...
- UVA 11461 - Square Numbers(水题)
题目链接 #include <cstdio> #include <cstring> #include <string> #include <cmath> ...
- UVA 11461 - Square Numbers
题目:统计区间中的平方数个数. 分析: ... #include <stdio.h> #include <string.h> ]; int main() { int i, a, ...
- UVA 1603 Square Destroyer
题意: 给定一个火柴棒拼成的方格阵,然后去掉一些火柴棒,问至少再去掉几根火柴棒能够让图中一个正方形都没有. 思路: 1. 由于题目中给定了 n 的范围,2 * n * (n + 1) <= 60 ...
- UVA 11542 Square ——线性基
[题目分析] 每个数没有超过500的因子.很容易想到把每一个数表示成一个二进制的数. (0代表该质数的次数为偶数,1代表是奇数) 然后问题转化成了选取一些二进制数,使他们的异或和为0. 高斯消元,2^ ...
- UVa 11542 Square (高斯消元)
题意:给定 n 个数,从中选出一个,或者是多个,使得选出的整数的乘积是完全平方数,求一共有多少种选法,整数的素因子不大于 500. 析:从题目素因子不超过 500,就知道要把每个数进行分解.因为结果要 ...
随机推荐
- Thunder——互评beta版本
基于NABCD和spec评论作品 Hello World!:http://www.cnblogs.com/vector121/p/7922989.html 欢迎来怼:http://www.cnblog ...
- tensorflow之曲线拟合
视频链接:https://morvanzhou.github.io/tutorials/machine-learning/ML-intro/ 1.定义层 定义 add_layer() from __f ...
- mosquitto启动时Address already in use 和 一般的 Address already in use
对于mosquitto启动时的地址占用错误,可能是mosquitto启动之后没关掉,进程一直占用了端口.查看mosquitto相关的进程,然后关掉就可启动了. 关掉mosquitto进程即可 参考: ...
- haproxy调度算法
调度算法详解 用balance指令指明调度算法, 例如:balance roundrobin 1:roundrobin :动态轮询算法,基于后端服务器的总权重做轮询,后端的服务器数量限制在4095 ...
- 学生导师互选系统(php代码规范)
学生导师互选系统(php代码规范) php编码规范 组名:一不小心就火了 负责项目:学生导师互选系统(安卓端) 编写目的 为了更好的提高团队的的合作效率,保证开发的有效性和合理性,并可最大程度的提高程 ...
- Android自定义XML属性以及遇到的命名空间的问题
转载请注明出处:http://www.cnblogs.com/kross/p/3458068.html 最近在做一些UI,很蠢很蠢的重复写了很多代码,比如一个自定义的UI Tab,由一个ImageVi ...
- Spring Boot 学习笔记 - 01
看了[纯洁的微笑]的博客后,我决定开始学好 Spring 体系了,真的是解决了饥渴的我.
- Spring boot整合shiro框架
ShiroConfiguration package com.energy.common.config; import java.util.LinkedHashMap; import java.uti ...
- centos中apache自用常用额外配置记录(xwamp)
xwamp套件中apache配置,记录下,以免忘记. 配置路径 ${wwwroot_dir}/conf/httpd.conf 配置内容 <ifmodule mod_deflate.c> D ...
- 51nod-1220-约数之和
题意 求 \[ \sum _{i=1}^n\sum _{j=1}^nd(ij) \\ d(x)=\sum _{e|x}e \] \(n\le 10^9\) . 分析 没有推出来.这题有几个要点要学习. ...