1003
题目链接
Clockwise or Counterclockwise
水题,经历过牛客应该知道叉积可以判断顺逆时针。
叉积参考:叉积
参考代码
#include<iostream>
#include<math.h>
#include<string.h>
#include <algorithm>
#include <numeric>
#include <vector>
#include <functional>
#include <list>
#include <ctype.h>
#include<map>
#include <set>
#define M 100
#define ll long long
using namespace std;
int main(){
int t;
scanf("%d",&t);
while(t--){
ll x1,y1,x2,y2,x3,y3;
scanf("%lld %lld %lld %lld %lld %lld",&x1,&y1,&x2,&y2,&x3,&y3);
ll a=(x2-x1)*(y3-y1);
ll b=(y2-y1)*(x3-x1);
if((a-b)<0) printf("Clockwise\n");
else printf("Counterclockwise\n");
}
}
1006-Fluctuation Limit
题目链接:Fluctuation Limit
思路:从后往前选择,缩小区间找。
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
#define ll long long
#define P pair<ll, ll> //first l,second r
const ll N = 1e5 + 5;
int n, k;
int main() {
int t ;
scanf("%d",&t);
while (t--) {
scanf("%d %d",&n,&k);
vector <P> a; //存储题目中的区间
int flag=1;
for (int i = 0; i < n; i++) {
ll l, r;
scanf("%lld %lld",&l,&r);
a.push_back({l, r});
}
vector <P> b;
b.push_back(a[0]);
for (int i = 1; i < n; i++) {
ll stl = b[i-1].first - k, str = b[i-1].second + k;
ll l, r;
l = max(stl, a[i].first);
r = min(str, a[i].second);
if (l > r) {
printf("NO\n");
flag=0;
break;
}
b.push_back({l, r});
} vector<ll> res;
if(flag){
res.push_back(b[n-1].first);
for (int i = n-2; i >= 0; i--) {
ll stl = res[n-2-i] - k, str = res[n-2-i] + k;
ll l = max(stl, b[i].first);
ll r = min(str, b[i].second);
res.push_back(l);
}
printf("YES\n");
for (int i = res.size() - 1; i >= 0; i--) {
printf("%d ", res[i]);
}
printf("\n");
}
a.clear();b.clear();res.clear();
}
return 0;
}
解决字符串同构问题的最小表示法:参考链接
看懂了上面的最小表示法基本就能看懂下面代码。
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
vector<int> factor;
string s,temp,s1;
bool is_prime(int n) {
if(n < 2) return false;
for(int i = 2; i <= sqrt(n); ++i) {
if(n % i == 0) return false;
}
return true;
}
string mins(string s) {//最小表示法的改编
int n = s.length();
s += s;
int i = 0, j = 1, k ;
while(i < n && j < n) {
for(k = 0; k < n -1 && s[i+k] == s[j+k]; k++);
if(k == n-1) break;
if(s[i+k] > s[j+k]) {
i = i + k + 1;
if(i == j) i++;
}
else {
j = j + k + 1;
if(j == i) j++;
}
}
int pos = min(i,j);
return s.substr(pos, n);
}
int main() {
int t,n;
// cin>>t;
scanf("%d",&t);
while(t--) {
//cin>>n;
scanf("%d",&n);
factor.clear();//存放n的因子
for(int i = 1; i*i <= n; i ++ ) {
if(n % i == 0) {
factor.push_back(i);
if(i != n/i) factor.push_back(n/i) ;
}
}
cin>>s;
bool f = false;
if(is_prime(n)) {//质数且字符全部相同则Yes
int num = count(s.begin(),s.end(),s[0]);
if(num == n) f = true;
}
else {
for(int i = 0; i < factor.size() ; i++) {
int len = factor[i];
if (len==n) {continue;}//长度为n时
temp = mins(s.substr(0, len));
int num = 1, k = n/len, pos = 0;
for(int j = 1; j < k; j ++) {//判断是否是同构
pos += len;
s1 = s.substr(pos, len);
if(mins(s1) == temp) {
num++;
}
else break;
}
if(num == k) {
f = true;
break;
}
}
}
if(f) printf("Yes\n");
else printf("No\n");
}
return 0;
}
菜鸡还需要慢慢学习,orz