套题链接:http://codeforces.com/contest/732
B题Cormen --- The Best Friend Of a Man贪心的考虑一下,只要相邻两位之和不小于k即可。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 const int maxn = 505; 7 int arr[maxn]; 8 int main() 9 { 10 int n,k; 11 cin>>n>>k; 12 for(int i=1;i<=n;i++) scanf("%d",&arr[i]); 13 int ans = 0; 14 int pre = 1000; 15 for(int i=1;i<=n;i++) 16 { 17 if(pre+arr[i]>=k) 18 { 19 pre = arr[i]; 20 } 21 else 22 { 23 pre = k-pre; 24 ans += pre-arr[i]; 25 arr[i] = pre; 26 } 27 } 28 printf("%d ",ans); 29 for(int i=1;i<=n;i++) 30 { 31 printf("%d",arr[i]); 32 if(i<n) printf(" "); 33 } 34 printf(" "); 35 return 0; 36 }
C题Sanatorium三餐对三餐九种情况,但最后写的时候没有那么复杂。 如果三个相等,不用加。如果两个相等并且为最大值,则ans = 最大值*3-1-最小值。否则三个全不相等,则ans = 最大值*3-2-b-d-s。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 using namespace std; 6 typedef long long ll; 7 const ll inf = 0x3f3f3f3f3f3f3f3f; 8 int main() 9 { 10 ll b,d,s; 11 cin>>b>>d>>s; 12 ll ans = inf; 13 ll maxn = max(max(b,d),s); 14 ll minn = min(min(b,d),s); 15 if(maxn==minn) 16 { 17 ans = 0; 18 } 19 else if(b==maxn&&d==maxn) 20 { 21 ans = maxn-1-minn; 22 } 23 else if(b==maxn&&s==maxn) 24 { 25 ans = maxn-1-minn; 26 } 27 else if(d==maxn&&s==maxn) 28 { 29 ans = maxn-1-minn; 30 } 31 else 32 { 33 ans = maxn*3-2-b-d-s; 34 } 35 printf("%I64d ",ans); 36 }
D题Exams 估计是很长时间没写二分了,自己想的时候没往那方面想。偷偷看了眼题解,说用二分。一想肯定是二分。 但是那个check不会写 QAQ
得一神指导 首先二分答案,然后check答案。写check时,判断[1,x]区间,首先肯定是取区间里面每个的最后值,如果没有直接false,这样是最优的情况,sort一下,从小到大综合前一个时间判断每个时间是否够复习。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <cmath> 6 #include <vector> 7 using namespace std; 8 const int maxn = 1e5+5; 9 int d[maxn]; 10 int a[maxn]; 11 vector<int> q[maxn]; 12 struct node 13 { 14 int la,sub; 15 }last[maxn]; 16 int n,m; 17 bool cmp(node A,node B) 18 { 19 return A.la<B.la; 20 } 21 bool check(int x) 22 { 23 memset(last,0,sizeof(last)); 24 for(int i=1;i<=m;i++) 25 { 26 int la = 0; 27 int tt = q[i].size(); 28 for(int j=0;j<tt;j++) 29 { 30 int cur = q[i][j]; 31 if(cur<=x) 32 { 33 la = cur; 34 } 35 } 36 if(la==0) return false; 37 last[i].la = la; 38 last[i].sub = i; 39 } 40 sort(last+1,last+m+1,cmp); 41 int time = 0; 42 for(int i=1;i<=m;i++) 43 { 44 if(time+a[last[i].sub]>=last[i].la) return false; 45 time += a[last[i].sub]+1; 46 } 47 return true; 48 } 49 int main() 50 { 51 scanf("%d %d",&n,&m); 52 for(int i=1;i<=n;i++) 53 { 54 scanf("%d",&d[i]); 55 if(d[i]!=0) 56 { 57 q[d[i]].push_back(i); 58 } 59 } 60 for(int i=1;i<=m;i++) 61 { 62 scanf("%d",&a[i]); 63 } 64 int l = 1; 65 int r = n; 66 int ans = 0; 67 while(l<=r) 68 { 69 int mid = (l+r)/2; 70 if(check(mid)) 71 { 72 ans = mid; 73 r = mid-1; 74 } 75 else l = mid+1; 76 } 77 if(ans==0) printf("-1 "); 78 else printf("%d ",ans); 79 return 0; 80 }
E题 Sockets