CCF CSP 第十九次認證
阿新 • • 發佈:2020-08-18
機器學習派上了用場
#include<bits/stdc++.h> using namespace std; int getsign(long long x){ return x<0?-1:1; } int main(int argc, char const *argv[]) { int n; int t; cin>>n>>t; map<char, vector<pair<long long,long long>>> p; int x,y; char c; for(int i=0; i<n; i++){ cin>>x>>y>>c; p[c].push_back(make_pair(x,y)); } while(t--){ long long a1,a2,a3; cin>>a1>>a2>>a3; long long A = a1+p['A'][0].first*a2+p['A'][0].second*a3; long long B = a1+p['B'][0].first*a2+p['B'][0].second*a3; if(getsign(A)==getsign(B)){ cout<<"No"<<endl; continue; } bool flag = false; for(auto pp:p['A']){ long long temp = a1+pp.first*a2+pp.second*a3; if(getsign(temp)*getsign(A)<0){ flag = true; break; } } for(auto pp:p['B']){ long long temp = a1+pp.first*a2+pp.second*a3; if(getsign(temp)*getsign(B)<0){ flag = true; break; } } if(flag) cout<<"No"<<endl; else cout<<"Yes"<<endl; } return 0; }
雙指標
#include<bits/stdc++.h> using namespace std; int main(int argc, char const *argv[]) { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin>>n; int a, b; cin>>a>>b; vector<pair<long long,long long>> X(a),Y(b); long long ans = 0; long long x,y; for (int i = 0; i < a; ++i) { cin>>x>>y; X[i] = make_pair(x,y); } for (int i = 0; i < b; ++i) { cin>>x>>y; Y[i] = make_pair(x,y); } sort(X.begin(), X.end()); sort(Y.begin(), Y.end()); int i=0,j=0; while(i<a&&j<b){ if(X[i].first==Y[j].first) ans += X[i++].second* Y[j++].second; else if(X[i].first<Y[j].first) i++; else j++; } cout<<ans<<endl; return 0; }
閱讀理解題,後面太麻煩了
先騙四十分
#include<bits/stdc++.h> using namespace std; string gets1(string s){ string res = ""; int l = 0; int r = s.length()-1; while(l<=r&&s[l]==' ') l++; while(l<=r&&s[r]==' ') r--; while(l<=r) res += s[l++]; return res; } int main(int argc, char const *argv[]) { string s; int n; // freopen("test.dat","r", stdin); cin>>n; // getchar(); int ans = 0; int state = 0; string temp = ""; int len = 0; while(getline(cin,s)){ // cout<<s; string res = gets1(s); if(res.length()==0){ //遇到了一個新的段落 if(state==0) continue; for(int i=0; i<temp.length(); i++){ if(len%n==0&&temp[i]==' ') continue; else{ len++; } } // cout<<len<<endl; ans += len/n + ((len%n)>0)+1; temp = ""; len = 0; state = 0; // cout<<ans<<endl; }else{ temp+= " "+res; state = 1; } } // cout<<len<<endl;ans += len/n + ((len%n)>0)+1; if(temp.length()){ // if(len%n!=0) // cout<<endl; for(int i=0; i<temp.length(); i++){ if(len%n==0&&temp[i]==' ') continue; else{ len++; } } ans += len/n + ((len%n)>0)+1; } cout<<ans-1<<endl; return 0; } // 段落: 去掉每行首位的連續空格 // 每行開頭如果是空格,那麼要去掉空格 // 如果有多行, 則將各行用一個空格連結起來 // 段落與段落之間或者段落與空行之間應該空出一行間距 // 專案: //
矩陣快速冪
能過96分,第一次接觸矩陣快速冪
判重需要仔細考慮,兩個的時候只能考慮單個數字生成
和相鄰數字生成的新的對
#include<bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
vector<vector<long long>> se={
{0,0,1,0,0,0,0,0,0,0,0,0,0,0},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,1,0,1,0,0,0,0,0,0,0,0,0,0},
{0,0,1,1,0,0,0,0,0,0,0,0,0,0},
{0,0,1,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,1,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,1,0,0},
{0,0,0,0,0,0,0,0,0,0,1,0,0,0},
{0,0,0,0,0,1,0,0,0,0,0,0,1,0},
{0,0,0,0,0,0,0,1,0,0,0,0,0,0},
{0,0,0,0,0,0,1,0,0,0,0,0,0,0},
{0,0,0,1,0,0,0,0,0,0,0,0,0,1},
{0,0,0,0,0,0,0,0,1,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,1,0,0,0,0}
};
// vector<vector<long long>> seini = {{1,0,0,0,0,0,0,0,0,0,0,0,0,0}};
vector<long long> fi = {1,2,4,6,16,26,41,44,46,61,62,64,66,42};
vector<vector<long long>> mmul(vector<vector<long long >> a, vector<vector<long long>> b){
int n = a.size();
int m = a[0].size();
int mm = b[0].size();
vector<vector<long long>> aa(n, vector<long long>(mm, 0LL));
for(int i=0; i<n; i++){
for(int j=0; j<mm;j++){
for(int k=0; k<m; k++){
aa[i][j] = ((aa[i][j]%mod)+((a[i][k]%mod)*(b[k][j]%mod))%mod)%mod;
}
}
}
return aa;
}
vector<vector<long long>> matrix_mul(vector<vector<long long>> a, int t){
vector<vector<long long>> res(14, vector<long long>(14, 0LL));
for(int i=0; i<14; i++){
for(int j=0; j<14; j++){
res[i][j]=0;
if(i==j)
res[i][j]=1LL;
}
}
while(t){
if(t&1){
res = mmul(a,res);
}
a = mmul(a,a);
t = t>>1;
}
return res;
}
int main()
{
int n;
cin>>n;
string s;
cin>>s;
int targ = stoi(s);
se = matrix_mul(se, n);
for(int i=0; i<14; i++){
if(fi[i]==targ){
cout<<se[i][0]<<endl;
return 0;
}
}
cout<<0<<endl;
return 0;
}
只會dfs暴力前四十分,但是不知道為啥只能過三十分
離譜了,想了一天也沒看出來哪裡錯了,隨緣了
#include<bits/stdc++.h>
#define LL long long
using namespace std;
// 0表示不選
// 1表示選邊界
// 2表示不選邊界
vector<vector<LL>> a(1100, vector<LL>(5,0));
LL n, m;
LL used[1100];
LL buy[1100];
LL ans = 0;
vector<pair<LL,pair<LL, LL>>> sss;
bool judge(){
for(auto ss:sss){
if(ss.second.second==1){
if(used[ss.first]&&!used[ss.second.first])
return false;
}else{
if(used[ss.first]==1&&!used[ss.second.first])
return false;
}
}
return true;
}
LL cal(){
LL res = 0;
for(LL i=1; i<=n; i++){
res += buy[i];
}
return res;
}
LL cal_2(LL i, LL x){
return a[i][2]*x*x+a[i][3]*x+a[i][4];
}
void dfs(LL i){
if(i==n+1){
if(judge())
ans = max(ans, cal());
// cout<<endl<<cal()<<endl;
return;
}
for(LL t=0; t<3; t++){
used[i]=t;
buy[i]=0;
if(t==1){
buy[i] = max(cal_2(i,a[i][0]), cal_2(i,a[i][1]));
}
else if(t==2){
if(a[t][2]==0){
buy[i]=max(cal_2(i,a[i][0]+1), cal_2(i,a[i][1]-1));
}else{
buy[i]=max(cal_2(i,a[i][0]+1), cal_2(i,a[i][1]-1));
LL temp = (-a[i][1]/(2LL*a[i][0]));
if(temp>a[i][0]&&temp<a[i][1])
buy[i] = max(buy[i], cal_2(i, temp));
temp -= 1;
if(temp>a[i][0]&&temp<a[i][1])
buy[i] = max(buy[i], cal_2(i, temp));
temp += 2;
if(temp>a[i][0]&&temp<a[i][1])
buy[i] = max(buy[i], cal_2(i, temp));
}
}
dfs(i+1);
}
}
int main()
{
ans = 0;
memset(used,0,sizeof used);
memset(buy,0,sizeof buy);
cin>>n>>m;
for(LL i=1; i<=n; i++){
cin>>a[i][0]>>a[i][1]>>a[i][2]>>a[i][3]>>a[i][4];
}
LL x,y,c;
for(LL i=0; i<m; i++){
cin>>c>>x>>y;
sss.push_back(make_pair(x,make_pair(y,c)));
}
dfs(1);
cout<<ans<<endl;
return 0;
}
離譜的,不知道九月考試能考多少分