hdu第一场


2020 Multi-University Training Contest 1

1004 Distinct Sub-palindromes(签到)

题意:给定长度为 n n n的字符串 S S S,任务是输出拥有最少不同子回文串个数的 S S S的个数
思路:
一开始以为是dp推序列,组合数学开始了…
但是手推一下发现好像就是一个简单签到题
先找拥有最少的子回文串的序列

  • n = 1 n=1 n=1:26个字母
  • n = 2 n=2 n=2 2 6 2 26^2 262 a a aa aa有俩: a , a a a,aa aaa a b ab ab有俩: a , b a,b a,b
  • n = 3 n=3 n=3 2 6 3 26^3 263(自己手推一下,发现咋放都可以,不同子回文串的个数都是3)
  • n ≥ 4 : A 3 26 = 26 ∗ 25 ∗ 24 n\ge4:A_{3}^{26}=26*25*24 n4:A326=262524,选择任意三个不同的字母,按着规定顺序排列(ex:abcabc),不同子回文串的个数都是3.
    代码:
int main() {
    
    
    LL t, n;
    LL ans = 0;
    cin >> t;
    while (t--)
    {
    
    
        cin >> n;
        if (n <= 3) {
    
    
            cout << qpow(26, n) << endl;
        }
        else
        {
    
    
            ans = (26 * 25 * 24) % MOD;
            cout << ans << endl;
        }

    }
    return 0;
    
}

1009 Leading Robots

Leading Robots
题意:已知机器人 v ( t ) = a ∗ t v(t)=a*t v(t)=at,现有 n n n个机器人,给定机器人的当前位置 p o s i t i o n position position,加速度 a a a。问经过无限长的时间后,有多少个机器人当过第一位?
思路:没啥思路,最后肯定是a最大的机器人排在第一位,把他们画成二维坐标上的线可能会清楚一点。感觉要sort搞一搞,但是到最后都没做出^^。

题目大意:给你每个机器人的初始位置和加速度,问你总共有多少个机器人有可能成为一次领导机器人(在最前面)。我们先给每个机器人的位置排序,按照位置最大的,加速度最大的优先,然后我们用栈来保存每个可能成为领导的机器人的位置,加速度和其成为领导的时间,那么很明显,在后面的机器人,如果加速度小于等于当前的机器人,那么他们是不可能成为领导人的,所以我们循环的时候可以直接continue。如果加速度大的话,那么它一定能够追上前面的机器人。

所以接下来我们要算一下它追上前面机器人的时间,希望大家的物理都学的不错,有公式 s = v 0 t + 1 2 a t 2 s=v_0t+\frac{1}{2}at^2 s=v0t+21at2。那么它追上前面那个机器人的世界就是当他们的路程一样的时候,也就是

p o s t a i l − p o s i + 0 t + 1 2 a t a i l t 2 = 0 t + 1 2 a i t 2 pos_{tail}-pos_i+0t+\frac{1}{2}a_{tail}t^2=0t+\frac{1}{2}a_it^2 postailposi+0t+21atailt2=0t+21ait2

那么为了使得我们的结果是准确的,我们将式子化为 1 2 t 2 = p o s t a i l − p o s i a i − a t a i l \frac{1}{2}t^2=\frac{pos_{tail}-pos_i}{a_i-a_{tail}} 21t2=aiatailpostailposi,那么我们不用将 t t t算出来,只需要用他来比较大小,所以 1 2 t 2 \frac{1}{2}t^2 21t2完全可以代替它的,接下来我们再用分数来保存每个时间。

然后如果当前的机器人追上栈顶机器人的时间小于等于栈顶机器人成为领导人的时间,那么我们将做出栈操作,也就是说栈顶的机器人在超越别人之前已经被人超越了。

最后需要判断一下栈内是否有位置一样,加速度一样的元素,这个我们可以做个map来计数。

代码:

struct fenshu
{
    
    
	int fz, fm;
	bool operator <(const fenshu &a)const {
    
    
		return (LL)fz*a.fm < (LL)fm*a.fz;
	}
	bool operator <=(const fenshu &a)const {
    
    
		return (LL)fz*a.fm <= (LL)fm*a.fz;
	}
	bool operator ==(const fenshu &a)const {
    
    
		return (LL)fz*a.fm == (LL)fm*a.fz;
	}
}times[maxn];
map<pii, int > mp;
struct node {
    
    
	int p, a;
}cun[maxn];
bool cmp(node a,node b){
    
    
	if (a.p == b.p)return a.a > b.a;
	return a.p > b.p;
}
stack<int> houxuan;
stack<fenshu>q;
int main()
{
    
    
	int t, n, nw, ans, leader;
	int st, temp;
	pii p;
	sci(t);
	while (t--)
	{
    
    
		
		while (!q.empty())q.pop();
		while (!houxuan.empty())houxuan.pop();
		mp.clear();
		ans = 0;
		
		sci(n);
		for (int i = 1; i <= n; i++) {
    
    
			sci(cun[i].p);
			sci(cun[i].a);
			p.first = cun[i].p;
			p.second = cun[i].a;
			mp[p]++;
		}
		sort(cun + 1, cun + 1 + n, cmp);
		leader = 1;
		

		//int st;
		houxuan.push(1);
		q.push(fenshu{
    
     0,1 });
		for (int i = 2; i <= n; i++) {
    
    
			if (cun[i].a <= cun[houxuan.top()].a)continue;
			while ((!houxuan.empty()) && (fenshu {
    
     cun[houxuan.top()].p - cun[i].p, cun[i].a - cun[houxuan.top()].a } <= q.top())) {
    
    
				houxuan.pop();
				q.pop();
			}
			q.push(fenshu{
    
     cun[houxuan.top()].p - cun[i].p, cun[i].a - cun[houxuan.top()].a });
			houxuan.push(i);
		}
		while (!houxuan.empty())
		{
    
    
			p.first = cun[houxuan.top()].p;
			p.second = cun[houxuan.top()].a;
			if (mp[p] == 1)ans++;
			houxuan.pop();
		}
		printf("%d\n", ans);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44986601/article/details/108678684