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;
}
}

最新文章

  1. 关于sqlserver 2008 无法远程连接的问题
  2. 深入理解JavaScript系列:为什么03-0.2不等于0.1
  3. 【BZOJ 1014】【JSOI 2008】火星人prefix
  4. 使用socket方式连接Nginx优化php-fpm性能
  5. Client默认用户及登录密码(转)
  6. 数据标准化 Normalization
  7. json格式的转换为json字符串函数
  8. 初级node+express建站
  9. iOS开发——免证书调试(Xcode7,iOS9)
  10. rails跑通第一个demo
  11. UVA10375 Choose and divide 质因数分解
  12. thrift实现HDFS文件操作
  13. error C4996: &#39;strcpy&#39;: This function or variable may be unsafe.
  14. 无法确定条件表达式的类型,因为“&lt;null&gt;”和“System.DateTime”之间没有隐式转换----解决办法
  15. 返回表达式列表中最小值least(exp1,exp2,exp3,&hellip;&hellip;,expn)
  16. 最近IOS10.2.1 iphone6 无法通过appStore 来更新 下载任何APP。好烦啊。
  17. SpringBoot框架的权限管理系统
  18. Javassist学习总结
  19. Python生成器笔记
  20. SQL学习之SqlMap SQL注入

热门文章

  1. 缓存-MemoryCache Class
  2. Spring中jdbc Template使用
  3. Java clone() 浅拷贝 深拷贝
  4. Windows下IntelliJ IDEA中调试Spark Standalone
  5. 1014C程序语法树
  6. ZOJ 2060 A-Fibonacci Again
  7. 【C】多线程编程笔记
  8. windows下的coreseek安装及PHP调用入门
  9. JMS学习一(JMS介绍)
  10. (转自大富翁)LockType 属性