记 2024.4.19
P3182
[HAOI2016] 放棋子
题目描述
给你一个 的矩阵,每行有一个障碍,数据保证任意两个障碍不在同一行,任意两个障碍不在同一列,要求你在这个矩阵上放 枚棋子(障碍的位置不能放棋子),要求你放 个棋子也满足每行只有一枚棋子,每列只有一枚棋子的限制,求有多少种方案。
输入格式
第一行一个 ,接下来一个 的矩阵。,0 表示没有障碍,1 表示有障碍。
输出格式
一个整数,即合法的方案数。
样例 #1
样例输入 #1
2
0 1
1 0
样例输出 #1
1
给出的障碍是没用的,考虑错排问题,自己想想就知道了。
注意以后要多方面思考问题,观察题目性质。
Code
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
bool operator<(vector<int> a,vector<int> b)
{
if(a.size()!=b.size()) return a.size()<b.size();
for(int i=a.size()-1;i>=0;i--)
{
if(a[i]!=b[i]) return a[i]<b[i];
}
return 0;
}
vector<int> operator-(vector<int> a,vector<int> b)
{
vector<int> c;
int t=0;
for(int i=0;i<a.size();i++)
{
t=a[i]-t;
if(i<b.size()) t=t-b[i];
c.push_back((t+10)%10);
if(t<0) t=1;
else t=0;
}
while(c.size()>1&&c.back()==0) c.pop_back();
return c;
}
vector<int> operator/(vector<int> a,vector<int> b)
{
if(a<b) return {0};
int la=a.size(),lb=b.size();
int dv=la-lb;
vector<int> c(dv+10,0);
reverse(b.begin(),b.end());
for(int i=0;i<dv;i++) b.push_back(0);
reverse(b.begin(),b.end());
lb=la;
for(int i=0;i<=dv;i++)
{
while(!(a<b))
{
a=a-b;
c[dv-i]++;
}
b.erase(b.begin());
}
while(c.size()>1&&c.back()==0) c.pop_back();
return c;
}
vector<int> eq(string b)
{
vector<int> a;
for(int i=b.length()-1;i>=0;i--) a.push_back(b[i]-'0');
return a;
}
vector<int> operator+(vector<int> a,vector<int> b)
{
vector<int> c(max(a.size(),b.size())+7);
int t=0;
for(int i=0;i<=max(a.size(),b.size());i++)
{
if(i<a.size()) t+=a[i];
if(i<b.size()) t+=b[i];
c[i]=t%10;
t/=10;
}
while(c.size()>1&&c.back()==0) c.pop_back();
return c;
}
vector<int> to_vector(int n)
{
vector<int> a;
while(n)
{
a.push_back(n%10);
n/=10;
}
return a;
}
int to_int(vector<int> a)
{
int x=0;
for(int i=a.size()-1;i>=0;i--)
{
x*=10;
x+=a[i]%10;
}
return x;
}
vector<int> operator*(vector<int> a,vector<int> b)
{
vector<int> c(a.size()+b.size()+7);
for(int i=0;i<a.size();i++)
{
for(int j=0;j<b.size();j++)
{
c[i+j]+=a[i]*b[j];
}
}
int t=0;
for(int i=0;i<c.size();i++)
{
t+=c[i];
c[i]=t%10;
t/=10;
}
while(c.size()>1&&c.back()==0) c.pop_back();
return c;
}
vector<int> to_ten(string s,int n)
{
vector<int> sum;
sum.push_back(0);
for(int i=s.length()-1;i>=0;i--)
{
int x=s[i]-'0';
if(s[i]>='A'&&s[i]<='Z') x=s[i]-'A'+10;
vector<int> summ;
summ.push_back(1);
vector<int> tmp;
tmp=to_vector(n);
for(int j=1;j<=s.length()-1-i;j++)
{
summ=summ*to_vector(n);
}
tmp=to_vector(x);
sum=sum+summ*to_vector(x);
}
return sum;
}
string to_m(vector<int> x,int m)
{
string y;
while(x.size()>1||x[0]!=0)
{
int t;
if(x<to_vector(m)) t=to_int(x);
else t=to_int(x-x/to_vector(m)*to_vector(m));
if(t>9) t=(t-10)+'A';
else t=t+'0';
y+=t;
x=x/to_vector(m);
}
string ans;
for(int i=y.length()-1;i>=0;i--) ans+=y[i];
return ans;
}
void put(vector<int> a)
{
while(a.size()) cout<<a.back(),a.pop_back();
}
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
int tmp;
cin>>tmp;
}
}
vector<int> last1,last2,now;
last1.push_back(0);
last2.push_back(1);
if(n==1)
{
put(last1);
return 0;
}
if(n==2)
{
put(last2);
return 0;
}
for(int i=3;i<=n;i++)
{
now=to_vector(i-1)*(last2+last1);
last1=last2;
last2=now;
}
put(now);
return 0;
}
超级无敌高精板子
P2885
[USACO07NOV] Telephone Wire G
题目描述
Farmer John's cows are getting restless about their poor telephone service; they want FJ to replace the old telephone wire with new, more efficient wire. The new wiring will utilize N (2 ≤ N ≤ 100,000) already-installed telephone poles, each with some heighti meters (1 ≤ heighti ≤ 100). The new wire will connect the tops of each pair of adjacent poles and will incur a penalty cost C × the two poles' height difference for each section of wire where the poles are of different heights (1 ≤ C ≤ 100). The poles, of course, are in a certain sequence and can not be moved.
Farmer John figures that if he makes some poles taller he can reduce his penalties, though with some other additional cost. He can add an integer X number of meters to a pole at a cost of X2.
Help Farmer John determine the cheapest combination of growing pole heights and connecting wire so that the cows can get their new and improved service.
给出若干棵树的高度,你可以进行一种操作:把某棵树增高h,花费为h*h。
操作完成后连线,两棵树间花费为高度差*定值c。
求两种花费加和最小值。
输入格式
* Line 1: Two space-separated integers: N and C
* Lines 2..N+1: Line i+1 contains a single integer: heighti
输出格式
* Line 1: The minimum total amount of money that it will cost Farmer John to attach the new telephone wire.
样例 #1
样例输入 #1
5 2
2
3
5
1
4
样例输出 #1
15
又看题解了......
状态表示 表示第 颗树,高度为 的花费。
性质 MAX。
状态转移方程就就不说了,自己推。
注意,将不变量作为状态,一定要仔细斟酌是不是不变量。
Code
#include <iostream>
#include <cstring>
using namespace std;
const int N=1e5+10,M=110;
int f[N][M];
int n,c;
int a[N];
int main()
{
cin>>n>>c;
int maxn=-0x3f3f3f3f;
for(int i=1;i<=n;i++) cin>>a[i],maxn=max(maxn,a[i]);
memset(f,0x3f,sizeof f);
for(int i=a[1];i<=maxn;i++) f[1][i]=(i-a[1])*(i-a[1]);
for(int i=2;i<=n;i++)
{
for(int j=a[i];j<=maxn;j++)
{
for(int k=a[i-1];k<=maxn;k++)
{
f[i][j]=min(f[i][j],abs(a[i]-j)*abs(a[i]-j)+f[i-1][k]+c*abs(k-j));
}
}
}
int minn=0x3f3f3f3f;
for(int i=1;i<=maxn;i++)
{
minn=min(minn,f[n][i]);
}
cout<<minn<<endl;
return 0;
}
另外,可以单调队列优化,但今天算了,明天看看。

浙公网安备 33010602011771号