HDOJ多校联合第五场

时间:2024-01-12 19:57:32

1001 Inversion

题意:求逆序对,然后交换k次相邻的两个数,使得剩下的逆序对最少。

分析:题目用到的结论是:数组中存在一对逆序对,那么可以通过交换相邻两个数使得逆序对减少1,交换k次,可以最多减少k个。

嘉定ai>aj,i < j,如果ai,aj相邻的,那么显然可以通过交换减少1;不相邻的情况,

考虑ak,k = j-1;

#11:ak > aj,那么ak,aj构成逆序对,交换后逆序对减少1;

#12:ak<=aj,那么ai,ak构成逆序对,问题转化为更小规模,可以通过同样的方法进一步分析,最终一定能够交换一次使得逆序对减少1个;

考虑ak,k = i+1;

#21:ak<ai,那么ai,ak构成逆序对,可以交换;

#22:ak>=ai那么ak,aj构成逆序对,问题规模缩小,和#12同样的处理方法。

所以最终答案就是max(0LL, ans-k)嘛,

代码:

分治法

 //Template updates date: 20140718
#include <iostream>
#include <sstream>
#include <cstdio>
#include <climits>
#include <ctime>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <string>
#include <stack>
#include <set>
#include <map>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#define esp 1e-6
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
#define pb push_back
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define lowbit(x) (x&(-x))
#define mp(a, b) make_pair((a), (b))
#define bit(k) (1<<(k))
#define iin freopen("pow.in", "r", stdin);
#define oout freopen("pow.out", "w", stdout);
#define in freopen("solve_in.txt", "r", stdin);
#define out freopen("solve_out.txt", "w", stdout);
#define bug puts("********))))))");
#define Inout iin oout
#define inout in out #define SET(a, v) memset(a, (v), sizeof(a))
#define SORT(a) sort((a).begin(), (a).end())
#define REV(a) reverse((a).begin(), (a).end())
#define READ(a, n) {REP(i, n) cin>>(a)[i];}
#define REP(i, n) for(int i = 0; i < (n); i++)
#define VREP(i, n, base) for(int i = (n); i >= (base); i--)
#define Rep(i, base, n) for(int i = (base); i < (n); i++)
#define REPS(s, i) for(int i = 0; (s)[i]; i++)
#define pf(x) ((x)*(x))
#define mod(n) ((n))
#define Log(a, b) (log((double)b)/log((double)a))
#define Srand() srand((int)time(0))
#define random(number) (rand()%number)
#define random_range(a, b) (int)(((double)rand()/RAND_MAX)*(b-a) + a) using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VI;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<PII, int> VIII;
typedef VI:: iterator IT;
typedef map<string, int> Mps;
typedef map<int, int> Mpi;
typedef map<int, PII> Mpii;
typedef map<PII, int> Mpiii;
const int maxn = + ;
int a[maxn];
int n, k;
LL ans;
int T[maxn]; void solve(int l, int r) {
if(l == r || l > r)
return;
int m = l+(r-l)/;
solve(l, m);
solve(m+, r);
int p = l, q = m+;
int cnt = ;
while(p <= m || q <= r) {
if(p > m|| (q <= r && a[q] < a[p]))
T[cnt++] = a[q++];
else {
ans = ans + (q-m-);
T[cnt++] = a[p++];
}
}
REP(i, cnt)
a[l+i] = T[i];
}
int main() { while(scanf("%d%d", &n, &k) == ) {
ans = ;
Rep(i, , n+) scanf("%d", a+i);
solve(, n);
cout<<max(0LL, ans-k)<<endl;
}
return ;
}

树状数组写法:

 //Template updates date: 20140718
#include <iostream>
#include <sstream>
#include <cstdio>
#include <climits>
#include <ctime>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <string>
#include <stack>
#include <set>
#include <map>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#define esp 1e-6
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
#define pb push_back
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define lowbit(x) (x&(-x))
#define mp(a, b) make_pair((a), (b))
#define bit(k) (1<<(k))
#define iin freopen("pow.in", "r", stdin);
#define oout freopen("pow.out", "w", stdout);
#define in freopen("solve_in.txt", "r", stdin);
#define out freopen("solve_out.txt", "w", stdout);
#define bug puts("********))))))");
#define Inout iin oout
#define inout in out #define SET(a, v) memset(a, (v), sizeof(a))
#define SORT(a) sort((a).begin(), (a).end())
#define REV(a) reverse((a).begin(), (a).end())
#define READ(a, n) {REP(i, n) cin>>(a)[i];}
#define REP(i, n) for(int i = 0; i < (n); i++)
#define VREP(i, n, base) for(int i = (n); i >= (base); i--)
#define Rep(i, base, n) for(int i = (base); i < (n); i++)
#define REPS(s, i) for(int i = 0; (s)[i]; i++)
#define pf(x) ((x)*(x))
#define mod(n) ((n))
#define Log(a, b) (log((double)b)/log((double)a))
#define Srand() srand((int)time(0))
#define random(number) (rand()%number)
#define random_range(a, b) (int)(((double)rand()/RAND_MAX)*(b-a) + a) using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VI;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<PII, int> VIII;
typedef VI:: iterator IT;
typedef map<string, int> Mps;
typedef map<int, int> Mpi;
typedef map<int, PII> Mpii;
typedef map<PII, int> Mpiii;
const int maxn = + ;
int a[maxn];
int r[maxn], pos[maxn];
int n, k;
LL sum[maxn];
bool cmp(int v1, int v2) {
return a[v1] < a[v2] || (a[v1] == a[v2] && v1 < v2);
}
void update(int x) {
while(x <= n) {
sum[x] += ;
x += lowbit(x);
}
}
LL getsum(int x) {
LL ans = ;
while(x > ) {
ans += sum[x];
x -= lowbit(x);
}
return ans;
}
int main() { while(scanf("%d%d", &n, &k) == ) {
Rep(i, , n+) scanf("%d", a+i), r[i] = i, sum[i] = ;
sort(r+, r+n+, cmp);
Rep(i, , n+)
pos[r[i]] = i;
LL ans = ;
Rep(i, , n+) {
update(r[i]);
ans += i-getsum(r[i]);
}
cout<<max(0LL, ans-k)<<endl;
}
return ;
}

 

1004 Linear recursive sequence

 题意:根据公式求指定某一项

题解分析在这里。

http://www.cnblogs.com/rootial/p/3920599.html

1007 Permutation

题意:给定n个数(n<=40),以及m种关系即第ai个数必须小于第bi个数,求这样的排列有多少个?

分析:m种关系显然构成一个拓扑图,且各个连通图之间互相独立,因此单个考虑每个连通图的可能性,比如一个连通图中有k个点,

从n个数中任选k个数,然后求这个图中能有多少个安排方案满足拓扑关系。

研究标程,具体实现是,通过并查集找出连通关系,(话说一直没这么用过,还想着去dfs求,弱菜),然后重新编号,通过cover[i] 表示从点i出发的能到达的点,先用于i有直接变得点初始化,然后floyd求连通性。

具体dp时,转移时这样的,dp[st|1<<i] += dp[st], st 满足(st&1<<i) == 0 && (st&cover[i]) == cover[i].

代码:

1009 Exclusive or

题意 :求f(n) = Sum{i^(n-i)| 1<=i <= n-1}.

分析:

此题关键在于对奇数和偶数分开求递推公式。

若n为偶数,设n = 2*k, 1<=i <= 2*k-1,分别考虑i为奇数和i为偶数时的值。

f(n) = i为奇数时+i为偶数 = sum{(2*i-1)^(n-(2*i-1))| 1<= i <= k} + sum{(2*i)^(n-2*i)| 1 <= i <= k-1}

= sum{(2*i-1)^(2*k-2*i+1)|1<=i <=k} + sum{(2*i)^(2*(k-i))| 1 <= i <= k-1}

  = sum{(2*(i-1))^(2*(k-i))|1<= i <= k} +  2*sum{i^(k-i)| 1 <= i <= k-1}

  = 2*sum{(i-1)^(k-i)|1<= i <= k} + 2*f(k)

  = 2*sum{(i-1)^(k-1-(i-1)) | 1 <= i <= k} + 2*f(k)

  = 2*sum{i^(k-1-i) | 0 <= i <= k-1} + 2*f(k)

  = 2*f(k-1)+2*f(k)+4*k-4

若n为奇数,令n = 2*k+1, 1 <= i <= 2*k, 同样分析可得f(n) = 4*f(k) + 6*k.

然后利用大数算出即可。

代码:

 #pragma comment(linker, "/STACK:167772160")
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <set>
#define in freopen("solve_in.txt", "r", stdin);
#define Rep(i, base, n) for(int i = (base); i < n; i++)
#define REP(i, n) for(int i = 0; i < (n); i++)
#define REPS(s, i) for(int i = 0; (s)[i]; i++)
#define VREP(i, n, base) for(int i = (n); i >= (base); i--)
#define SET(a, n) memset(a, (n), sizeof(a));
#define pb push_back
#define mp make_pair using namespace std;
typedef vector<unsigned short> VI;
typedef pair<unsigned short, unsigned short> PII;
typedef vector<PII> VII;
typedef long long LL;
const int maxn = ;
const int B = ; struct BigInt {
int dig[maxn], len;
BigInt(int num = ):len(!!num) {
memset(dig, , sizeof dig);
dig[] = num;
}
int operator[] (int index)const {
return dig[index];
}
int& operator[] (int index) {
return dig[index];
}
BigInt normalize() {
while(len && dig[len-] == )
len--;
return *this;
}
void output() {
if(len == ) {
cout<<<<endl;
return;
}
printf("%d", dig[len-]);
for(int i = len-; i >= ; i--)
printf("%04d", dig[i]);
cout<<endl;
}
};
bool operator < (const BigInt &a, const BigInt &b) {
if(a.len != b.len) return a.len < b.len;
for(int i = a.len-; i >= ; i--) if(a[i] != b[i])
return a[i] < b[i];
return false;
}
BigInt operator + (const BigInt &a, const BigInt &b) {
BigInt c;
c.len = max(a.len, b.len)+;
for(int i = , delta = ; i < c.len; i++) {
delta += a[i]+b[i];
c[i] = delta%B;
delta /= B;
}
return c.normalize();
}
BigInt operator - (const BigInt &a, const int &b) {
BigInt c;
c.len = a.len;
for(int i = , delta = -b; i < c.len; i++) {
delta += a[i];
c[i] = delta;
delta = ;
if(c[i] < ) {
c[i] += B;
delta = -;
}
}
return c.normalize();
}
BigInt operator * (const BigInt &a, const BigInt &b) {
BigInt c;
c.len = a.len+b.len+;
for(int i = ; i < a.len; i++)
for(int j = , delta = ; j <= b.len; j++) {
delta += a[i]*b[j]+c[i+j];
c[i+j] = delta%B;
delta /= B;
}
return c.normalize();
}
BigInt operator / (const BigInt &a, const int &b) {
BigInt c;
c.len = a.len;
for(int i = a.len-, delta = ; i >= ; i--) {
delta = a[i] + delta*B;
c[i] = delta/b;
delta %= b;
}
return c.normalize();
}
char s[];
typedef map<BigInt, BigInt> MPS;
MPS mps; BigInt solve(BigInt n) {
BigInt nn;
nn = n/;
// n.output();
// nn.output();
if(!mps.count(n)) {
if(n[]&) {
mps[n] = solve(nn)* + nn*;
} else {
mps[n] = solve(nn-)*+solve(nn)*+nn*-;
}
}
return mps[n];
}
int tmp[] = {, , , }; int main() { mps[] = ;
mps[] = ;
mps[] = ;
mps[] = ;
while(scanf("%s", s) == ) {
BigInt x;
int len = strlen(s);
reverse(s, s+len);
for(int i = ; i < len; i++) {
int id = i/+;
x.len = max(x.len, id);
x[id-] = x[id-] + (s[i]-'')*tmp[i%];
}
solve(x).output();
}
return ;
}

 1010 Matrix multiplication

 题意: 2个n*n的矩阵,输出相乘后每个元素对3取模后矩阵。

分析:输入时对每个元素取模,则剩下1*1,1*2,2*1,2*2这几种相乘情况,利用bitset,将相应的为1,2 的元素单独拿出来考虑,然后按照上面4种情况,分别求出相乘后的结果,最后加起来取模。

ADD:学习了一下bitset位集的使用。

代码:

 #include <iostream>
#include <bitset>
#include <cstdio>
#define in freopen("solve_in.txt", "r", stdin); using namespace std;
const int maxn =;
bitset<maxn> b[][][maxn];
int n; int main() { while(~scanf("%d", &n)) {
for(int k = ; k < ; k++)
for(int i = ; i < n; i++)
for(int j = ; j < n; j++) {
int t;
scanf("%d", &t);
t %= ;
if(k == ) {
b[k][][j][i] = b[k][][j][i] = ;
if(t == )
b[k][][j][i] = ;
else if(t == )
b[k][][j][i] = ;
} else {
b[k][][i][j] = b[k][][i][j] = ;
if(t == )
b[k][][i][j] = ;
else if(t == )
b[k][][i][j] = ;
}
}
for(int i = ; i < n; i++)
for(int j = ; j < n; j++) {
int t1 = (b[][][i]&b[][][j]).count();
int t2 = (b[][][i]&b[][][j]).count()*;
int t3 = (b[][][i]&b[][][j]).count()*;
int t4 = (b[][][i]&b[][][j]).count();
int ans = (t1+t2+t3+t4)%;
printf("%d%c", ans, j == n- ? '\n' : ' ');
}
}
return ;
}