Codeforences Educational Round10 C. Foe Pairs
2016-03-26 14:53
507 查看
【题意】给了n个数的1个排列,给了m个特殊的二元组,你可以在这n个数里面选出两个数作为一对组合,现在要求选出的这两个数不能包含m对pair的任何一对,求满足这个条件的二元组的个数。
【AC代码】
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
#define inf 0x3fffffff
#define ll long long
int p[300005],b[300005];
int main()
{
int n,m;
scanf("%d%d",&n,&m);
int x,y,x1,y1;
for(int i=1; i<=n; i++)
{
scanf("%d",&x);
p[x]=i;
}
for(int i=1; i<=n; i++)b[i]=inf;
for(int i=1; i<=m; i++)
{
scanf("%d %d",&x,&y);
x1=p[x],y1=p[y];
if(x1>y1)swap(x1,y1);
b[x1] = min(b[x1],y1);
}
for(int i=n-1; i>=1; i--)
{
b[i] = min(b[i],b[i+1]);
}
ll ans=0;
for(int i=1; i<=n; i++)
{
if(b[i]==inf)ans += (n-i+1);
else ans+=b[i]-i;
}
printf("%I64d\n",ans);
return 0;
}
【AC代码】
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
#define inf 0x3fffffff
#define ll long long
int p[300005],b[300005];
int main()
{
int n,m;
scanf("%d%d",&n,&m);
int x,y,x1,y1;
for(int i=1; i<=n; i++)
{
scanf("%d",&x);
p[x]=i;
}
for(int i=1; i<=n; i++)b[i]=inf;
for(int i=1; i<=m; i++)
{
scanf("%d %d",&x,&y);
x1=p[x],y1=p[y];
if(x1>y1)swap(x1,y1);
b[x1] = min(b[x1],y1);
}
for(int i=n-1; i>=1; i--)
{
b[i] = min(b[i],b[i+1]);
}
ll ans=0;
for(int i=1; i<=n; i++)
{
if(b[i]==inf)ans += (n-i+1);
else ans+=b[i]-i;
}
printf("%I64d\n",ans);
return 0;
}
相关文章推荐
- [LeetCode] Paint House II 粉刷房子之二
- 职责链模式
- 2440test 裸机測试 调试不进main 设置改动方法
- WHY IE AGAIN? - string.charAt(x) or string[x]?
- 现在不能使用foxmail同步qq记事本功能,可能是对字数的大小有限制
- HDU 5046 Airport ( Dancing Links 反复覆盖 )
- codeforces_652C. Foe Pairs
- 如何理解naive Bayes
- codeforces 651B Beautiful Paintings
- cf#ecr10-C. Foe Pairs- two pointers
- wait 和 sleep 的区别
- hdoj 1787 GCD Again (欧拉函数模板 )
- 对await(),notify()的理解
- CodeForces 652C Foe Pairs
- hdu 3487 play with chain
- 【杭电】[1787]GCD Again
- 解决spark运行中failed to locate the winutils binary in the hadoop binary path的问题
- boostrap中模态窗口的学习与总结
- org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
- lintcode:Paint House