4.10

· · 个人记录

正常发挥 |A |B |C |D |E |总 |排 | |:---:|:---:|:--------:|:-:|:--:|:--------:|:-:| |100|100|100(如果评测机心情不好70)|0|70|340~370|1|

A.T750436

非常简单的一道map模版题,map的第一项存商品名字,第二项存商品赚的钱,记住第一次出现要把赚的钱数\times0.75

#include <bits/stdc++.h>
using namespace std ;
int n ;
string s ;
double p ;
map <string , double> mp ; 
int main()
{
    cin >> n ;
    while(n --)
    {
        cin >> s >> p ;
        if(mp.count(s))
        {
            mp[s] += p ;
        }
        else
        {
            mp[s] += p * 0.75 ;
        }
    }
    for(auto tm : mp)
    {
        cout << tm.first << " " ;
        printf("%.1lf\n" , tm.second) ;
    }
    return 0 ;
} 

B.T739897

我们手推一下会发现……

a_1=a_1 \\ a_1\oplus a_2\oplus(a_1\oplus a_2)=0 \\ a_1\oplus a_2\oplus a_3\oplus(a_1\oplus a_2)\oplus(a_2\oplus a_3)\oplus(a_1\oplus a_2\oplus a_3)=a_1\oplus a_3 \\ ...

类似的,我们用f(l,r)表示a_l到a_r的所有子区间的异或和的异或和,那我们可以推理出……

f(l,r)=\begin{cases} a_l+a_{l+2}+\cdots+a_r&r-l+1\equiv1\pmod2\\ 0&r-l+1\equiv0\pmod2 \end{cases}

证明如下
:::success[证明]{open} 为了简化,我们统一用f(1,a)来表示所有类似f(1+x,a+x)长度一样的

假设你要找a_x出现了多少次,你会怎么算?其实我们可以抽象成找到两个数字l,r满足1\le l\le x\le r\le a快看多么完美的不等式,我们可以轻易发现l\in\{n\in\N_+|n\le x\},r\in\{n\in\N_+|x\le n\le a\},l有x-1+1=x种选择,r有a-x+1种选择,所以a_x总共出现了x(a-x+1)次

然后我们接下来可以分析奇偶性,因为:

\bigoplus_{i=1}^ax=\begin{cases} x&a\equiv1\pmod2\\ 0&a\equiv0\pmod2\\ \end{cases}

我们可以分类讨论

\small x(a-x+1)\begin{cases} 偶&x为偶(x为偶\rarr x(a-x+1)为偶)\\ 偶&a为偶,x为奇(a偶,x奇\rarr a-x+1偶\rarr x(a-x+1)偶)\\ 奇&a,x都奇(a,x都奇\rarr a-x+1奇\rarr x(a-x+1)奇) \\ \end{cases}
应该懂了吧
```cpp line-numbers
#include <bits/stdc++.h>
using namespace std ;
int n , q , a[5005] , op , l , r ;
int main()
{
cin >> n >> q ;
for(int i = 1 ; i <= n ; i ++)
{
cin >> a[i] ;
}
while(q --)
{
cin >> op >> l >> r ;
if(op == 1) a[l] = r ;//修改操作
else
{
int sum = 0 ;
if((r - l + 1) % 2)//只当长度是奇数时才需要异或
{
for(int i = l ; i <= r ; i += 2)//隔一个异或一个
{
sum ^= a[i] ;
}
}
cout << sum << "\n" ;
}
}
return 0 ;
}
```
# C.P2058
大家请看这么冤的代码,如果我不是评测机心情好只能拿70(不优化的838ms,优化的200ms)
```cpp line-numbers lines=26-30
#include <bits/stdc++.h>
using namespace std ;
int n , t , k , x , f[100005] ;
struct node
{
int t , x ;
} ;//第一项存时间,第二项存国籍
queue <node> q ;
int main()
{
cin >> n ;
while(n --)
{
cin >> t >> k ;
while(k --)
{
cin >> x ;
q.push({t , x}) ;//进队
f[x] ++ ;//标记每个国家的人数
}
while(q.front().t <= t - 86400)//把超过一天的人给退了
{
f[q.front().x] -- ;
q.pop() ;
}
int cnt = 0 ;
for(int i = 1 ; i <= 1e5 ; i ++)//你这时间复杂度不对吧?
{
cnt += bool(f[i]) ;
}
cout << cnt << "\n" ;
}
return 0 ;
}
```
很明显因为里面有一个10^5的嵌套暴力,所以最多要运行10^5\times n\le10^{10}次(那看样子要增强数据了),其实我们更新cnt可以像双指针一样正在途中更新
```cpp line-numbers lines=20,25
#include <bits/stdc++.h>
using namespace std ;
int n , t , k , x , f[100005] , cnt ;
struct node
{
int t , x ;
} ;
queue <node> q ;
int main()
{
cin >> n ;
while(n --)
{
cin >> t >> k ;
while(k --)
{
cin >> x ;
q.push({t , x}) ;
f[x] ++ ;
if(f[x] == 1) cnt ++ ;//第一次出现
}
while(q.front().t <= t - 86400)
{
f[q.front().x] -- ;
if(f[q.front().x] == 0) cnt -- ;//已经没有这个国家的人了
q.pop() ;
}
cout << cnt << "\n" ;
}
return 0 ;
}
```
# D.T750439
我们可以使用mid表示中位数,还有一个小跟堆和大根堆来存储除了第一个所有的元素,然而你们知道的,小跟堆的最大值<mid<大根堆的最小值,然后我们还要保持大根堆和小跟堆的元素数尽可能相同
```cpp line-numbers
#include <bits/stdc++.h>
using namespace std ;
int n , a[100005] ;
priority_queue <int , vector <int> , less <int> > pq1 ;
priority_queue <int , vector <int> , greater <int> > pq2 ;
int mid ;
int main()
{
cin >> n ;
for(int i = 1 ; i <= n ; i ++)
{
cin >> a[i] ;
}
mid = a[1] ;
cout << mid << "\n" ;
for(int i = 2 ; i <= n ; i ++)
{
if(a[i] > mid) pq2.push(a[i]) ;//放在元素较小的大根堆里
else pq1.push(a[i]) ;//放在元素较大的小跟堆里
if(i % 2)//只有总元素数量是偶数时才能保持一样
{
while(pq1.size() != pq2.size())
{
if(pq1.size() > pq2.size())//把pq1的元素放在pq1
{
pq2.push(mid) ;
mid = pq1.top() ;
pq1.pop() ;
}
else//同理
{
pq1.push(mid) ;
mid = pq2.top() ;
pq2.pop() ;
}
}
cout << mid << "\n" ;
}
}
return 0 ;
}
```
# E.T750440
暴力:我们可以用vector来存,因为不能看记录我就不写代码了

正解:
要用set,本质上就是模拟

#include <bits/stdc++.h>
using namespace std ;
int n , a , now ;
bool p ;
set <int> s1 , s2 ;
int main()
{
    ios::sync_with_stdio(0) ;
    cin.tie(0) ;
    cout.tie(0) ;
    cin >> n ;
    for(int i = 1 ; i <= n ; i ++)
    {
        cin >> a ;
        if(a) s1.insert(i) ;//如果是苹果插入第一个set
        else s2.insert(i) ;//否则插入第二个
    }
    s1.insert(1e9) ;//防止集合为空
    s2.insert(1e9) ;
    p = *s1.begin() >= *s2.begin() ;//看先去哪一个
    while(!p && s1.size() > 1 || p && s2.size() > 1)
    {
        if(!p)
        {
            now = *s1.upper_bound(now) ;//找到第一个
            if(now == 1e9)//如果不存在
            {
                now = 0 ;
                p = *s1.begin() >= *s2.begin() ;
                cout << "\n" ;//跳转下一个
                continue ;
            }
            cout << now << " " ;//否则的话取出来
            s1.erase(now) ;
            p = !p ;
        }
        else//同理
        {   
            now = *s2.upper_bound(now) ;
            if(now == 1e9)
            {
                now = 0 ;
                p = *s1.begin() >= *s2.begin() ;
                cout << "\n" ;
                continue ;
            }
            cout << now << " " ;
            s2.erase(now) ;
            p = !p ;
        }
    }
    cout << "\n" ;
    while(s1.size() > 1)//把剩下的单独输出
    {
        cout << *s1.begin() << "\n" ;
        s1.erase(*s1.begin()) ;//因为每次只会拿一个,所以输出"\n"
    }
    while(s2.size() > 1)//同理
    {
        cout << *s2.begin() << "\n" ;
        s2.erase(*s2.begin()) ;
    }
    return 0 ;
}

总结:

也是非常的冤,只要一个小错误就可以扣几十分,累积下来就是一百来分下次要加大样例了,还好我没有