03 2013 档案
区间覆盖问题的贪心策略
摘要:区间覆盖问题数轴上有n个闭区间[ai,bi],选择尽量少的区间覆盖一条指定线段[s,t]。《算法竞赛入门经典》P154 重点说一下区间覆盖的贪心策略 首先将所有区间超出[s,t]的部分切掉,再对所有区间按a从大到小排序①,如果区间1的起点大于s,则无解。对n个区间扫描一次,代码:l = area[0].a;r = area[0].b; //把第一个区间的左右端点付给左右边界lr。②cnt = 1; //计数已记录下第一个区间for(i = 1;i < n;i++){ //从第二个区间开始扫描 if(area[i].a > r) break; //不能和当前区间链接,无解 else
阅读全文
UVa 10382 Watering Grass
摘要:#include<cstdio>#include<cmath>#include<algorithm>using namespace std;const int MAXN = 10001;struct node{ double s,t; bool operator < (const node& x) const { if(s!=x.s) return s <x.s; else return t >x.t; }};int main(){ int n,cnt; double w,l,p,r; struct node num[MAXN];
阅读全文
UVa 10340 All in All
摘要:数组需要开的很大#include<cstdio>#include<cstring>using namespace std;const int MAXN = 1000000;int main(){ char s1[MAXN],s2[MAXN]; while(scanf("%s%s",s1,s2)==2) { int i=0; for(int j=0;s1[i] && s2[j];j++) if(s1[i]==s2[j]) i++; if(s1[i]==0) printf("Yes\n"); else printf(&
阅读全文
UVa 10970 Big Chocolate
摘要:找规律 include<cstdio>using namespace std;int main(){ int n,m; while(scanf("%d%d",&n,&m)==2) printf("%d\n",n*m-1); return 0;}
阅读全文
UVa 1368 DNA Consensus String
摘要:题目比较难懂,求一个串使它到所有Si的距离和最小 #include<cstdio>#include<cstring>using namespace std;const int MAXN = 50;const int MAXM = 1001;int n,m,cnt[MAXM][27];int ham(char *s1,char *s2){ int c=0; for(int i=0;i<m;i++) if(s1[i]!=s2[i]) c++; return c;}int main(){ int T; char s[MAXM]; scanf("%d"
阅读全文
UVa 1339 Ancient Cipher
摘要:#include<cstdio>#include<algorithm>using namespace std;const int MAXN = 500000;struct node{ int i; bool is; bool operator < (const node& x)const { return i>x.i; }};int main(){ int T; int n; struct node a[MAXN]; scanf("%d",&T); while(T--) { scanf("%d",&am
阅读全文
UVa 11039 Building designing
摘要:#include<cstdio>#include<algorithm>using namespace std;const int MAXN = 500000;struct node{ int i; bool is; bool operator < (const node& x)const { return i>x.i; }};int main(){ int T; int n; struct node a[MAXN]; scanf("%d",&T); while(T--) { scanf("%d",&am
阅读全文
UVa 11636 Hello World!
摘要:#include<cstdio>#include<cmath>using namespace std;int main(){ int n,T=1; while(scanf("%d",&n),n>0) { printf("Case %d: %d\n",T++,(int)ceil(log10(n)/log10(2))); //换底公式 } return 0;}
阅读全文
浙公网安备 33010602011771号