NOIP2014 提高組類比試題
第一試試題
題目概況:
中文題目名稱
合理種植
排隊
科技節
來源程式檔案名稱
plant.pas/.c/.cpp
lineup.pas/.c/.cpp
scifest.pas/.c/.cpp
輸入檔案名稱
plant.in
lineup.in
scifest.in
輸出檔案名
plant.out
lineup.out
scifest.out
每個測試點時限
1s
1s
1s
測試點數目
10
10
10
每個測試點分值
10
10
10
記憶體上限
128MB
128MB
128MB
1. 合理種植
(plant.pas/.c/.cpp)
【問題描述】
大COS在氯銫石料場幹了半年,受盡了勞苦,終於決定辭職。他來到表弟小cos的寒樹中學,找到方克順校長,希望尋個活幹。
於是他如願以償接到了一個任務……
美麗寒樹中學種有許多寒樹。方克順希望校園無論從什麼角度看都是滿眼寒樹,因此他不希望有三棵甚至更多寒樹種在一條直線上。現在他把校園裡n棵寒樹的座標都給了大COS,讓他數出存在多少多樹共線情況。(若一條直線上有三棵或以上的樹,則算出現一個多樹共線情況。)
【輸入】
輸入檔案名稱為plant.in。 第1行一個正整數n,表示寒樹棵數。 接下來n行,每行兩個非負整數x、y,表示一顆寒樹的座標。沒有兩顆寒樹在同一位置。
【輸出】
輸出檔案名為plant.out。 輸出一個整數,表示存在多少多樹共線情況。
【輸入輸出範例】
plant.in
plant.out
6
0 0
1 1
2 2
3 3
0 1
1 0
1
【資料範圍】
對於30%的資料,有n≤10; 對於50%的資料,有n≤100; 對於100%的資料,有n≤1,000,0≤x,y≤10,000。
2. 排隊
(lineup.pas/.c/.cpp)
【問題描述】
小sin所在的班有n名同學,正準備排成一列縱隊,但他們不想按身高從矮到高排,那樣太單調,太沒個性。他們希望恰好有k對同學是高的在前,矮的在後,其餘都是矮的在前,高的在後。如當n=5,k=3時,假設5人從矮到高分別標為1、2、3、4、5,則(1,5,2,3,4)、(2,3,1,5,4)、(3,1,4,2,5)都是可行的排法。小sin想知道總共有多少種可行排法。
【輸入】
輸入檔案名稱為lineup.in。 一行兩個整數n和k,意義見問題描述。
【輸出】
輸出檔案名為lineup.out。 輸出一個整數,表示可行排法數。由於結果可能很大,請輸出排法數mod 1799999的值。
【輸入輸出範例】
lineup.in
lineup.out
5 3
15
【資料範圍】
對於20%的資料,有n≤10,k≤40; 對於60%的資料,有n≤100,k≤500; 對於100%的資料,有n≤100,k≤n*(n-1)/2。
3. 科技節
(scifest.pas/.c/.cpp)
【問題描述】
一年一度的科技節即將到來。同學們報名各項活動的名單交到了方克順校長那,結果校長一看皺了眉頭:這幫學生熱情竟然如此高漲,每個人都報那麼多活動,還要不要認真學習了。。這樣不行。……於是,校長要求減少一些活動,使每位學生只能參加一項(一名同學要參加某活動,必須已報名且該活動未被去掉)。當然,他也不希望哪位同學因此不能參加任何活動。他想知道自己的方案能否實行。
【輸入】
輸入檔案名稱為scifest.in。 輸入資料包括多組。 對於每組資料: 第一行兩個正整數n和m,分別表示活動數和學生數。 接下來n行,每行m個為0或1的數。第i+1行第j列的數若為1,表示j同學報名參加活動i,否則表示j同學沒有報名參加活動i。
【輸出】
輸出檔案名為scifest.out。
對於每組資料輸出一行,若校長方案可行則輸出“Yes”,否則輸出“No”。(均不包括引號)
【輸入輸出範例】
scifest.in
scifest.out
3 3
0 1 0
0 0 1
1 0 0
4 4
0 0 0 1
1 0 0 0
1 1 0 1
0 1 0 0
Yes
No
【資料範圍】
對於20%的資料,n≤10,m≤200,資料群組數≤10; 對於60%的資料,n≤16,m≤300,資料群組數≤100; 對於100%的資料,n≤16,m≤300,資料群組數≤1,000。
第一題
最初的O(2n2logn) 但是因為STL 的 map 卡常, T了一半
TLE代碼:
#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<vector>#include<queue>#include<stack>#include<map>#include<set>#include<string>#include<iomanip>#include<ctime>#include<climits>#include<cctype>#include<algorithm>#ifdef WIN32#define AUTO "%I64d"#else#define AUTO "%lld"#endifusing namespace std;const int maxn=1005;const double eps=1e-7;inline int dcmp(double x){ if(fabs(x) <= eps) return 0; return x>0.0? 1 : -1;}struct Line{ double k,b; Line (const double _k,const double _b) { k=_k;b=_b; } bool operator == (const Line l) const { return !dcmp(k - l.k) && !dcmp(b - l.b); } bool operator < (const Line l) const { if(dcmp(k - l.k)==0) return b < l.b; return k < l.k; }};struct Point{ int x,y;}point[maxn];#define x(i) point[i].x#define y(i) point[i].ymap <Line,int> g;map <Line,int> ::iterator it;int non[maxn*10];int main(){ freopen("plant.in","r",stdin); freopen("plant.out","w",stdout); int n; scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d%d",&point[i].x,&point[i].y); for(int i=1;i<n;i++) for(int j=i+1;j<=n;j++) { if(x(i) == x(j)) { non[x(i)]++; continue; } double k=(double) (y(i)-y(j)) / (x(i)-x(j)); double b=(double) y(i) - k * x(i); if(!g.count(Line(k,b))) g[Line(k,b)]=1; else g[Line(k,b)]++; } int ans=0; for(it=g.begin();it!=g.end();it++) { if((*it).second>=2) ans++; } for(int i=0;i<=10000;i++) if(non[i]>=2) ans++; printf("%d",ans); return 0;}
某神犇想出來的方法, 枚舉所有三元組, 用前面去改變後面的判斷
AC代碼:
#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<vector>#include<queue>#include<stack>#include<map>#include<set>#include<string>#include<iomanip>#include<ctime>#include<climits>#include<cctype>#include<algorithm>#ifdef WIN32#define AUTO "%I64d"#else#define AUTO "%lld"#endifusing namespace std;const int maxn=1005;struct Point{ int x,y;}point[maxn];#define x(i) point[i].x#define y(i) point[i].yint n;typedef long long LL;const LL base=(1ll<<30); // must bigger than the 1e9 'coz the minimum of rake is 1/(9999*10000)LL rake[maxn][maxn];bool a[maxn][maxn];int main(){ freopen("plant.in","r",stdin) ; freopen("plant.out","w",stdout); scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d%d",&x(i),&y(i)); for(int i=1;i^n;i++) for(int j=i+1;j<=n;j++) rake[i][j] = x(i)==x(j)? 1ll : base * (y(i)-y(j)) / (x(i)-x(j)); int ans=0; for(int i=1;i^(n-1);i++) // not strictly n^3 for(int j=i+1;j^n;j++) for(int k=j+1;k<=n;k++) if(rake[i][j] == rake[j][k]) if(a[i][j] || a[j][k] || a[i][k]) a[i][j] = a[j][k] = a[i][k] = true; else ans++, a[i][j] = a[j][k] = a[i][k] = true; printf("%d",ans); return 0;}
再給個標程, 每次給個標記, 同一個斜率如果x座標在左邊則說明之前算過, 藉此可以排除掉斜率相同算過的情況
標程:
#include <fstream>#include <cstdlib>#include <cstring>#include <algorithm>using namespace std;ifstream fin("plant.in");ofstream fout("plant.out");long n;struct xxx{long x,y;} a[1005];struct kk{long dx,dy;bool c;} k[1005];bool operator <(kk k1,kk k2){ if (k1.dx==10001) return k2.dx<10001; if (k2.dx==10001) return 0; return k2.dx*k1.dy<k1.dx*k2.dy;}bool operator ==(kk k1,kk k2){ if (k1.dx==10001||k2.dx==10001) return k1.dx==k2.dx; return k2.dx*k1.dy==k1.dx*k2.dy;}void init(){ long i; fin>>n; for (i=0;i<n;i++) fin>>a[i].x>>a[i].y;}void tr(){ long i,j,s,ans=0; bool f; for (i=0;i<n;i++){ for (j=0;j<n;j++) if (i!=j) if (a[i].x==a[j].x){ k[j].dx=10001; k[j].c=a[i].y<a[j].y; }else{ k[j].dx=a[j].x-a[i].x; k[j].dy=a[j].y-a[i].y; if (k[j].dx<0){ k[j].dx=-k[j].dx; k[j].dy=-k[j].dy; } k[j].c=a[i].x<a[j].x; } for (j=i;j<n-1;j++) k[j]=k[j+1]; sort(k,k+n-1); f=1; s=0; for (j=0;j<n-2;j++){ s++; f=f&&k[j].c; if (!(k[j]==k[j+1])){ if (f&&s>1) ans++; f=1; s=0; } } if (f&&k[n-2].c&&s>0) ans++; } fout<<ans;}int main(){ init(); tr(); fin.close(); fout.close(); return 0;}
第二題
動態規劃但是可以用首碼和最佳化或者變換一下遞推公式, 把相同部分替換掉
原來的 1AC 代碼:
#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<vector>#include<queue>#include<stack>#include<map>#include<set>#include<string>#include<iomanip>#include<ctime>#include<climits>#include<cctype>#include<algorithm>#ifdef WIN32#define AUTO "%I64d"#else#define AUTO "%lld"#endifusing namespace std;const int maxn=105;const int mod=1799999;int n,k;int f[maxn][maxn*maxn];int main(){ freopen("lineup.in","r",stdin); freopen("lineup.out","w",stdout); scanf("%d%d",&n,&k); memset(f,-1,sizeof(f)); for(int i=1;i<=n;i++) f[i][0]=1; for(int i=1;i<=n;i++) for(int j=1;j<=min(k,i*(i-1)/2);j++) { bool flag=true; f[i][j]=0; for(int p=0;p<=i-1;p++) { if(!~f[i-1][j-p]) continue; flag=false; f[i][j] += f[i-1][j-p]; if(f[i][j]>=mod) f[i][j]%=mod; } if(flag) f[i][j]=-1; } printf("%d",f[n][k]); return 0;}
第三題
位元運算 + 可行性剪枝(先考慮人數多的,容易更快發現衝突)
但是使用位向量產生法的話要一塊一塊地跳, 所以dfs更優
TLE代碼:
#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<vector>#include<queue>#include<stack>#include<map>#include<set>#include<string>#include<iomanip>#include<ctime>#include<climits>#include<cctype>#include<algorithm>#ifdef WIN32#define AUTO "%I64d"#else#define AUTO "%lld"#endifusing namespace std;const int maxn=20;const int maxm=305;int n,m;struct Activity{ int s[11]; // max of 300 evry int has 32 bits int cnt; bool operator < (const Activity t) const { return cnt >= t.cnt; }}act[maxn];inline bool init(){ if(!~scanf("%d%d",&n,&m)) return false; memset(act,0,sizeof(act)); for(int i=1;i<=n;i++) { for(int j=0;j^m;j++) { int tmp; scanf("%d",&tmp); if(tmp) act[i].s[j>>5] |= (1 << (j - (j>>5<<5)) ),act[i].cnt++; } } sort(act+1,act+n+1); return true;}int now[11];inline bool check(){ for(int j=0; j^(m-1>>5); j++) if(now[j]^(0xffffffff)) return false; if(!(m-1>>5)) return now[0] ^ ((1<<m)-1)? false : true; else return now[m-1>>5] ^ ((1<<m-1-(m-1>>5))-1)? false : true;}bool bit(){ for(int status=1; status<=(1<<n)-1; ) { memset(now,0,sizeof(now)); bool flag=false; int pos=0; for(int i=1;i<=n;i++) { if(!(status & (1<<i-1))) continue; // don't forget to check here!! for(int j=0;j<=(m-1>>5);j++) if((now[j] & act[i].s[j]) ^ 0) { flag=true;pos=i-1;break; } else now[j] |= act[i].s[j]; if(flag) break; } if(flag) // step by blocks!! { status %= (1<<pos); status += (1<<pos-1); } else status++; if(!check()) continue; else return true; } return false;}int main(){ freopen("scifest.in","r",stdin); freopen("scifest.out","w",stdout); while(init()) if(bit()) printf("Yes\n"); else printf("No\n"); return 0;}
AC代碼:
#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<vector>#include<queue>#include<stack>#include<map>#include<set>#include<string>#include<iomanip>#include<ctime>#include<climits>#include<cctype>#include<algorithm>#ifdef WIN32#define AUTO "%I64d"#else#define AUTO "%lld"#endifusing namespace std;const int maxn=20;const int maxm=305;int n,m;struct Activity{ int s[11]; // max of 300 evry int has 32 bits int cnt; bool operator < (const Activity t) const { return cnt >= t.cnt; }}act[maxn];bool dfs(