1. 程式人生 > >ACM-ICPC 2018 徐州賽區網路預賽 Feature Track

ACM-ICPC 2018 徐州賽區網路預賽 Feature Track

Morgana is learning computer vision, and he likes cats, too. One day he wants to find the cat movement from a cat video. To do this, he extracts cat features in each frame. A cat feature is a two-dimension vector <xx, yy>. If x_ixi​ = x_jxj​ and y_iyi​ = y_jyj​, then <x_ixi​, y_iyi​> <x_jxj​, y_jyj​> are same features.

So if cat features are moving, we can think the cat is moving. If feature <aa, bb> is appeared in continuous frames, it will form features movement. For example, feature <aa , bb > is appeared in frame 2,3,4,7,82,3,4,7,8, then it forms two features movement 2-3-42−3−4 and 7-87−8 .

Now given the features in each frames, the number of features may be different, Morgana wants to find the longest features movement.

Input

First line contains one integer T(1 \le T \le 10)T(1≤T≤10) , giving the test cases.

Then the first line of each cases contains one integer nn (number of frames),

In The next nn lines, each line contains one integer k_iki​ ( the number of features) and 2k_i2ki​ intergers describe k_iki​ features in ith frame.(The first two integers describe the first feature, the 33rd and 44th integer describe the second feature, and so on).

In each test case the sum number of features NN will satisfy N \le 100000N≤100000 .

Output

For each cases, output one line with one integers represents the longest length of features movement.

樣例輸入複製

1
8
2 1 1 2 2
2 1 1 1 4
2 1 1 2 2
2 2 2 1 4
0
0
1 1 1
1 1 1

樣例輸出複製

3

題目來源

ACM-ICPC 2018 徐州賽區網路預賽

 先說題意,首先是t組資料,每組資料第一行是一個n,接下來有n行,每行最開始有一個數x代表這一行有x對數,接下來有x

對數(兩個數字為一對)。

題目問在這n

行之中,連續出現次數最大的那一對數出現的次數。

比如第一個樣例(1,1)這一對連續出現了3次。

我們首先把這個問題從一個數對轉化成一個數字,那麼問題會轉化成這麼一個問題:給你一串數字,求這串數字中連續出現的數字次數的最大值。

那麼我們很容易能想一種dp的解法,令dp[i][j]表示前i個數字中j這個數字出現的最大次數,然後dp[i][j]=dp[i-1][j]+1,然後取一個最大值即可。

那麼對於這個問題,只不過是把數字轉化成了數對,我們可以利用map來處理一下這個數對,map<pair<int,int>,int>mp,這樣就可以把一個數對對映成為一個數字,接下來因為資料範圍較大,有105

,我們用到的狀態只有當前狀態和前一個狀態,所以可以利用滾動陣列優化一下。

#include "bits/stdc++.h"
using namespace std;
#include <map>
#define rep(i,j,k) for(int i=j;i<=k;i++)
//特別疑惑一點,如果把using namespace std放到下面就會報,沒有pair這個型別的錯誤,求教?
typedef pair<int,int> pir;//像這個型別裡是包括了名稱空間的,所有不能放到前面
map<pir,int> mp[2];



int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n,max1=0;
		int cur=0;
		cin>>n;
		mp[0].clear();
		mp[1].clear();
		rep(i,1,n)
		{
			int m;
			cin>>m;
			cur^=1;//滾動陣列,關鍵在於使用map這個資料結構
			mp[cur].clear();
			rep(j,1,m)
			{
				int a,b;
				scanf("%d%d",&a,&b);
				pir tmp=make_pair(a,b);
				mp[cur][tmp]=mp[cur^1][tmp]+1;
				if(max1<=mp[cur][tmp])
				{
					max1=	mp[cur][tmp];
			}
			}
		}
		cout<<max1<<endl;
	}
	return 0;
}

上面名稱空間問題已解決

使用名稱空間的目的是對識別符號的名稱進行本地化,以避免命名衝突。

在C++中,變數、函式和類都是大量存在的。如果沒有名稱空間,這些變數、函式、類的名稱將都存在於全域性名稱空間中,會導致很多衝突。比如,如果我們在自己的程式中定義了一個函式toupper(),這將重寫標準庫中的toupper()函 數,這是因為這兩個函式都是位於全域性名稱空間中的。命名衝突還會發生在一個程式中使用兩個或者更多的第三方庫的情況中。此時,很有可能,其中一個庫中的名 稱和另外一個庫中的名稱是相同的,這樣就衝突了。這種情況會經常發生在類的名稱上。比如,我們在自己的程式中定義了一個Stack類,而我們程式中使用的某個庫中也可能定義了一個同名的類,此時名稱就衝突了。

Namespace 關鍵字的出現就是針對這種問題的。由於這種機制對於聲明於其中的名稱都進行了本地化,就使得相同的名稱可以在不同的上下文中使用,而不會引起名稱的衝突。或許名稱空間最大的受益者就是C++中的標準庫了。在命名空間出現之前,整個C++庫都是定義在全域性名稱空間中的(這當然也是唯一的名稱空間)。引入名稱空間後,C++庫就被定義到自己的名稱空間中了,稱之為std。這樣就減少了名稱衝突的可能性。我們也可以在自己的程式中建立自己的名稱空間,這樣可以對我們認為可能導致衝突的名稱進行本地化。這點在我們建立類或者是函式庫的時候是特別重要的。

C++標準程式庫中的所有識別符號都被定義於一個名為std的namespace中。 
namespace是指識別符號的各種可見範圍。名稱空間用關鍵字namespace 來定義。名稱空間是C++的一種機制,用來把單個識別符號下的大量有邏輯聯絡的程式實體組合到一起。此識別符號作為此組群的名字。

C++標準程式庫中的所有識別符號都被定義於一個名為std的namespace中。 由於namespace的概念,使用C++標準程式庫的任何識別符號時, 

 

#include "bits/stdc++.h"
#include "vector"
#include "map"
#include "algorithm"
#include "string"
#define rep(i,j,k) for(int i=j;i<=k;i++)
using namespace std;
//從小到大排序 
void quickSort(int left,int right ,int *a)
{
	if(left>right)
	return;
	int i=left,j=right;
	int cur=a[left];
	while(i!=j)
	{
		while(a[j]>=cur&&i<j)
		j--;
		while(a[i]<=cur&&i<j)
		i++;
		if(i<j)
		swap(a[i],a[j]);
	}
	a[left]=a[i];//最後i和j相遇肯定有且僅有一種情況 
	a[i]=cur;
	quickSort(left,i-1,a);
	quickSort(i+1,right,a);
}

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		int a[100];
		cin>>n;
		rep(i,0,n-1)
		{
			scanf("%d",a+i);
		}
		quickSort(0,n-1,a);
		rep(i,0,n-1)
		{
			cout<<a[i]<<" ";
		}
		
	}
	return 0;
}

https://github.com/zhblue/hustoj,網上oj 開原始碼

#include "stdio.h"
const int maxn = 10000;
int a[maxn];
int  main()
{
	int n;
	scanf("%d",&n);
	a[0]=1;
	for(int i=2;i<=n;i++)
	{
		int cur=0;
		for(int j=0;j<=maxn-1;j++)
		{
			cur+=a[j]*i;
			a[j]=cur%10;
			cur/=10;
		}
	}
	int i;
	for(i=maxn-1;i>=0;i--)
	{
		if(a[i]!=0)
		{
			break;
		}
	}
	for(int j=i;j>=0;j--)
	{
		printf("%d",a[j]);
	}
	return 0;
}

 大數階乘