Codeforces Round #181 (Div. 2) 部分解題報告

思想總結:

上次rating 略微漲了一點,這次心急。連a題的樣例都沒仔細查對,就wa了一次。並且老毛病,改代碼不細想。

b題,並查集也搞了好久,不熟練。然後處理集合一直處於想清楚,沒想清楚的混沌狀態。然後一個條件判斷錯了。早上起來想哪裏沒處理清楚。原來是負數也能被一個數整除。沒細想。

c題,據說是數位dp。不會,去年暑假集訓有一個打標題是dp。但打表ac後,就沒管了。。。

要學習的東西很多啊。。。

 

http://www.codeforces.com/contest/300

 

a題水題;

#include<cstdlib>
#include<algorithm>
#include<cstdio>
//#inclu 
#include<iostream>
using namespace std;

int a[5566];

int main()
{ 
  int n;
  while(cin>>n)
  {
               for(int j=0;j<n ;j++)
               cin>>a[j];
               
               
               sort(a,a+n);
               
               if(a[0]<0&& a[n-1]>0){
                       cout<<"1 " <<a[0]<<endl;
                       cout<<"1 "<<a[n-1]<<endl;
                       
                       cout<<n-2 ;
                       
                      
                       for(int j=1;j<n-1;j++)
                       cout<<" "<<a[j];
                       
                       cout<<endl;
               }
               else   {
                      cout<<"1 "<< a[0]<<endl;
                      cout<<"2 " <<a[1]<<" "<<a[2]<<endl;
                      
                      cout<<n-3;
                      for(int j=3;j<n;j++)
                      cout<<" "<<a[j];
                      cout<<endl;
                      }
                      }
  return 0;
}


 

 

 

b題,就是一個並查集再加點想法,如果一個集合是3個元素的話,直接輸出這個集合中的元素,如果這個集合只有2個元素的話, 就要找一個只有一個元素的集合,一起輸出。

 

 

#include<cstdlib>
#include<algorithm>
#include<cstdio>
//#inclu 
#include<iostream>
using namespace std;


int f[1000];

int find(int x)
{
	int t=x;
	while(f[t] > 0)
		t=f[t];
	//f[x] = (f[x] < 0 ? f[x] : find(f[x]));
	return t;
}

void merge(int x,int y)
{
	if(find(x) == find(y) ) return ;
	int t=f[ find(x) ] +  f [find(y) ];

	f[find(x)] = y;
	f[find(y)] = t;
}
int main()
{
	int n,m,t;
	int j,i,k,l;
	int x,y;
	int one,two,three,four;
	while(cin>>n>>m){

		for(j=1;j<=n;j++)  f[j]=-1;
		four=one=two=three=0;
		
		for(j=0;j<m;j++){
			cin>>x>>y;
 			merge(x,y);
		}

		for(j=1;j<=n;j++)
			if(f[ find(j)] == -1)
				one++;
			else if(f[find(j)] == -2)
				two++;
			else if(f[find(j)] == -3)
				three++;
			else 
				four++;
            
			two/=2;
			three/=3;
			
			if(four>0)
            {
                      cout<<"-1"<<endl;
                      continue;
                      } 
	/*		if(one != 0   &&(one -two) < 0 )
				{
					cout<<"-1"<<endl;
					//cout<< "-1"<<one<<"  "<<two<<endl;                  
					continue;
				}
			if(one == 0 && two != 0)
				
				{
					cout<<"-1"<<endl;
					//cout<<"-1"<<"llkjkljljlk"<<endl;
					continue;
				}
				
		*/		
		
		if(two != 0 && one - two <0){
               cout<<"-1"<<endl;
               continue;
               }
			else 
			{
				j=1;
				k=2;
				l=3;
				while(three--)
				{
					for(;j<n+1;j++)
						for(k=j+1;k<n+1;k++)
							for(l=k+1;l<n+1;l++)
								if(j !=k && j!=l && k!=l && find(j)==find(k)&&find(j)== find(l)  && f[find(j)] == -3){
									cout<<j<<" "<<k<<" "<<l << endl;
									f[find(j)] = -432;
                                  
									goto out1;
								}
								out1:;
				}

				t=1;
				while(f[find(t)]!=-1 && t <=n)
					t++;
              
              j=1;
              k=2;
				while(two--){

					for(;j<n+1;j++)
						for(k=j+1;k<n+1;k++)
							if(j!=k && find(j)== find(k)  && f[find(j)]== -2){
                                    f[find(j)]= -3423;
								cout<<j<<" "<<k<<" " <<t<< endl;
                             
                             
								goto out2;
                            }
					out2:
						t++;
					while(f[find(t)]!=-1 && t<=n)t++;
					one--;

				}
				j=n;
				while(one>0){
					while(f[find(j)] != -1 )
						j--;
					cout<<j;
					j--;

					while(f[find(j)] != -1 ) j--;
					cout<<" "<<j;
					j--;

				while(f[find(j)] != -1) j--;
					cout<<" " << j<<endl;
					j--;

					one--;
					one--;
					one--;    
				}


			}




	}
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章