统计数字
送分题,但是我不会这个迭代器,所以超时了,统计个数,只需要记得排个序就行了OvO
100pts
C++ 复制 #include <bits/stdc++.h>
#define int long long
using namespace std ;
const int N= 2 e 5 + 5 ;
int n,a[N];
map<int ,int> vis;
signed main ()
{
cin>> n;
for (int i= 1 ;i<= n;i++ ){
cin>> a[i];
vis[a[i]]++ ;
}
for (auto i:vis)cout<< i.first<< " " << i.second<< " \n " ;
return 0 ;
}
跳房子
一个十分简单的dfs/bfs,尝试从每一个点出发,跳五次(不用标记走没走过,因为可以重复)六个点,用mp标记这个六位数有没有重复过,若没有重复过,答案就+1。最后输出就行
100pts
C++ 复制 #include <bits/stdc++.h>
using namespace std ;
char a[10 ][10 ];
int num,ans;
int dx[]= {1 ,- 1 ,0 ,0 };
int dy[]= {0 ,0 ,1 ,- 1 };
map<int ,int> mp;
set< string> st;
void f (int x ,int y ,string s )
{
if (s.size ()== 6 ){
st.insert (s);
return ;
}
for (int i= 0 ;i< 4 ;i++ )
{
int nx= x+ dx[i];
int ny= y+ dy[i];
if (nx> 5 || nx< 1 || ny> 5 || ny< 1 )continue ;
f (nx,ny,s+ a[nx][ny]);
}
}
int main ()
{
for (int i= 1 ;i<= 5 ;i++ )
for (int j= 1 ;j<= 5 ;j++ )
cin>> a[i][j];
for (int i= 1 ;i<= 5 ;i++ )
{
for (int j= 1 ;j<= 5 ;j++ )
{
f (i,j,"" );
}
}
cout<< st.size ();
return 0 ;
}
营业额统计
每一天,由于需要找与这一天最相近的(也就是差最小的),所以我们可以想使用lower_bound(),虽然序列并非递增或递减,但是set会帮助我们自动排序,所以可以把这个序列存入set里,接着只用lower_bound()找最相近的元素,但是需要注意的是,我们要考虑如果找不到相同的元素,就要比较比他大和比它小的与本身的差绝对值谁最优。
100pts
C++ 复制 #include <bits/stdc++.h>
using namespace std ;
#define int long long
set<int> Set;
int n,x,ans;
signed main ()
{
Set.insert (1 e 9 );
Set.insert (- 1 e 9 );
cin>> n>> x;
ans+= x;
Set.insert (x);
for (int i= 2 ;i<= n;i++ )
{
cin>> x;
auto it= Set.lower_bound (x);
if (* it!= x)
{
auto it1= it;
it2-- ;
ans+= min (abs (* it- x),abs (* it1- x));
Set.insert (x);
}
}
cout<< ans<< endl;
return 0 ;
}
ZNANSTVENIK
这道题要删掉最少行数,让每一列拼出的字符串都不重复,先把所有行都标记成删掉,我们从第一行开始,一行一行试着删除,每删一行就检查一次:把每一列剩下的字符拼成一串,用map判断有没有重复。如果所有串都不一样,就说明删够了,直接输出总行数-未删掉的行数就行。
100pts
C++ 复制 #include <bits/stdc++.h>
using namespace std ;
const int N= 1 e 3 + 5 ;
int n,m;
bool bj[N];
map< string, bool> vis;
char a[N][N];
bool f ()
{
vis.clear ();
for (int i= 1 ;i<= m;i++ )
{
string s= "" ;
for (int j= n;j>= 1 ;j-- )
{
if (bj[j]== 1 )continue ;
s+= a[j][i];
}
if (vis[s]== 1 )return 0 ;
vis[s]= 1 ;
}
return 1 ;
}
int main ()
{
cin>> n>> m;
for (int i= 1 ;i<= n;i++ )
{
for (int j= 1 ;j<= m;j++ )cin>> a[i][j];
bj[i]= 1 ;
}
int num= 0 ;
for (int i= n;i>= 1 ;i-- )
{
num++ ;
bj[i]= 0 ;
if (f ())break ;
}
cout<< n- num;
return 0 ;
}
鬼子进村
D x:鬼子将 x 号房子摧毁了,将x放入set和stack里面,分别表示此位置被摧毁与最新一次的炸毁点。
R :村民们将鬼子上一个摧毁的房子修复了,删除栈顶也就是最近一次炸毁的位置,再把这个修好的位置从set中删掉。
Q x:有一名士兵被围堵在 x 号房子中,在set中找到第一个≥x的炸毁点记为右端点r(当然如果士兵现在站的这个点就已经被炸毁了,那么为0),接着找x左边第一个被炸毁的点记为左端点l,最后他们之间的距离就是r-l-1。
100pts
C++ 复制 #include <bits/stdc++.h>
#define int long long
using namespace std ;
int n,m,ans;
stack<int> stk;
set<int> se;
signed main ()
{
cin>> n>> m;
se.insert (0 );
se.insert (n+ 1 );
while (m-- )
{
char op;
int x;
cin>> op;
if (op== 'D' )
{
cin>> x;
se.insert (x);
stk.push (x);
}
else if (op== 'R' )
{
x= stk.top ();
stk.pop ();
auto it= se.lower_bound (x);
se.erase (it);
}
else if (op== 'Q' )
{
cin>> x;
auto it= se.lower_bound (x);
if (* it== x)cout<< "0 \n " ;
else
{
int l,r;
r=* it;
it-- ;
l=* it;
cout<< r- l- 1 << " \n " ;
}
}
}
return 0 ;
}