NOIP2014 Improving Group simulation test questions
First Test test
Topic Overview:
Name of Chinese title
Reasonable planting
Queuing
Science and Technology Festival
Source program file name
Plant.pas/.c/.cpp
Lineup.pas/.c/.cpp
Scifest.pas/.c/.cpp
Enter file name
Plant.in
Lineup.in
Scifest.in
Output file name
Plant.out
Lineup.out
Scifest.out
Time limit per test point
1s
1s
1s
Number of test points
10
10
10
Score per test point
10
10
10
Memory limit
128MB
128MB
128MB
1. Reasonable planting
(Plant.pas/.c/.cpp)
"Problem description"
Big cos in the chlorine cesium stone yard dry for half a year, suffered a labor, finally decided to resign. He came to cousin Little cos of the cold Tree Middle School, find Fonkshun headmaster, hope to seek a job.
So he received a task ...
There are many cold trees in the middle school of beautiful cold trees. Fonkshun hope that the campus from any point of view is an eyeful of cold trees, so he does not want to have three trees or even more cold trees in a straight line. Now he put the school's n tree coordinates are given to the big Cos, let him count the existence of how many trees common line situation. (if there are three or more trees in a straight line, a multi-tree collinear condition appears.) )
Input
The input file name is plant.in.
the 1th line is a positive integer n, which indicates the tree number of cold trees. The
next n rows, two nonnegative integers x, y per line, represent the coordinates of a cold tree. There are no two cold trees in the same place.
Output
The output file name is Plant.out.
outputs an integer that indicates how many tree collinear conditions exist.
"Input and Output sample"
Plant.in
Plant.out
6
0 0
1 1
2 2
3 3
0 1
1 0
1
"Data Range"
For 30% of the data, there is n≤10;
for 50% of the data, there is n≤100;
for 100% of the data, there is n≤1,000,0≤x,y≤10,000.
2. Queuing
(Lineup.pas/.c/.cpp)
"Problem description"
Little sin is in the class have n students, is ready to row into a column, but they do not want to height from the low to high row, so too monotonous, too no personality. They hope that there are just k pairs of classmates are high in front, short in the rear, the rest are short in front, high in the rear. If the n=5,k=3, if 5 people from low to high are labeled 1, 2, 3, 4, 5, then (1,5,2,3,4), (2,3,1,5,4), (3,1,4,2,5) are feasible row method. Little sin wants to know the total number of feasible methods.
Input
The input file name is lineup.in.
a row of two integers n and k, meaning the problem description.
Output
The output file name is Lineup.out.
outputs an integer representing the number of feasible rows. Because the result can be large, output the value of the number of mod 1799999.
"Input and Output sample"
Lineup.in
Lineup.out
5 3
15
"Data Range"
For 20% of the data, there is n≤10,k≤40,
for 60% of the data, there is n≤100,k≤500,
for 100% of the data, there is n≤100,k≤n* (n-1)/2.
3. Science and Technology Festival
(Scifest.pas/.c/.cpp)
"Problem description"
The annual science Festival is about to come. Students enrolled in the list of activities of the Fonkshun headmaster that, the result of the headmaster a look wrinkled brow: This help students enthusiasm unexpectedly so high, everyone reported so many activities, but also to seriously study. That doesn't work. ...... As a result, the headmaster asked for a reduction in activities so that each student could only participate in one item (a student had to attend an event and had to register and the activity was not removed). Of course, he does not want any students to participate in any activities. He wanted to know if his plan could be implemented.
Input
The input file name is scifest.in. The
input data includes multiple groups.
for each set of data:
The first row of two positive integers n and M, respectively, representing the number of active and students.
next n rows, the number of m for each row is 0 or 1. The number of column J in row i+1, if 1, indicates that J students enrolled in activity I, otherwise, J classmate did not sign up to participate in the activity I.
Output
The output file name is Scifest.out.
For each set of data output row, if the principal scheme is feasible, output "Yes", otherwise output "No". (all quotes are not included)
"Input and Output sample"
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
"Data Range"
For 20% of data, n≤10,m≤200, number of data groups ≤10;
for 60% of data, n≤16,m≤300, number of data groups ≤100;
for 100% data, n≤16,m≤300, number of data groups ≤1,000.
First question
The original O (2N2LOGN) but because the STL map card is often, T half
Tle Code:
#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" #endif using 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].y map <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; }
The method that some God Ben think out, enumerate all triples, use the front to change the judgment behind
AC Code:
#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" #endif using namespace std;
const int maxn=1005; struct point {int x, y;}
POINT[MAXN];
#define X (i) point[i].x #define Y (i) point[i].y int 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; }
Give a standard, each time a mark, the same slope if the x-coordinate on the left side of the description before the calculation, so as to eliminate the slope of the same calculation of the situation
Standard process:
#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; }
Second question
Dynamic programming But you can use prefixes and optimizations or transform recursive formulas to replace the same parts
The original 1AC code:
#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" #endif using 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; }
Third question
Bitwise operations + Feasibility pruning (first consider the number of people, easy to find conflicts faster)
But using bit-vector generation to jump a piece, so Dfs is better
Tle Code:
#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" #endif using namespace std;
const int MAXN=20;
const int maxm=305;
int n,m;
struct Activity {int s[11];//MAX of Evry int has +-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 Code:
#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"
#endif
using namespace std ;
const int maxn=20;
const int maxm=305;
int n,m;
struct Activity
{
int s[11];//MAX of Evry int has +-bits
int cnt;
BOOL operator < (const Activity t) const
{
return cnt >= t.cnt;
}
} ACT[MAXN];
BOOL Dfs (