求 n (1 <= n <= 100)
个矩形的面积并。 题目链接:
http://poj.org/problem?id=1151
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
![](/Images/OutliningIndicators/None.gif)
const int maxn = 210;
const double eps = 1e-9;
![](/Images/OutliningIndicators/None.gif)
struct line_t
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
double l_x, r_x, y;
bool flag;
};
![](/Images/OutliningIndicators/None.gif)
bool cmp(const line_t &a, const line_t &b)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
return a.y < b.y;
}
![](/Images/OutliningIndicators/None.gif)
bool is_equal(const double &a, const double &b)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
return abs(a - b) < eps;
}
![](/Images/OutliningIndicators/None.gif)
int n;
double x[maxn];
int cnt_x, cnt_line;
line_t line[maxn];
![](/Images/OutliningIndicators/None.gif)
int main()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
int num = 0;
while(scanf("%d", &n) != EOF && n)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cnt_x = 0;
cnt_line = 0;
for(int i = 0; i < n; ++i)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
double x1, y1, x2, y2;
scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
x[cnt_x++] = x1;
x[cnt_x++] = x2;
line[cnt_line].flag = false;
line[cnt_line].l_x = x1;
line[cnt_line].r_x = x2;
line[cnt_line++].y = y1;
line[cnt_line].flag = true;
line[cnt_line].l_x = x1;
line[cnt_line].r_x = x2;
line[cnt_line++].y = y2;
}
sort(line,line + cnt_line, cmp);
sort(x, x + cnt_x);
cnt_x = unique(x, x + cnt_x, is_equal) - x;
double area = 0.0;
for(int i = 0; i < cnt_x - 1; ++i)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int cnt = 0;
double now_y;
for(int j = 0; j < cnt_line; ++j)
if(line[j].l_x <= x[i] && line[j].r_x >= x[i + 1])
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(cnt == 0) now_y = line[j].y;
if(!line[j].flag) ++cnt;
else --cnt;
if(cnt == 0) area += (x[i + 1] - x[i]) * (line[j].y - now_y);
}
}
printf("Test case #%d\n", ++num);
printf("Total explored area: %.2lf\n\n", area);
}
return 0;
}
![](/Images/OutliningIndicators/None.gif)
posted @
2011-10-08 10:59 wuxu 阅读(228) |
评论 (0) |
编辑 收藏
#include<iostream>
#include<string>
#include<stack>
using namespace std;
![](/Images/OutliningIndicators/None.gif)
string DecToBinary(const string &dec)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
int i,len,sta;
stack<int> s;
len=dec.length();
int *num=new int[len+1];
for(i=0;i<len;++i)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
num[i]=dec[i]-'0';
}
while(true)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
sta=len;
for(i=0;i<len;++i)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(num[i]!=0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
sta=i;
break;
}
}
if(sta==len) break;
int remain=0;
for(i=sta;i<len;++i)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
remain=remain*10+num[i];
num[i]=remain/2;
remain=remain%2;
}
s.push(remain);
}
string ans;
while(!s.empty())
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ans+=s.top()+'0';
s.pop();
}
if(ans.length()==0)
ans="0";
delete [] num;
return ans;
}
![](/Images/OutliningIndicators/None.gif)
int main()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
string dec,bin;
while(cin>>dec)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
bin=DecToBinary(dec);
cout<<bin<<endl;
}
return 0;
}
![](/Images/OutliningIndicators/None.gif)
posted @
2011-07-31 15:37 wuxu 阅读(825) |
评论 (0) |
编辑 收藏
#include<iostream>
#include<string>
using namespace std;
![](/Images/OutliningIndicators/None.gif)
string BigIntegerMod(const string &str1,const string &str2) // str1%str2
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
int i;
int len,len1,len2,index;
bool flag;
![](/Images/OutliningIndicators/InBlock.gif)
len1=str1.length();
len2=str2.length();
len=len1-len2;
if(len<0) return str1;
![](/Images/OutliningIndicators/InBlock.gif)
int *num1=new int[len1+2];
int *num2=new int[len2+2];
![](/Images/OutliningIndicators/InBlock.gif)
memset(num1,0,sizeof(int)*(len1+2));
memset(num2,0,sizeof(int)*(len2+2));
![](/Images/OutliningIndicators/InBlock.gif)
for(i=len1-1,index=0;i>=0;--i)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
num1[index++]=str1[i]-'0';
}
for(i=len2-1,index=0;i>=0;--i)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
num2[index++]=str2[i]-'0';
}
![](/Images/OutliningIndicators/InBlock.gif)
while(true)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
for(i=len1-1;i>=0;i--)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(num1[i])
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
len1=i+1;
break;
}
if(i==0) len1=0;
}
![](/Images/OutliningIndicators/InBlock.gif)
len=len1-len2;
if(len<0) break;
![](/Images/OutliningIndicators/InBlock.gif)
flag=false;
index=0;
![](/Images/OutliningIndicators/InBlock.gif)
for(i=len1-1;i>=len;i--)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(num1[i]==num2[i-len]) continue;
else if(num1[i]<num2[i-len])
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
flag=true;
break;
}
else break;
}
![](/Images/OutliningIndicators/InBlock.gif)
if(flag) --len;
if(len<0) break;
![](/Images/OutliningIndicators/InBlock.gif)
while(++index)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
flag=false;
for(i=len1-1;i>=len;i--)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(num1[i]==num2[i-len]) continue;
else if(num1[i]<num2[i-len])
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
flag=true;
break;
}
else break;
}
if(flag)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
--index;
break;
}
![](/Images/OutliningIndicators/InBlock.gif)
for(i=len;i<len1;i++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
num1[i]-=num2[i-len];
if(num1[i]<0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
num1[i]+=10;
--num1[i+1];
}
}
}
![](/Images/OutliningIndicators/InBlock.gif)
if(index==0) break;
}
![](/Images/OutliningIndicators/InBlock.gif)
string ans;
flag=false;
for(i=len1;i>=0;--i)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(flag||num1[i])
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
flag=true;
ans+='0'+num1[i];
}
}
if(!flag) ans="0";
![](/Images/OutliningIndicators/InBlock.gif)
delete [] num1;
delete [] num2;
![](/Images/OutliningIndicators/InBlock.gif)
return ans;
}
![](/Images/OutliningIndicators/None.gif)
int main()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
string str,str1,str2;
while(cin>>str1>>str2)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
str=BigIntegerMod(str1,str2);
cout<<str<<endl;
}
return 0;
}
![](/Images/OutliningIndicators/None.gif)
posted @
2011-07-31 15:24 wuxu 阅读(491) |
评论 (0) |
编辑 收藏
摘要: Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->#include<iostream>#include<string>using namespace std;string BigIntegerDiv(...
阅读全文
posted @
2011-07-31 15:22 wuxu 阅读(976) |
评论 (0) |
编辑 收藏
#include<iostream>
#include<string>
using namespace std;
![](/Images/OutliningIndicators/None.gif)
string BigIntegerMult(const string &str1,const string &str2)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
string ans;
int index,i,j;
int len1,len2;
![](/Images/OutliningIndicators/InBlock.gif)
len1=str1.length();
len2=str2.length();
![](/Images/OutliningIndicators/InBlock.gif)
unsigned int *total=new unsigned int[len1+len2+2];
unsigned int *integ1=new unsigned int[len1];
unsigned int *integ2=new unsigned int[len2];
![](/Images/OutliningIndicators/InBlock.gif)
index=0;
for(i=len1-1;i>=0;--i)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
integ1[index++]=str1[i]-'0';
}
![](/Images/OutliningIndicators/InBlock.gif)
index=0;
for(i=len2-1;i>=0;--i)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
integ2[index++]=str2[i]-'0';
}
memset(total,0,sizeof(unsigned int)*(len1+len2+2));
![](/Images/OutliningIndicators/InBlock.gif)
for(i=0;i<len1;i++)
for(j=0;j<len2;j++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
total[i+j]+=integ1[i]*integ2[j];
}
![](/Images/OutliningIndicators/InBlock.gif)
for(i=0;i<len1+len2+1;i++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(total[i]>9)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
total[i+1]+=total[i]/10;
total[i]=total[i]%10;
}
}
bool flag=false;
for(i=len1+len2+1;i>=0;i--)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(flag||total[i])
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
flag=true;
ans+=total[i]+'0';
}
}
if(!flag)
ans+='0';
![](/Images/OutliningIndicators/InBlock.gif)
delete [] integ1;
delete [] integ2;
delete [] total;
![](/Images/OutliningIndicators/InBlock.gif)
return ans;
}
![](/Images/OutliningIndicators/None.gif)
int main()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
string str1,str2,str;
cin>>str1>>str2;
str=BigIntegerMult(str1,str2);
cout<<str<<endl;
return 0;
}
![](/Images/OutliningIndicators/None.gif)
posted @
2011-06-09 16:51 wuxu 阅读(323) |
评论 (0) |
编辑 收藏
摘要: /**//* sap+gap优化+当前弧优化, 采用邻接表+反向弧指针 时间复杂度:O(mn^2)*/#include<iostream>using namespace std;const int maxnode = 1024...
阅读全文
posted @
2011-05-10 13:17 wuxu 阅读(722) |
评论 (0) |
编辑 收藏
摘要: /**//* sap+gap优化,采用邻接矩正 时间复杂度:O(mn^2)*/#include<iostream>#include<queue>using namespace std;const int msize=205; &nbs...
阅读全文
posted @
2011-05-10 13:12 wuxu 阅读(456) |
评论 (0) |
编辑 收藏
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//*
EdmondsKarp算法,O(m^2n)
*/
#include<iostream>
#include<queue>
using namespace std;
const int maxn=205;
const int inf=0x7fffffff;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int r[maxn][maxn]; //残留网络,初始化为原图
bool visit[maxn];
int pre[maxn];
int m,n;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
bool bfs(int s,int t) //寻找一条从s到t的增广路,若找到返回true
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
int p;
queue<int > q;
memset(pre,-1,sizeof(pre));
memset(visit,false,sizeof(visit));
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
pre[s]=s;
visit[s]=true;
q.push(s);
while(!q.empty())
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
p=q.front();
q.pop();
for(int i=1;i<=n;i++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(r[p][i]>0&&!visit[i])
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
pre[i]=p;
visit[i]=true;
if(i==t) return true;
q.push(i);
}
}
}
return false;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int EdmondsKarp(int s,int t)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
int flow=0,d,i;
while(bfs(s,t))
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
d=inf;
for(i=t;i!=s;i=pre[i])
d=d<r[pre[i]][i]? d:r[pre[i]][i];
for(i=t;i!=s;i=pre[i])
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
r[pre[i]][i]-=d;
r[i][pre[i]]+=d;
}
flow+=d;
}
return flow;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int main()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
while(scanf("%d%d",&m,&n)!=EOF)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int u,v,w;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
memset(r,0,sizeof(r));/**////
for(int i=0;i<m;i++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
scanf("%d%d%d",&u,&v,&w);
r[u][v]+=w;
}
printf("%d\n",EdmondsKarp(1,n));
}
return 0;
}
posted @
2011-05-10 13:10 wuxu 阅读(289) |
评论 (0) |
编辑 收藏
摘要: /**//* dinic算法,边表存储(仿指针),实用于稀疏图,O(mn^2)*/#include<iostream>using namespace std;const int maxn=205; //最大定点数const int maxm=205...
阅读全文
posted @
2011-05-10 13:07 wuxu 阅读(273) |
评论 (0) |
编辑 收藏
摘要: /**//* dinic算法,邻接矩正,O(mn^2)*/#include<iostream>#include<algorithm>#include<queue>using namespace std;const int maxnode=205;const int&n...
阅读全文
posted @
2011-05-10 13:05 wuxu 阅读(321) |
评论 (0) |
编辑 收藏