閒着無聊作了點hust上 acm的訓練題ios
A題 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=104738#problem/A數組
看了20分鐘英文才看懂他在瞎比比個啥spa
基本都是廢話。。code
大體意思就是blog
第二行是咱們的主人公掛衣服的地方。。 3-n+1行 爲 其餘人掛衣服的地方。。ci
找出沒有重疊的地方。。(看了半天英文就給我作這個??)get
數據蒟蒻只有100,也就是隻要看懂題就能作的模擬題。。it
ps:把數據改大才有點意思嘛io
15msclass
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 using namespace std; 5 int n,ll,rr,l,r,ans; 6 bool a[105]; 7 int main() { 8 cin>>n; 9 cin>>l>>r; 10 for (int i=l; i<r; i++) a[i]=true; 11 for (int i=1; i<n; i++) 12 { 13 cin>>ll>>rr; 14 for (int j=ll; j<rr; j++) a[j]=false; 15 } 16 for (int i=l; i<r; i++) if (a[i]) ans++; 17 cout<<ans; 18 return 0; 19 }
B題 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=104738#problem/B
一樣看了20分鐘的英文。。而後悲劇的我看錯題目了。。
覺得 n由 l 和 r 兩個數湊出來。。最後發現是l-r之間的數。。
仍是怎麼作都WA,找不出
最後發現long long 問題。。坑死爹的long long 浪費了好長好長的時間
15ms
祭奠我死去的兩個小時。。。
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 using namespace std; 5 long long t,n,l,r; 6 int main(){ 7 cin >> t ; 8 for ( int o = 0 ; o < t ; o ++ ) 9 { 10 cin >> n >> l >> r ; 11 long long k=n/l*r; 12 if ( k>=n) cout<<"Yes"<<endl; 13 else cout<<"No"<<endl; 14 } 15 return 0; 16 }
C和D題。。看A的人不多就跳過了。。
先作的E http://acm.hust.edu.cn/vjudge/contest/view.action?cid=104738#problem/E
比較短。。比較好理解。。讓咱們找出n個數的最大公因數x。。而後輸出 n*x 就能夠了
(找公因數的方法。。。呃。。)31ms
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 using namespace std; 5 int n,l=500; 6 int a[120]; 7 int main(){ 8 cin>>n; 9 for (int i=1; i<=n; i++) 10 { 11 cin>>a[i]; 12 if (a[i]<l) l=a[i]; 13 } 14 for (int i=l; i>=1; i--) 15 { 16 bool y=true; 17 for (int j=1; j<=n; j++) 18 if (a[j]% i !=0 ) 19 { 20 y=false; break; 21 } 22 if (y) { cout<<n*i; break;} 23 } 24 return 0; 25 }
F題 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=104738#problem/F
裝十字架而已。。找到最上面的# 而後模擬判斷 整個十字區域是否爲#就能夠了。。
又是一題英語閱讀理解。
30ms
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 using namespace std; 5 int n; 6 char c; 7 bool a[120][120]; 8 int main(){ 9 cin>>n; 10 for (int i=1; i<=n; i++) 11 for (int j=1; j<=n; j++) 12 { 13 cin>>c; 14 if (c=='#') a[i][j]=true; 15 } 16 17 for (int i=1; i<=n; i++) 18 for (int j=1; j<=n; j++) 19 if (a[i][j]) 20 { 21 if (j==1 || !a[i+1][j-1] || !a[i+1][j] || !a[i+1][j+1] || !a[i+2][j] ) 22 { 23 cout<<"NO"<<endl; 24 return 0; 25 } 26 a[i+1][j-1]=false; 27 a[i+1][j]=false; 28 a[i+1][j+1]=false; 29 a[i+2][j]=false; 30 } 31 cout<<"YES"<<endl; 32 return 0; 33 }
G題 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=104738#problem/G
這題閱讀理解的難度有點高,表示沒搞清楚意思就這麼WA了。。
意思就是 方塊上疊方塊。。
方塊有個長度 length ,這個方塊上面最多放[length](!!!)個方塊 而且只能放置長度不超過length的方塊
看懂就很好作了。。。考英語的。。
15ms
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 using namespace std; 5 int n,ans,t; 6 bool ok; 7 int a[105]; 8 int main(){ 9 cin>>n; 10 for (int i=0; i<n; i++) cin>>a[i]; 11 sort(a,a+n); 12 while (t<n) 13 { 14 int s=0; 15 for (int i=0; i<n; i++) 16 if (a[i]>=s)s++,a[i]=-1; 17 ans++; 18 t+=s; 19 } 20 cout<<ans<<endl; 21 return 0; 22 }
I題 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=104738#problem/I
這題是我最後時刻作完的。。。。英文題就是神祕get看不懂題目的buff
受到了來自犇犇的嘲諷
作法爲貪心。。把 n 組裏面的
每組牌 分左邊一半和 右邊一半 分給不一樣的兩我的
若是這組牌爲奇數張,中間多餘的一張留下來。
按從大到小順序給左邊給右邊給左邊給右邊…………
(爲何這麼貪心呢) 由於題目要求每一個人都要盡力而爲。。
那麼任何一我的都不可能讓對手拿到靠近本身這邊的牌(若是拿對手那邊爲最優,那麼敵人也會選擇最優去阻止本身這邊的牌被順走)。。
因此左邊一半和右邊一半一開始就能夠分給彼此。。
中間剩餘的那一張呢。。他們都會盡力拿最大的。。
1 #include<acstdio> 2 #include<iostream> 3 #include<algorithm> 4 using namespace std; 5 int n,ansl,ansr,t; 6 bool ok; 7 int a[105]; 8 bool cmp(const int x,const int y) 9 { 10 return x>y; 11 } 12 int main(){ 13 cin>>n; 14 for (int i=1; i<=n; i++) 15 { 16 int x,y; 17 cin>>x; 18 for (int j=1; j<=x/2; j++) 19 { 20 cin>>y; 21 ansl+=y; 22 } 23 if (x%2==1) cin>>a[++t]; 24 for (int j=1; j<=x/2; j++) 25 { 26 cin>>y; 27 ansr+=y; 28 } 29 } 30 sort(a+1,a+1+t,cmp); 31 for (int i=1; i<=t; i++) 32 if (i%2==1) ansl+=a[i]; 33 else ansr+=a[i]; 34 cout<<ansl<<" "<<ansr<<endl; 35 return 0; 36 }
而後作完就沒有時間叻。。。!!我作得真太慢了。。。二級英語才60+跪爛
閒着研究了一下D題 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=104738#problem/D
尋找質素數。。令q(i)爲不超過 i 的最大質素數,p(i) 爲超過 i 的 最小質素數。
而後令 i=2-n 累加 1/q(i)p(i)...
這題能夠發現一個規律。。就是 1/q(i)p(i)=( 1/q(i) - 1/p(i) )/ ( p(i)-q(i) );
若是 n+1 爲質素數。則 答案就是 1/2 - 1/(n+1) ..
獲得了這個規律就能夠作了。。。
而後就是判斷質素數的方法。。
呃。。由於我懶得寫就沒有寫了——偷偷調用一下泡犇犇的代碼
1 #include <cstdio> 2 #define LL long long 3 //------------------------------------------------------------ 4 int tcase; 5 int icase; 6 7 LL n,d; 8 LL ls,rs; 9 LL fm,fz; 10 //------------------------------------------------------------ 11 int su(LL d) 12 { 13 //--0 init 14 LL i; 15 //--1 16 for (i=2; i*i<=d&&d%i!=0; i++); 17 return i*i>d; 18 } 19 //------------------------------------------------------------ 20 int main( ) 21 { 22 for (scanf("%d",&tcase); ++icase<=tcase; ) 23 { 24 scanf("%I64d",&n); 25 ls=n ; for (; !su(ls); ls--); 26 rs=n+1; for (; !su(rs); rs++); 27 d=ls+rs-n; 28 d=d-1LL; 29 d=d*2LL; 30 fz=ls*rs; 31 fz=fz-d; 32 fm=ls*rs; 33 fm=fm*2LL; 34 if (fz%2 ==0) { fz/=2LL; fm/=2LL; } 35 if (fz%ls==0) { fz/=ls; fm/=ls; } 36 if (fz%rs==0) { fz/=rs; fm/=rs; } 37 printf("%I64d/%I64d\n",fz,fm); 38 } 39 }
11 int su(LL d) 12 { 13 //--0 init 14 LL i; 15 //--1 16 for (i=2; i*i<=d&&d%i!=0; i++); 17 return i*i>d; 18 }
而後看到這段。。!!臥槽暴力搜不是浪費一大把的時間嘛。。這麼能夠這麼搞。。(跑了702ms)
因而我給稍微改了改去交了一下 而後變成93ms了。。快了好幾倍。。
大概就是
int su(LL d) { //--0 init LL i; //--1 for (i=0; a[i]*a[i]<=d && d%a[i]!=0; i++); return a[i]*a[i]>d; }
a[]數組爲質素數數組 從 2開始。。 2 3 5 7 11……………………
因爲最大的質素數是大於10^9 的。。因此咱們只須要 找到 31622 之內附近的質素數就能夠了。。到40000也只有4203個質素數。。
能夠先篩出那麼多個素數後存儲下來備用。。節省很是長的時間
蒟蒻遠處%泡犇犇