A.直接模拟吧不解释了
import random
import sys
import os
import math
from collections import Counter, defaultdict, deque
from functools import lru_cache, reduce
from itertools import accumulate, combinations, permutations
from heapq import nsmallest, nlargest, heapify, heappop, heappush
from io import BytesIO, IOBase
from copy import deepcopy
import threading
import bisect
BUFSIZE = 4096
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
def I():
return input()
def II():
return int(input())
def MI():
return map(int, input().split())
def LI():
return list(input().split())
def LII():
return list(map(int, input().split()))
def GMI():
return map(lambda x: int(x) - 1, input().split())
def LGMI():
return list(map(lambda x: int(x) - 1, input().split()))
def solve():
n=II()
a=I()
b=a
for x,y in zip(a,b):
print(x,end="")
print(y,end="")
if __name__ == '__main__':
for _ in range(1):
solve()
B.也直接模拟c的话记得开longlong
import random
import sys
import os
import math
from collections import Counter, defaultdict, deque
from functools import lru_cache, reduce
from itertools import accumulate, combinations, permutations
from heapq import nsmallest, nlargest, heapify, heappop, heappush
from io import BytesIO, IOBase
from copy import deepcopy
import threading
import bisect
BUFSIZE = 4096
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
def I():
return input()
def II():
return int(input())
def MI():
return map(int, input().split())
def LI():
return list(input().split())
def LII():
return list(map(int, input().split()))
def GMI():
return map(lambda x: int(x) - 1, input().split())
def LGMI():
return list(map(lambda x: int(x) - 1, input().split()))
def solve():
a=LII()
res=0
for i in range(0,64):
res+=pow(2,i)*a[i]
print(res)
if __name__ == '__main__':
for _ in range(1):
solve()
C.把每个数字位置拿出来排个序即可
import random
import sys
import os
import math
from collections import Counter, defaultdict, deque
from functools import lru_cache, reduce
from itertools import accumulate, combinations, permutations
from heapq import nsmallest, nlargest, heapify, heappop, heappush
from io import BytesIO, IOBase
from copy import deepcopy
import threading
import bisect
BUFSIZE = 4096
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
def I():
return input()
def II():
return int(input())
def MI():
return map(int, input().split())
def LI():
return list(input().split())
def LII():
return list(map(int, input().split()))
def GMI():
return map(lambda x: int(x) - 1, input().split())
def LGMI():
return list(map(lambda x: int(x) - 1, input().split()))
def solve():
n=II()
p=[[] for _ in range(0,n+10)]
a=LII()
for i,x in enumerate(a):
p[x].append(i+1)
res=[]
for i in range(1,n+1):
p[i].sort()
res.append([p[i][1],i])
res.sort()
for i in range(0,n):
print(res[i][1],end=" ")
if __name__ == '__main__':
for _ in range(1):
solve()
D
很明显一个状态机dp
状态表示,前i个选项里的最大值,且当前状态是无毒或者有毒
如果有毒,只能从前面的无毒转移过来或者不选当前选项从前面有毒继续转移过来
如果无毒,可以从前面有毒或者无毒或者不选转移过来
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
const int N = 5e5+10;
typedef pair<int, int> PII;
int n,m,k;
PII a[N];
long long f[N][3];
signed main(){
cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);
cin>>n;
//f[0]代表当前有毒,[1]无毒
for(int i=1;i<=n;i++){
cin>>a[i].first>>a[i].second;
}
memset(f,-0x3f,sizeof(f));
f[0][0]=0;
f[0][1]=0;
for(int i=1;i<=n;i++)
{
if(a[i].first==0)
{
f[i][0]=f[i-1][0];
f[i][1]=max({f[i-1][1]+a[i].second,f[i-1][1],f[i-1][0]+a[i].second});
}
else{
f[i][1]=f[i-1][1];
f[i][0]=max({f[i-1][0],f[i-1][1]+a[i].second});
}
}
cout<<max(f[n][0],f[n][1])<<"\n";
}
E
拿两个set维护
第一个set维护最大的k个,第二个维护剩余数
每次把全部数的最大的k个通过st1和st2的最小值和最大值交换即可
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
const int N = 5e5+10;
int n,m,k;
int a[N];
signed main(){
cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);
cin>>n>>k>>m;
multiset<long long> st1,st2;
long long res=0;
for(int i=1;i<=n;i++){
if(i<=k) st1.insert(0);
else st2.insert(0);
}
while (m -- )
{
int x,y;cin>>x>>y;
int z=a[x];
if(st1.find(a[x])!=st1.end())
{
st1.erase(st1.lower_bound(a[x]));
res-=a[x];
}
else
{
st2.erase(st2.lower_bound(a[x]));
}
a[x]=y;
st1.insert(a[x]);
res+=a[x];
while(st1.size()>k)
{
auto it=*st1.begin();
st2.insert(it);
st1.erase(st1.begin());
res-=it;
}
while(st1.size()&&st2.size()&&*st1.begin()<*st2.rbegin())
{
auto a=*st1.begin(),b=*st2.rbegin();
res+=b-a;
st2.insert(a);st2.erase(st2.lower_bound(b));
st1.insert(b);st1.erase(st1.lower_bound(a));
}
cout<<res<<"\n";
}
}
F.离散化+树状数组
首先先别想那么多,对于第一个集合的第一个数他的所有贡献是啥呢
贡献就是下面全部集合比他小的值的全部个数
所以要有个数据结构可以边添加边询问前缀的总和(树状数组或者线段树)
从最后一个集合遍历即可,边加贡献边添加数
到这里还没结束,因为还没算自己集合的,自己集合前面比自己小的数也要算贡献
第i个集合下面有(n-i)个集合要和他匹配,且前面有j个比自己小,别忘了这个贡献
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
const int N = 1e4+10,M=110,H=1e6+10;
typedef pair<int, int> PII;
int n,m,k;
int a[N][M];
vector<int> num;
int tr[H];
int lowbit(int x)
{
return x & -x;
}
void add(int x, int c) // 位置x加c
{
for (int i = x; i <=num.size(); i += lowbit(i)) tr[i] += c;
}
int sum(int x) // 返回前x个数的和
{
int res = 0;
for (int i = x; i; i -= lowbit(i)) res += tr[i];
return res;
}
int find(int x){
return lower_bound(num.begin(),num.end(),x)-num.begin()+1;
}
signed main(){
cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++)
{
cin>>a[i][j];
num.push_back(a[i][j]);
}
}
sort(num.begin(),num.end());
num.erase(unique(num.begin(),num.end()),num.end());
long long res=0;
for(int i=n;i>=1;i--)
{
for(int j=1;j<=m;j++)
{
int x=find(a[i][j]);
res+=sum(x);
res+=(n-i)*j;
}
for(int j=1;j<=m;j++){
add(find(a[i][j]),1);
}
}
cout<<res<<"\n";
}