P6153 询问-题解
很好的一道题。
1 仔细分析题目
开始我想用并查集水过,结果
直到看到
他保证每次给出的条件都是有用的,也就是说,每次给出的条件无法由之前的条件推导得来。
那也就是说,我们开始把每个物品都看成一种,这样总共就有
注意开 unsigned long long !
2 跑得快
算法知道了,开始研究怎样跑得快吧
2.1 1 级
直接按上述说明模拟即可,代码如下
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ULL;
ULL n,m,x,y;
int main(){
scanf("%lld%lld",&n,&m);
for(ULL i=1;i<=m;i++){
scanf("%lld%lld",&x,&y);
n--;
}
printf("%lld",n);
return 0;
}
2.2 2 级
既然每读入一个操作就 n-- ,有 n-- 要执行
那么输入数据呢?不用管ta
在洛谷,即使输入数据没读完也不会
\color{Purple}\mathtt{RE} --《深入浅出-基础篇》
于是又可以轻松得出代码
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ULL;
ULL n,m,x,y;
int main(){
scanf("%lld%lld",&n,&m);//直接输出n-m即可
printf("%lld",n-m);
return 0;
}
2.3 3 级
还可以更快吗?
快读应运而生!!!unsigned long long 哟
typedef unsigned long long ULL;
inline ULL read(){
char c;
bool flag=false;
while((c=getchar())<'0'||c>'9')
if(c=='-') flag=true;
ULL res=c-'0';
while((c=getchar())>='0'&&c<='9')
res=(res<<3)+(res<<1)+c-'0';
return flag? -res:res;
}
于是完整代码完美出炉
#include <cstdio>
using namespace std;
typedef unsigned long long ULL;
ULL n,m,x,y;
inline ULL read(){
char c;
bool flag=false;
while((c=getchar())<'0'||c>'9')
if(c=='-') flag=true;
ULL res=c-'0';
while((c=getchar())>='0'&&c<='9')
res=(res<<3)+(res<<1)+c-'0';
return flag? -res:res;
}
int main(){
n=read(),m=read();
printf("%lld",n-m);
return 0;
}
3 结语
在确保代码正确性的基础上一定要加速代码,这样排名才会高!
比如这张图(比赛刚开始):
可见排名高的都卡了时间!