BestCoder Round #4 前两题 hdu 4931 4932
第一題太水了。。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 int a[6]; 7 int main(){ 8 int cas; 9 scanf( "%d", &cas ); 10 while( cas-- ){ 11 for( int i = 0; i < 6; i++ ){ 12 scanf( "%d", &a[i] ); 13 } 14 sort( a, a + 6 ); 15 int ans1 = a[5] + a[4]; 16 int ans2 = a[1] + a[2] + a[3]; 17 if( ans1 > ans2 ) puts( "Grandpa Shawn is the Winner!" ); 18 else puts( "What a sad story!" ); 19 } 20 return 0; 21 } View Code第二題。。呵呵,一個大坑,記得前幾次的BC,被hack到哭。。這次做的時候就感覺很怪,開始的時候就是想排序,枚舉相鄰兩個的長度,但是不知道怎么判斷符合條件,大神很快就寫出代碼,但是感覺他的貪心好像有問題,就發了一組數據給他,他發現錯誤改后又交,又過了pt。。但是始終放心不下,后來在比賽進行到一個半小時的時候就想有沒有可能出現小數的數據,最后發現了!!!大神改后我們再交了一發,剩下就把我們測試過的數據準備好來hack別人。。比賽結束后,發現room里只有我過了第二題,全部成功hack,表示不應該手軟的,有幾個人沒有hack到,怕失敗了,下次有數據一定不手軟。。。
講一下怎么判斷符合條件,a[i]從小到大1 - n,貪心,如果可以的話盡量把線段放在a[i]的左邊,用一個pre來記錄。。首先,第一個肯定可以把線段放在它的左邊,初始化pre = a[1] ,從第2個開始看 if( pre + d <= a[i] ) 說明第i個位置的線段可以放在a[i]的左邊,更新pre = a[i];如果不可以放在左邊,那么線段肯定要放在a[i]的右邊,if( a[i] + d > a[i+1] )就是說明d不合條件,return false;如果可以放在右邊,就更新pre = a[i] + d。。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 7 #define mnx 600005 8 #define inf 0x3f3f3f3f 9 10 double a[mnx]; 11 int n; 12 13 bool check( double d ) { 14 double pre = a[1]; 15 for( int i = 2; i < n; ++i ) { 16 if( a[i] == pre ) 17 continue; 18 if( pre + d <= a[i] ) 19 pre = a[i]; 20 else 21 if( a[i] + d > a[i+1] ) 22 return 0; 23 else 24 pre = a[i] + d; 25 } 26 return 1; 27 } 28 int main() { 29 int cas; 30 scanf( "%d", &cas ); 31 while( cas-- ) { 32 scanf( "%d", &n ); 33 for( int i = 1; i <= n; ++i ) 34 scanf( "%lf", &a[i] ); 35 sort( a + 1, a + n + 1 ); 36 double ans = 0; 37 for( int i = 1; i < n; ++i ) { 38 if( check( a[i+1] - a[i] ) ) 39 ans = max( ans, a[i+1] - a[i] ); 40 if( check( ( a[i+1] - a[i] ) / 2 ) ) 41 ans = max( ans, ( a[i+1] - a[i] ) / 2 ); 42 } 43 printf( "%.3lf\n", ans * 1.0 ); 44 } 45 return 0; 46 } 47 //這個是我hack的數據,如果不ac可以試一下這些數據 48 /* 49 12 50 5 51 -4 1 8 15 21 52 3 53 -1000000000 0 1000000000 54 5 55 -9 -8 1 10 12 56 5 57 -9 0 1 10 12 58 5 59 1 10 -8 12 -9 60 4 61 -5 0 100 111 62 5 63 -9 0 11 18 21 64 5 65 -9 0 11 12 21 66 4 67 -1 1 10 11 68 6 69 -1 1 10 16 23 25 70 4 71 1 3 8 10 72 6 73 2 3 8 9 16 17 74 answer 75 7.000 76 1000000000.000 77 9.000 78 9.000 79 9.000 80 100.000 81 7.000 82 9.000 83 9.000 84 6.000 85 5.000 86 2.500 87 */ View Code?
轉載于:https://www.cnblogs.com/LJ-blog/p/3905595.html
總結
以上是生活随笔為你收集整理的BestCoder Round #4 前两题 hdu 4931 4932的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: TCP/IP之TCP连接的建立与中止状态
- 下一篇: Cocos学习笔记