USACO 3.4 Electric Fence
用一根平行x軸的直線掃描三角形,求交點,然后計算中間點的數,求和即可。
#include?<iostream>
#include?<fstream>
#include?<cmath>
using?namespace?std;
ifstream?fin("fence9.in");
ofstream?fout("fence9.out");
#ifdef?_DEBUG
#define?out?cout
#define?in?cin
#else
#define?out?fout
#define?in?fin
#endif
void?solve()
{
????int?n,m,p;
????in>>n>>m>>p;
????int?res?=?0;
????int?s,e;
????for(int?y=1;y<m;++y){
????????s?=?n*y/m+1;
????????e?=?(?y*(n-p)%m==0?)???(y*(n-p)/m-1+p)?:?floor(((double)y*(n-p)/m)+p);
??????//??out<<y<<"?"<<s<<"?"<<e<<endl;
????????res?+=?max(0,(e-s+1));
????}
????out<<res<<endl;
}
int?main(int?argc,char?*argv[])
{
????solve();?
????return?0;
}
#include?<fstream>
#include?<cmath>
using?namespace?std;
ifstream?fin("fence9.in");
ofstream?fout("fence9.out");
#ifdef?_DEBUG
#define?out?cout
#define?in?cin
#else
#define?out?fout
#define?in?fin
#endif
void?solve()
{
????int?n,m,p;
????in>>n>>m>>p;
????int?res?=?0;
????int?s,e;
????for(int?y=1;y<m;++y){
????????s?=?n*y/m+1;
????????e?=?(?y*(n-p)%m==0?)???(y*(n-p)/m-1+p)?:?floor(((double)y*(n-p)/m)+p);
??????//??out<<y<<"?"<<s<<"?"<<e<<endl;
????????res?+=?max(0,(e-s+1));
????}
????out<<res<<endl;
}
int?main(int?argc,char?*argv[])
{
????solve();?
????return?0;
}
posted on 2009-07-10 22:19 YZY 閱讀(337) 評論(0) 編輯 收藏 引用 所屬分類: Algorithm 、USACO