标签:dig 排列 单位 平均值 net accept 实现 from cow
从有序数组中查找某个值
1 #include <cstdio> 2 #include <cctype> 3 #include <algorithm> 4 #define num s-‘0‘ 5 6 using namespace std; 7 8 const int MAX_N=100000; 9 const int INF=0x3f3f3f3f; 10 int n,k; 11 int a[MAX_N]; 12 13 void read(int &x){ 14 char s; 15 x=0; 16 bool flag=0; 17 while(!isdigit(s=getchar())) 18 (s==‘-‘)&&(flag=true); 19 for(x=num;isdigit(s=getchar());x=x*10+num); 20 (flag)&&(x=-x); 21 } 22 23 void write(int x) 24 { 25 if(x<0) 26 { 27 putchar(‘-‘); 28 x=-x; 29 } 30 if(x>9) 31 write(x/10); 32 putchar(x%10+‘0‘); 33 } 34 35 int search(int); 36 37 int main() 38 { 39 read(n);read(k); 40 for (int i=0; i<n; i++) read(a[i]); 41 int p = search(k); 42 write(p); 43 putchar(‘\n‘); 44 write(lower_bound(a,a+n,k)-a); 45 putchar(‘\n‘); 46 } 47 48 int search(int k) 49 { 50 int l=-1, r=n-1; 51 while (r-l>1) 52 { 53 int mid=(r+l)/2; 54 if (a[mid]>=k) r=mid; 55 else l=mid; 56 } 57 return r; 58 }
假定一个解并判断是否可行
对于任意满足C(x)的x,如果所有的x‘≥x也满足C(x‘)的话,我们就可以用二分搜索来求得最小的x。首先我们将区间的左端点初始化为不满足C(x)的值,右端点初始化为满足C(x)的值,然后每次取中点mid=(lb+ub)/2,判断C(mid)是否满足并缩小范围,直到(lb,ub]足够小了为止,最后ub就是要求的最小值。最大化的问题也可以用同样的方法求解。
Cable master(POJ 1064)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 65114 | Accepted: 13413 |
Description
Input
Output
Sample Input
4 11 8.02 7.43 4.57 5.39
Sample Output
2.00
1 #include <cstdio> 2 #include <cctype> 3 #include <algorithm> 4 #include <cmath> 5 #define num s-‘0‘ 6 7 using namespace std; 8 9 const int MAX_N=100000; 10 const int INF=100000; 11 int n,k; 12 double L[MAX_N]; 13 14 void read(int &x){ 15 char s; 16 x=0; 17 bool flag=0; 18 while(!isdigit(s=getchar())) 19 (s==‘-‘)&&(flag=true); 20 for(x=num;isdigit(s=getchar());x=x*10+num); 21 (flag)&&(x=-x); 22 } 23 24 void write(int x) 25 { 26 if(x<0) 27 { 28 putchar(‘-‘); 29 x=-x; 30 } 31 if(x>9) 32 write(x/10); 33 putchar(x%10+‘0‘); 34 } 35 36 double search(); 37 bool C(double x); 38 39 int main() 40 { 41 read(n);read(k); 42 for (int i=0; i<n; i++) scanf("%lf", &L[i]); 43 double p = search(); 44 printf("%.2f", floor(p*100)/100); 45 putchar(‘\n‘); 46 } 47 48 bool C(double x) 49 { 50 int sum=0; 51 for (int i=0; i<n; i++) 52 { 53 sum+=(int)(L[i]/x); 54 if (sum>=k) return true; 55 } 56 return false; 57 } 58 59 double search() 60 { 61 double lb=0, ub=INF; 62 //while (ub-lb>0.001) 63 for (int i=0; i<100; i++) 64 { 65 double mid=(lb+ub)/2; 66 if (C(mid)) lb=mid; 67 else ub=mid; 68 } 69 return ub; 70 }
最大化最小值
Aggressive cows(POJ 2456)
Time Limit: 1000MS | Memory Limit: 65536K |
Total Submissions: 20518 | Accepted: 9737 |
Description
Input
Output
Sample Input
5 3 1 2 8 4 9
Sample Output
3
Hint
1 #include <cstdio> 2 #include <cctype> 3 #include <algorithm> 4 #include <cmath> 5 #define num s-‘0‘ 6 7 using namespace std; 8 9 const int MAX_N=101000; 10 const int INF=0x3f3f3f3f; 11 int N,M; 12 int x[MAX_N]; 13 14 void read(int &x){ 15 char s; 16 x=0; 17 bool flag=0; 18 while(!isdigit(s=getchar())) 19 (s==‘-‘)&&(flag=true); 20 for(x=num;isdigit(s=getchar());x=x*10+num); 21 (flag)&&(x=-x); 22 } 23 24 void write(int x) 25 { 26 if(x<0) 27 { 28 putchar(‘-‘); 29 x=-x; 30 } 31 if(x>9) 32 write(x/10); 33 putchar(x%10+‘0‘); 34 } 35 36 bool C(int); 37 38 int main() 39 { 40 read(N);read(M); 41 for (int i=0; i<N; i++) read(x[i]); 42 sort(x, x+N); 43 int lb=0, ub=INF; 44 while (ub-lb>1) 45 { 46 int mid=(lb+ub)/2; 47 if (C(mid)) lb=mid; 48 else ub=mid; 49 } 50 write(lb); 51 putchar(‘\n‘); 52 } 53 54 bool C(int d) 55 { 56 int last=0; 57 for (int i=1; i<M; i++) 58 { 59 int crt=last+1; 60 while (crt<N && x[crt]-x[last]<d) crt++; 61 if (crt==N) return false; 62 last=crt; 63 } 64 return true; 65 }
最大化平均值
1 #include <cstdio> 2 #include <cctype> 3 #include <algorithm> 4 #include <cmath> 5 #define num s-‘0‘ 6 7 using namespace std; 8 9 const int MAX_N=101000; 10 const int INF=0x3f3f3f3f; 11 int n,k; 12 int v[MAX_N],w[MAX_N]; 13 double y[MAX_N]; 14 15 void read(int &x){ 16 char s; 17 x=0; 18 bool flag=0; 19 while(!isdigit(s=getchar())) 20 (s==‘-‘)&&(flag=true); 21 for(x=num;isdigit(s=getchar());x=x*10+num); 22 (flag)&&(x=-x); 23 } 24 25 void write(int x) 26 { 27 if(x<0) 28 { 29 putchar(‘-‘); 30 x=-x; 31 } 32 if(x>9) 33 write(x/10); 34 putchar(x%10+‘0‘); 35 } 36 37 bool C(double); 38 39 int main() 40 { 41 read(n);read(k); 42 for (int i=0; i<n; i++) 43 { 44 read(w[i]); 45 read(v[i]); 46 } 47 double lb=0, ub=INF; 48 for (int i=0; i<100; i++) 49 { 50 double mid=(lb+ub)/2; 51 if (C(mid)) lb=mid; 52 else ub=mid; 53 } 54 printf("%.2f\n",ub); 55 } 56 57 bool C(double x) 58 { 59 for (int i=0; i<n; i++) 60 { 61 y[i]=v[i]-x*w[i]; 62 } 63 sort(y,y+n); 64 double sum=0; 65 for (int i=0; i<k; i++) 66 { 67 sum+=y[n-1-i]; 68 } 69 return sum>=0; 70 }
标签:dig 排列 单位 平均值 net accept 实现 from cow
原文地址:https://www.cnblogs.com/Ymir-TaoMee/p/9492747.html