| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
Re:看discuss大家都是2次sort,为啥我就TLE呢?In Reply To:看discuss大家都是2次sort,为啥我就TLE呢? Posted by:temp_ptr at 2011-09-02 00:02:50 我把代码贴出来,已经2天了,还是TLE:
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
#define FSTREAM
int case_num = 0;
int row, collumn, stone_num;//[1, 131072]
struct MyPoint
{
int x,y;
};
MyPoint stone_pos[140000];
bool MyCompXY(MyPoint a, MyPoint b)
{
if(a.x < b.x)
return true;
else if(a.x > b.x)
return false;
else if(a.y < b.y)
return true;
else
return false;
}
bool MyCompYX(MyPoint a, MyPoint b)
{
if(a.y < b.y)
return true;
else if(a.y > b.y)
return false;
else if(a.x < b.x)
return true;
else
return false;
}
int main()
{
int num_of_situation, pre_x, pre_y, diff_row_num, diff_coll_num;
#ifdef FSTREAM
fstream fin("1974.txt");
fin>>case_num;
while(case_num--)
{
fin>>row>>collumn>>stone_num;
for(int i = 0; i < stone_num; i++)
fin>>stone_pos[i].x>>stone_pos[i].y;
#else
cin>>case_num;
while(case_num--)
{
cin>>row>>collumn>>stone_num;
for(int i = 0; i < stone_num; i++)
cin>>stone_pos[i].x>>stone_pos[i].y;
#endif
num_of_situation = 0;
//////////////////////////////////////////////////////////////////////////
sort(stone_pos, stone_pos+stone_num, MyCompXY);
pre_x = stone_pos[0].x;
pre_y = stone_pos[0].y;
if(pre_y >= 3)
num_of_situation++;
diff_row_num = 1;
for(int i = 1; i < stone_num; i++)
{
if(stone_pos[i].x != pre_x)//换行
{
diff_row_num++;
if(collumn - pre_y >= 2)//上一行的行末是否可以容纳2
num_of_situation++;
if(stone_pos[i].y >= 3)//新行
num_of_situation++;
}
else if(stone_pos[i].y - pre_y >= 3)
num_of_situation++;
pre_x = stone_pos[i].x;
pre_y = stone_pos[i].y;
}
if(collumn - pre_y >= 2)//最后石头的行末是否可以容纳2
num_of_situation++;
if(collumn >= 2)
num_of_situation += (row - diff_row_num);//空白行数
//////////////////////////////////////////////////////////////////////////
sort(stone_pos, stone_pos+stone_num, MyCompYX);
pre_x = stone_pos[0].x;
pre_y = stone_pos[0].y;
if(pre_x >= 3)
num_of_situation++;
diff_coll_num = 1;
for(int i = 1; i < stone_num; i++)
{
if(stone_pos[i].y != pre_y)//换列
{
diff_coll_num++;
if(row - pre_x >= 2)//上一列的列末是否可以容纳2
num_of_situation++;
if(stone_pos[i].x >= 3)//新列
num_of_situation++;
}
else if(stone_pos[i].x - pre_x >= 3)
num_of_situation++;
pre_x = stone_pos[i].x;
pre_y = stone_pos[i].y;
}
if(row - pre_x >= 2)//最后石头的列末是否可以容纳2
num_of_situation++;
if(row >= 2)
num_of_situation += (collumn - diff_coll_num);//空白列数
cout<<num_of_situation<<endl;
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator