賽前聽說這次比賽挺水(學校自己說的- -能是真的嗎),區域賽銅牌水平,於是本蒟蒻慕名而去,最終果然被大牛血洗。。。
https://www.nowcoder.com/acm/contest/90#question
不過收穫還是有一些的,所以特意總結一下:
剛開始比賽,按順序做題,有點兒緊張,大腦一片空白覺得A題這種水題居然有點兒難做。。。A題遞歸,頭次提交TLE了,TLE表明咱代碼還是能得出正確結果的,於是改代碼n = 1跑到30輸出結果以","間隔。
開一個新代碼把輸出結果作爲const數組,直接輸出。第一個AC。。。
前一次代碼:
#include <iostream>
using namespace std;
int n;
long long ans;
void f(int x){
if(x == n){
ans++;
return;
}
for(int i = 1;x+i <= n;i++)
f(x+i);
}
int main(){
for(n = 1;n <= 30;n++){
ans = 0;
f(0);
cout << ans << ",";
}
return 0;
}
第二次代碼:#include <iostream>
using namespace std;
const long long a[] = {0,1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608,16777216,33554432,67108864,134217728,268435456,536870912};
int main(){
int t;
cin >> t;
while(t--){
int n;
cin >> n;
cout << a[n] << endl;
}
return 0;
}
刷新看AC%,發現I簽到題,ACx2
之後打開D,J,L三道題都看了看,初看覺得J動態規劃,暫時放下看L,一看L我應該會吧。。。於是按照約分的思路做……卡了1個多小時,樣例都得不出正確結果。。好吧,打 碼能力太弱了。
D的話倒是好說一下,經典的LPS算法,求最長迴文子串長度n,總長度-n得結果。。ACx3
打開F,發現是poj2917原題,只是輸出格式改了改。。ACx4
此時會看J,發現1和n-1是互補的,對n-1個數+1和對1個數-1起到的效果是相同的。。步數step解決了,最大數採用相同思想,對1 2 3這三個數,對3執行-1操作2次,對2進行-1一次。結果爲4……意思就是,改動除了最大數的數,則結果的最大數+1
舉例
①1 2 4
2-1 = 1
4-1 = 3
step = 1 + 3 = 4
max = 4 + 1 = 5
②1 3 4
3-1 = 2
4-1 = 3
step = 2 + 3 = 5
max = 4 + 2 = 6
代碼:
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int num[100010];
int main(){
int t,n;
cin >> t;
while(t--){
cin >> n;
int max,min,step = 0;
memset(num,0,sizeof(num));
for(int i = 0;i < n;i++)cin >> num[i];
sort(num,num+n);
max = num[n-1];
min = num[0];
for(int i = 0;i < n;i++)step += num[i]-min;
cout << step << " " << step+min << endl;
}
return 0;
}
ACx5
開K題,一看,應該不難,跟天梯賽裏的L1題差不多(參見 古風排版)
代碼:
#include <iostream>
#include <cstring>
using namespace std;
string l[100010];
int main(){
int k,t;
cin >> k;
while(k--){
for(int i = 0;i < 100010;i++)l[i].clear();
int n;
cin >> n;
string str;
cin >> str;
if(n == 1){
cout << str << endl;
continue;
}
for(int i = 0;str[i];i++){
t = i%(2*n-2);
if(t >= n)
l[2*n-2-t]+=str[i];
else l[t]+=str[i];
}
for(int i = 0;i < n;i++)cout << l[i];
cout << endl;
}
return 0;
}
值得一提的是這題我WA了兩次,沒有考慮特殊情況n=1,後來n=1的時候忘了輸出換行符- -耽誤了十多分鐘。。
ACx6
放不下L題,那道耽誤了一個多小時的題,這次想想從別的角度試試呢?不用約分的思路。。。我想到數學上的求對數log
比較x^a和y^b,只需比較alogx和blogy
那麼這個題就好說了。。。由於不能直接比較浮點數,引入eps。。。因爲調整eps的大小(一次1e-5,一次1e-9)又WA了兩次,不過能AC已經不錯了。。。
代碼:
#include <iostream>
#include <cmath>
using namespace std;
const double eps = 1e-3;
int main(){
int t,x,a,y,b;
double m,n;
cin >> t;
while(t--){
cin >> x >> a >> y >> b;
m = log(x);
n = log(y);
if(abs(a*m-b*n) < eps)cout << "Yes\n";
else cout << "No\n";
}
return 0;
}
ACx7
這時候剩下大概一個小時,看AC%決定開G
打開一看便有思路,無非是有點兒變化的行列互換。自信滿滿寫好代碼運行樣例答案相符,心想運氣可真好,還能做一題,可是交上去WA了。。。之後開始了漫長的半個多小時的找錯。。。找到幾處可能導致WA的地方提交上去都不對,我連WA了5次。。這時候離比賽結束只有不到十分鐘了,突然發現對負數的處理上存在問題,向右旋轉的次數x爲負的時候我直接使x+4,以爲這樣就成了正值。。。那一屋!改成了if(x<0)x = 4-x%4;之後就AC了,總算沒有辜負最後一個小時。。
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
char str[40][40];
char tmp[40][40];
void turn_right(int n,int m){
memset(tmp,0,sizeof(tmp));
memmove(tmp,str,sizeof(str));
memset(str,0,sizeof(str));
char ch;
for(int i = 0;i < n;i++)
for(int j = 0;j < m;j++){
ch = tmp[i][j];
if(ch == '-')ch = '|';
else if(ch == '|')ch = '-';
str[j][n-1-i] = ch;
}
}
int main(){
int t,n,m;
cin >> t;
while(t--){
int x = 0;
cin >> n >> m;
for(int i = 0;i < n;i++)cin >> str[i];
string cmd;
cin >> cmd;
for(int i = 0;cmd[i];i++)
if(cmd[i] == 'R')x++;
else x--;
if(x<0)x = 4-abs(x)%4;
x%=4;
for(int i = 0;i < x;i++){
turn_right(n,m);
swap(n,m);
}
cout << n << " " << m << endl;
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++)
cout << str[i][j];
cout << endl;
}
cout << endl;
}
return 0;
}
ACx8
8AC對於我這種蒟蒻來說已經不少了,雖然有些遺憾,沒有嘗試別的題,甚至看都沒能看一眼,不過進步總是一步一步地嘛,這次8題,爭取下次9題,再下次10題……希望自己能在ACM的路上走遠……