CodeForces - 811B Vladik and Complicated Book

/*
此题最初太过想当然了,非常不好,当初TLE的时候,也没有想想能不能用更好的方法,毕竟排序的效率应该没有查找快,而我...每次都要用memcpy复制页数的顺序,每次都要调用一次sort()函数,应该会产生不小的时间开销

后来发现,真的有简单的思路,而且不止一种

1. 可以找到[l,r]区间对应的位置的数组元素中,页码比x对应的页码小的个数,记作tot,若l+tot==x,则没有变动,否则变动
2. 也可以分别统计,[l,x]区间对应的位置的数组元素中,页码比x大的,和[x,r]区间对应的位置的数组元素中,页码比x小的个数,若两者相等,则不会变动,否则变动

参考借鉴至网址:
http://blog.csdn.net/szh_0808/article/details/72793597

再次提醒:
不要想当然,不要想当然!!!第一时间能想到的方法,往往不是效率最高的

再贴上自己的TLE代码,以防止重蹈覆辙!
//Time limit exceeded on test 35
#include <bits/stdc++.h>
using namespace std;
const int N = 10005;
int a[N], b[N];

struct change
{
	int l, r, x;
	
	int notchanged()
	{
		sort(b+l-1, b+r);
		return (b[x-1] == a[x-1])?1:0;
	} 
}c[N];
istream& operator >> (istream&in, change &d)
{
	in >> d.l >> d.r >> d.x;
	return in;
}

int main()
{
	cin.tie(0);
    cin.sync_with_stdio(false);
	int n, m;
	cin >> n >> m;
		for (int i = 0; i < n; i++)
		cin >> a[i];
		for (int i = 0; i < m; i++)
		{
			cin >> c[i];
		}
		for (int i = 0; i < m; i++)
		{
			if (c[i].l == c[i].r || c[i].x < c[i].l || c[i].x > c[i].r)
			{
				cout << "Yes" << endl; continue;
			} 
			memcpy(b, a, sizeof(int)*n);
			if (c[i].notchanged()) cout << "Yes" << endl;
			else cout << "No" << endl;
		}
	return 0;
}

另外注意这句话的理解:
For every of such sorting Vladik knows number x — what index of page in permutation he should read.
x表示的是,它是在整个全排列中的第几个,而不是表示在[l,r]中的第几个

*/



#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 10;
int a[N];
struct change
{
	int l, r, x;
	
	void solve1()
	{
		if (l == r)
		{
			cout << "Yes" << endl; return;
		}
		int s1 = 0, s2 = 0;
		for (int i = l; i <= x; i++)
		{
			if ( a[i] > a[x] ) s1++;
		}
		for (int i = x; i <= r; i++)
		{
			if ( a[i] < a[x] ) s2++;
		}
		if (s1 == s2) cout << "Yes" << endl;
		else cout << "No" << endl;
	}
	
	void solve2()
	{
		if ( l == r )
		{
			cout << "Yes" << endl; return;
		}
		int s = 0;
		for (int i = l; i <= r; i++)
			if ( a[i] < a[x] )
			s++;
			
		if ( s + l == x ) cout << "Yes" << endl;
		else cout << "No" << endl;
		
	}
}c[N];
istream& operator >> (istream&in, change &d)
{
	in >> d.l >> d.r >> d.x;
	return in;
}
int main()
{
	int n, m;
	while (cin >> n >> m)
	{
		for (int i = 1; i <= n; i++) cin >> a[i];
		
		for (int i = 1; i <= m; i++) cin >> c[i];
		
		for (int i = 1; i <= m; i++) c[i].solve2();
		
	}
	return 0;
}


posted @ 2017-08-26 09:16  mofushaohua  阅读(157)  评论(0编辑  收藏  举报